One executable that starts as a GUI application or console application based on command line in Visual Studio 2005

Solution 1:

I think the preferred technique for the situation here is the ".com" and ".exe" method. In Windows from the command line, if you run a program and don't specify an extension, the order of precedence in locating the executable will .com preferred over a .exe file.

Then you can use tricks to have that ".com" be a proxy for the stdin/stdout/stderr and launch the same-named .exe file. This give the behavior of allowing the program to preform in a command-line mode when called form a console (potentially only when certain command-line arguments are detected) while still being able to launch as a GUI application free of a console.

There are various articles describing this, like "How to make an application as both GUI and Console application?" (see references in link below).

I hosted a project called dualsubsystem on google code that updates an old codeguru solution of this technique and provides the source code and working example binaries.

I hope that is helpful!

Solution 2:

You can't. See this article by Raymond Chen:

How do I write a program that can be run either as a console or a GUI application?

For the reasons given in this article you sometimes see two versions of the same tool provided, one suffixed with 'w' such as in java.exe and javaw.exe on Windows.

However you might implement this clever workaround: How to make an application as both GUI and Console application.

Solution 3:

Have you tried calling AttachConsole in your program to get the output redirected to the calling terminal?