Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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++.

like image 682
balint.miklos Avatar asked Jan 24 '23 16:01

balint.miklos


2 Answers

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!

like image 153
gabeiscoding Avatar answered Feb 07 '23 08:02

gabeiscoding


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.

like image 45
Dirk Vollmar Avatar answered Feb 07 '23 08:02

Dirk Vollmar