Detect if application was running in a new or existing console

How do I know if a console application is running from another console or if a new console opens at startup?

For example, if I want the console to have some custom colors, if it opens a new console window when it starts, or just leaves it as it is if it was launched from another console. (How does PowerShell.exe do this)

+4
source share
1 answer

Just run the application in a new window (shortcut, application), configured to transfer a specific parameter on the command line to your executable file to indicate that the environment should be painted.

For instance, myapp.exe -RunInNewConsole

- , AttachConsole:

    [DllImport("kernel32.dll")]
    static extern bool AttachConsole(int dwProcessId);

    private static bool IsRunningInConsole()
    {
        return AttachConsole(-1);
    }
+1

All Articles