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

Issue

I have a Qt application in Visual Studio 2005 which is linked using \subsystem:windows such that when I run the compiled executable it does not create a command line terminal, as well.

I would like to create a command-line mode: when I start it with the --nogui command line argument, then the GUI is not presented, but a simple command-line program is run. Since the linking uses /subsystem:windows, the command line mode doesn’t show any of the std::cout outputs unless I link my executable with \subsystem:console.

Is there a way to set the compilation/linking such that the same executable can either present the GUI windows or behave as a console application based on command-line parameters?

PS. I use Qt 4.2.0 and Visual Studio 2005 and the project is in C++.

Solution

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!

Answered By – gabeiscoding

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *