views:

512

answers:

4

I have a qt application in VS2005 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 --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 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 console application based on command line parameters?

PS. I use Qt4.2.0 and Visual Studio 2005 and the project is C++

+1  A: 

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

Konrad Rudolph
could you please give me a hint what header files and macro definition I need to be able to use AttachConsole? I tried #include <windows.h>#include <Wincon.h>, but apparently that is not enough to find AttachConsole function
balint.miklos
The documentation says to include `<windows.h>` and this is definitely enough. Did you forget the argument when calling the function?
Konrad Rudolph
AllocConsole() may also be appropriate: http://msdn.microsoft.com/en-us/library/ms681944.aspx
RaphaelSP
The AllocConsole() works for me, and opens a new console even if I start from a console. Anyway thanks for the answer
balint.miklos
I did try it with AttachConsole(ATTACH_PARENT_PROCESS) but the compiler told me doesn't find the identifier AttachConsole... with #include <windows.h>
balint.miklos
+3  A: 

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.

0xA3
Sorry, but this information is simply wrong in this particular, and `AllocConsole` will work just fine. What it doesn't do is create a new console when none exists – but this was not asked (since an extra argument has to be entered here to launch the console version anyway). Several people have also made this comment to Raymond's post.
Konrad Rudolph
Sorry, I meant `AttachConsole`, *not* `AllocConsole` (the latter does something else). Also, I forgot a word: "case".
Konrad Rudolph
@Konrad: From my understanding `AttachConsole` would only work if your parent process is cmd.exe, i.e. starting the application with --nogui from a shortcut would not work. IMHO there is no general solution to this problem, only some workarounds.
0xA3
@divo: your understanding is indeed correct (although it would still be possible to fall back to `AllocConsole` if the parent process isn't cmd.exe -- an information that can be retrieved using other WinAPI functions).
Konrad Rudolph
+3  A: 

I know my answer is coming in late, but 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 prefer a .com over a .exe.

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 args 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!

gabeiscoding
A: 

To use AttachConsole you need to include the following line before including :

//require at least windows XP

define _WIN32_WINNT 0x0501

Bruce