![]() |
| | #1 |
| Registered User Join Date: Dec 2003
Posts: 68
| Stop GUI Application returning when run I have a GUI windows application that has some command line ( non-UI options ). When the application is run from the command line, it detatches from the cmd process and still runs. For example, if I run c:\> App.exe /quiet /testparam:1 I get a cmd prompt back striaght away. Whereas if I have a console app it obviously runs until int main() returns. I was hoping that by not returning DialogProc() to WinMain when params are passed I'd essentially have a console app, but that doesn't seem to be the case. I'm currently doing Code: int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst = hInstance;
/*
* Tokenize lpCmdLine (command line options)
* and deal with various options.
*/
char *token;
token = strtok( lpCmdLine , " ");
bool bQuietMode = false;
/* Loop through tokenized lpCmdLine */
while ( token )
{
/* token is the commandline option */
if ( strcmp(token , "/csv") == 0 )
{
bQuietMode = true;
outputCSV();
}
token = strtok( NULL , " ");
}
/* If we've specified some cmd line options, don't show the GUI. */
if (!bQuietMode)
{
// The user interface is a modal dialog box
return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, DialogProc);
}
else
{
return ERROR_SUCCESS;
}
}
Thanks Dave |
| DaveHope is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| You still should be checking for events whether or not you spawned the dialog. See Tutorial: Understanding the Message Loop and/or http://en.wikipedia.org/wiki/Message...rosoft_Windows |
| zacs7 is offline | |
| | #3 | |
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| Unless outputCSV() spawns a thread I don't see why you'd want it to stall, as your program has finished doing it's work. That assumes what you posted is the entirety of your WinMain. Given those two conditions, you'll need to wait for it to the spawned thread to finish before exiting your app (WaitForSingleObject or whatever joining capability your thread api exposes). Quote:
| |
| adeyblue is offline | |
| | #4 | |
| Registered User Join Date: Dec 2003
Posts: 68
| Quote:
I'm not sure if I've managed to get my point across. Consider the following: From a cmd prompt, I run calc. The calc.exe process runs, and I can interact with it. Yet, the command prompt has returned. If calc.exe were a console app, the prompt would not be usable until main() from calc.exe returns. I want to achieve console app behavior with my Windows app, but only when command line params are passed. No other windows are created. | |
| DaveHope is offline | |
| | #5 |
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| In that case, no you can't really do that. The page linked to in that article describes 2 workarounds, but both require two binaries. cmd.exe waits for child processes to exit if they require a console, since it and the child use the same console. It doesn't wait for GUI processes since they implicitly have no need to use its' console. |
| adeyblue is offline | |
| | #6 | |
| Registered User Join Date: Dec 2003
Posts: 68
| Quote:
Thanks to the both of you for taking the time to respond to my question! | |
| DaveHope is offline | |
| | #7 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| > DialogBox implements it own message loop, adding another one would be redundant if no other windows are created. I know that... that's the point. And no it is not redundant if you ignore various messages, and only catch WM_QUIT. Remember you still have stdin, stdout and stderr all valid, just Windows won't "swawn" a console for you. Last edited by zacs7; 06-29-2009 at 05:41 PM. |
| zacs7 is offline | |
| | #8 | |||
| Hat seller extraordinaire Join Date: Apr 2008
Posts: 159
| Quote:
A message loop in this app given DaveHope's info (no other threads, no windows) will just stall it. Quote:
Quote:
| |||
| adeyblue is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating a Stand-Alone Application | QtApprentice | C++ Programming | 4 | 11-03-2003 01:33 PM |
| Simple C++ GUI programme does'nt run from dos | Shadowhunt | C++ Programming | 4 | 05-31-2003 05:30 AM |
| Run vbscript from C++ console application | dp_goose | C++ Programming | 3 | 05-15-2003 09:00 AM |
| EXE Closing all instances of Internet explorer - How can it run in Win 95/98 | joh | Windows Programming | 2 | 10-29-2002 04:56 PM |
| Win application not very portable | swed | Windows Programming | 5 | 10-01-2001 11:17 AM |