4

How can I detect if my console application was started from another console, or if it opened a new console upon launch?

For example if I want the console to have some custom colors if it opened a new console window upon starting it, or to just leave everything as it is if started from another console. (Much like PowerShell.exe seems to do)

DeCaf
  • 6,026
  • 1
  • 29
  • 51
  • In the duplicate someone wrote the C# code for it [here](http://stackoverflow.com/a/12356147/188246). – David Sherret Oct 10 '14 at 21:19
  • 1
    Yep, I noticed. No matter how much you search, sometimes the correct page does not seem to show up. Thanks for the link and for your efforts. :) – DeCaf Oct 10 '14 at 21:20

1 Answers1

0

Just have whatever starts the app in a new window (shortcut, application) configured to pass a specific parameter on the command line to your executable to indicate the environment should be colored.

For example, myapp.exe -RunInNewConsole

Another option is to determine if you are already running in a console by using the AttachConsole method:

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

    private static bool IsRunningInConsole()
    {
        return AttachConsole(-1);
    }
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 2
    this is a nice hack but it doesn't answer the question of how to detect if the app was started from a console, or it created its' own console – Mike Dinescu Oct 10 '14 at 20:33
  • @MikeDinescu: why not? If I have an open console window and I type myapp.exe, there is no command line parameter. If I select the shortcut from the start menu, the shortcut is configured with the startup parameter and a new window is opened. If I launch the app from within another app, the launch command is executed with the startup parameter and a new window is opened. – competent_tech Oct 10 '14 at 20:37
  • 2
    This would work if I knew it was started from a shortcut. Which I don't. It might just be the user double-clicking the .exe from Windows Explorer for example. And also, this does not seem to be what PowerShell.exe does. So there has to be a better way. – DeCaf Oct 10 '14 at 20:37
  • @DeCaf when I start powershell from windows explorer there aren't any custom colours. The shortcut has the custom colours defined in the "Colors" tab. – David Sherret Oct 10 '14 at 20:42
  • @David That's strange, it does here. If I do Win+R and type in "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" I get the window with the blue background etc, and this does not appear to be a shortcut as far as I can tell. – DeCaf Oct 10 '14 at 20:45
  • @DeCaf strange, I get the default command prompt colours when I do that. I have Windows 7 on this computer. – David Sherret Oct 10 '14 at 20:50
  • @David Okay, I am running Windows 8.1 here. – DeCaf Oct 10 '14 at 20:52
  • I have updated the answer with an additional approach to try. – competent_tech Oct 10 '14 at 20:52
  • @DeCaf Just to add some information. In Windows 7, I found I can set different custom colours that are reloaded the next time I run it for each of these: 1. running `cmd` then changing the colours, 2. setting colours on powershell shortcut then clicking it, 3. running `%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe` then changing the colours – David Sherret Oct 10 '14 at 20:54
  • Well, your second suggestion returns `false` however I run it it seems. – DeCaf Oct 10 '14 at 20:55
  • @David: Interesting about the color changing through the properties though. Any idea on where this information is stored? If I wanted to set it up for a specific scheme upon installation. – DeCaf Oct 10 '14 at 20:58
  • My guess right now is the PowerShell is not really a console application. I believe it is built as a Win32 application and then it uses the AttachConsole API to determine if it was started in a console. If there is a console it uses that, if not it creates one and attaches to it. – Mike Dinescu Oct 10 '14 at 20:59
  • No, I'm pretty sure it has to be a standard console application, since it is near impossible to get a non-console application to work correctly when launched from a console (and share the same console). – DeCaf Oct 10 '14 at 21:04
  • It would seem that some information is stored in the registry under HKCU\Console however. Perhaps this is the secret in this case. Only question is, how do they get this information in there for newly created users? – DeCaf Oct 10 '14 at 21:04