Click here to Skip to main content
15,913,084 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a application *.exe which is a GUI applicaition. Now I need to convert it to work as both console and GUI i.e.
if any commandline args is entered like :
D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe fdsa
It should simply work as console application wherein application accepts the command args.

if no commandline args are entered it need to work as GUI application.

I did this way:
C++
int main(int argc, char *argv[])
{
 HWND hwndC = GetConsoleWindow();
 QApplication::Type appType = QApplication::GuiClient;
 if (argc > 1)
 {
  appType = QApplication::Tty;
 }
 QApplication a(argc, argv, appType);
 if (a.type() != QApplication::Tty)
 {
  CSensoRescue *w = new CSensoRescue();
  w->show();
  if (hwndC)
  {
   ShowWindow(hwndC, SW_HIDE);
  }
 }


Two problems i'm facing is when i enter like :

D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe fdsa

i'm getting Invalid arg as output.


Second when i enter as
D:\Projects\TestProjects\TestQtConsole\Debug>TestQtConsole.exe

let me improve my Question:

When i start my .exe directly by double clicking , my console is also getting opened, that i don't want.
i just want GUI only.
Posted
Updated 29-Aug-11 19:14pm
v2
Comments
Timberbird 29-Aug-11 7:00am    
Do you mean that for GUI mode you want to open the console in Windows, enter [Your_app_full_exe_name], press enter, and then your APPLICATION should run in GUI mode, on start closing console window that YOU opened before?
glued-to-code 29-Aug-11 7:40am    
Hi,
Yes , if i open cmd window and type [App_exe_name] and enter it should open corresponding GUI and close the cmd window in which we entered exe name.

Also if i enter [App exe name + some cmd arguments], then it should passon the cmd arguments to my application and it shouldn't display invalid arg, as it is happening now.
Albert Holguin 29-Aug-11 10:05am    
Your app shouldn't close the command line if it didn't open it, that's not normal behavior.
Philippe Mori 29-Aug-11 20:58pm    
If you don't need it, then does start it... If you kill it, then you user might notice the console briefly particulary if it start your application on a very loaded system. It does not look professionnal.

As I can see from your comment, you want to close the console your application has been launched from in GUI mode. In this case the answer is simple: don't do it.

Seriously, this kind of behaviour would be unexpected and frustrating for users. Imagine someone's opening console, examining your disk with good old cds and dirs, running apps and system commands, launching you app just out of curiosity... BLAM, no console, no command history, app window instead. I'd be angered with that :). "No program should close MY console! It's mine, and it's me who controls it!" - that's what I'd say :). Running a .bat/.cmd file would also be difficult, and what about your application being launched with command line arguments via shortcut? I simply don't know whether it would have parent console.
So my recomendation is to leave the console alone unless you create this application for own/enterprise use only and it's an absolute must.
 
Share this answer
 
Comments
Albert Holguin 29-Aug-11 10:05am    
I agree...
Mohibur Rashid 31-Aug-11 2:39am    
I completely agree with you.
Put common code in a DLL and create distinct application for the GUI and the console.

Using separate applications would have a lot of advantages beginning by the fact that from the start you GUI application would be a GUI application without a console.... and your console application would be a console application without a GUI.

It would be easier to create shortcut for both of them.

You won't have to specifyu any command like options for the console application. If no argument are provided, simply dispaly usage information if the console application always need arguments.
 
Share this answer
 
suggest:
1. make your console program named TestQtConsole.com
2. make your GUI program named TestQtConsole.exe
 
Share this answer
 
I completely agree with Timberbird. Developer should not use anything that might annoy the user. specially destroying cmd window.

Besides I am not sure whether user will be able to figure out actual console from which the program has been launched.
Follow the link link
It might help you. There is another software provided on Codeproject,but i cannot find the link

Any make a better plan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900