Question

I have curiosity question regarding Console vs Windows Application when running the application from the Cmd, calling the exe directly.
If the application is compiled as a Console Application (will refer to it as my ConApp), when running the app via the cmd, the prompt waits for the app to complete running before continuing. However if the application has been compiled as an Windows Application (will to refer to it as my WinApp), starting the app via cmd the prompt will fire-and-forget the WinApp, understandably this is expected behavior.
To get the WinApp to stop the cmd from firing-and-forgetting, one would have to start the app in cmd with something like "start \wait WinApp.exe", this will cause the Cmd prompt to wait for WinApp to stop running before continuing.
My question around this is how does Console Application inform the Cmd, or how does the Cmd know that it needs to stay open, and not fire-and-forget the ConApp? And is it possible to invoke the same kinda of behavior in my WinApp ie so that I don't have to call "start \wait"?

Note:
I have played around with using AllocConsole and AttachConsole, however when using AttachConsole(-1) in my WinApp, the Cmd still fire-and-forgets the WinApp however the WinApp seems to just open a new Console Window.

Was it helpful?

Solution

This information is stored as a part of the PEOptHeader portion of the EXE binary format. Specifically the Subsystem field tells the operating system what type of application this is and can have the following values

  • 1: Native
  • 2: Windows/GUI
  • 3: Windows non-GUI
  • 5: OS/2
  • 7: POSIX

This is what tells windows what type of program it is and hence gives it the ability to make different choices as cmd does.

Documentation Link

OTHER TIPS

Technical details aside (JaredPar did a really good job of doing that), if you want to change your program from Windows/GUI mode to Windows non-GUI all you need to do is go in to your project settings and change the drop-down from "Windows Application" to "Console Application"

enter image description here

You can have a Window application run as a Console Application and it will give you the behavior you are expecting. (note however, now if you double click on the icon for the program it will open a console window that will stay open while your program is open.)

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top