PDA

View Full Version : how to enable an app to be executed in GUI mode and console mode



locke
19th August 2010, 12:49
Hi guys,

I made an App with GUI. But now I would like to manage the options in the command line.

Examaple, if I execute the MyApp.exe without parameters (or by clicking the icon), then It should launch the Main window, etc.

But If I execute form the command line something like MyApp.exe -version, I would like to get only the version, without launching the App.

I modified the .pro file and added


CONFIG += console


in order to return the version with a cout sentence in the main.cpp.

But now if I execute the app without any argument, the console is launched at the same time of the main window. So the console keeps behind the main window.

What am I missing? There is another way to do that?

Thanks in advance!

tbscope
19th August 2010, 13:05
This should be very easy:



int main(int argc, char *argv[])
{
if (argv[1] == "-version") {
printf("Version");
exit 0;
}

QApplication a(argc, argv);

...

return a.exec();
}

locke
19th August 2010, 13:13
This is not the problem. I've made alreadyt the code, the problem is that, If I configure the .pro file as CONFIG += console. Then when I start the app with no arguments, it lauches the console and the main window as well. I this case I dont want to see thjie console, just the main window.

tbscope
19th August 2010, 13:29
Don't add config += console

locke
19th August 2010, 13:30
If I dont add config+=console, when I rund the program from the command promp with the -version argument, then nothing appears.

tbscope
19th August 2010, 13:47
Then I don't know. Is this even possible in Windows?

Here you have a GUI application where you can perfectly add an about dialog with the version number.
Why do you want to print that in the console?

The Storm
19th August 2010, 13:49
I don't think that the behavior you want is possible under Windows platform...

locke
19th August 2010, 13:52
I want to print it in the console because an external software will made some checks about the version. So the real user will run the app just from the icon (without parameters, ) but the other checking software will run it with the versiona argument, in order to get the version and make some checks.

The Storm
19th August 2010, 14:02
Nice but not possible on Windows. Though you can always try with IPC, if you have the sources of the other software. :)

tbscope
19th August 2010, 14:09
Isn't it possible to set a version number for an exe just like you do for a DLL?

locke
19th August 2010, 14:09
That sounds a bit complicate. Thanks anyways

squidge
19th August 2010, 14:14
You can use AllocConsole(). If your app already has a console it'll fail, otherwise it'll open a new console and set the appropriate handles for you (by default, GUI applications do not have a console)

You can then use GetStdHandle(STD_OUTPUT_HANDLE) to get a handle to your console to which you can write to using WriteFile() or WriteConsole().

http://msdn.microsoft.com/en-us/library/ms681944%28VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms687401%28v=VS.85%29.aspx