Share via


How to make an application as both GUI and Console application?

This is another common asked question. But it is a Win32 question.

In Windows GUI applications and Console applications are very different. GUI applications have at least one window, and a message loop, but no standard in/out/error. Console applications have standard in/out/error, but no window, no message loop. An application is either a GUI application or Console application, but not both.

Some people want their application behaves differently depending on input. If there are inputs, the application behaves like Console application. If there is no input, it behaves like GUI applications.

Just like devenv (Visual Studio) and ildasm.

How do they do that?

VisualStudio and ildasm achieve this very differently.

In VisualStudio case, there are actually two binaries: devenv.com and devenv.exe. Devenv.com is a Console app. Devenv.exe is a GUI app. When you type devenv, because of the Win32 probing rule, devenv.com is executed. If there is no input, devenv.com launches devenv.exe, and exits itself. If there are inputs, devenv.com handles them as normal Console app.

In ildasm case, there is only one binary: ildasm.exe. It is first compiled as a GUI application. Later editbin.exe is used to mark it as console subsystem. In its main method it determines if it needs to be run as console mode or GUI mode. If need to run as GUI mode, it relaunches itself as a GUI app.

Comments

  • Anonymous
    February 06, 2004
    So is that the same for cmd.exe? Or does the OS do something special? If I launch it from Win+R it creates a new window, but if I then call cmd again from within that console itself, it creates a new cmd instance and redirects its stdio to the same window.
  • Anonymous
    February 06, 2004
    Are there any issues with simply renaming a .NET console .exe to .com?
  • Anonymous
    February 06, 2004
    Eric,
    See my another post "Name of your-app.exe". Basically we won't be able to find itself in non-default domain because we only probe .dll/.exe

    Consoled,
    It is nothing special for cmd.exe. Console apps by default re-use parent's console, unless you specify a flag in CreateProcess. If launched from explorer, because explorer is a GUI app, a new console is created for cmd.exe (because cmd.exe is a console app). If launched from cmd.exe itself, it re-uses the existing console.
  • Anonymous
    February 14, 2004
    The comment has been removed
  • Anonymous
    February 18, 2004
    I'm not clear on why editbin is needed.

    You mention fundamental features of a GUI app: "GUI applications have at least one window, and a message loop," but there's absolutely nothing stopping a console application having these. The fact that you have a stdin and stdout doesn't stop you from also opening windows.

    There's no real need to use editbin. I just used VS.NET 2003 to build a standard Win32 console application. I then put enough of the code from the good old 'Generic' sample in the PSDK to get a window up and running. Works fine. I have a window that behaves normally, and also a console window that behaves normally.

    This has always worked. The console vs windows app is mainly a matter of a linker switch and supplying the right kind of entry point.
  • Anonymous
    February 18, 2004
    The comment has been removed
  • Anonymous
    July 18, 2004
    The comment has been removed
  • Anonymous
    July 20, 2004
    This is an OS issue and there is probably nothing you can do about it.
  • Anonymous
    June 11, 2009
    PingBack from http://castironbakeware.info/story.php?title=junfeng-zhang-s-windows-programming-notes-how-to-make-an-application