Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Windows Application vs Console Application via Cmd

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.

like image 557
Heinrich Avatar asked Jan 07 '14 00:01

Heinrich


People also ask

How does a Windows based application differ from a console application?

The sole difference is that a console application always spawns a console if it isn't started from one (or the console is actively suppressed on startup). A windows application, on the other hand, does not spawn a console. It can still attach to an existant console or create a new one using AllocConsole .

Is CMD is a console application?

Because users run Cmd.exe or PowerShell.exe and see a Console window appear, they labor under the common misunderstanding that Cmd and PowerShell are, themselves, “Consoles” … they're not!

How do I run a dotnet console app from the command line?

In . NET Core, it runs from the dll, so you have to just run the application by running the command prompt and using the command - dotnet run. Open your command prompt and go to that folder where your application persists. This resulted in printing "Hello World!" as it is written in our console application.


1 Answers

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

like image 149
JaredPar Avatar answered Nov 11 '22 15:11

JaredPar