tags:

views:

161

answers:

3

How do I force my application to wait until WinExec has completed?

A: 

WinExec is only there for compatibility with 16-bit Windows. The simplest way to execute a program and wait is to use system():

#include <stdlib.h>

int main() {
    system( "notepad" );
    // only gets to here when the notepad instance is closed
}
anon
WinExec is needed
SomeUser
@AndrewSmith Why would that be?
anon
WinExec is for GUI apps, and system() starts a console. Not an equivalent replacement. For a GUI app, system() won't wait for completion anyway.
Seva Alekseyev
Actually ShellExecute() is better
0A0D
All the programming I do these days is on Windows. None of it is GUI programming. The OP nowhere mentioned that either his initial app or the one he wants to wait for are GUI apps. And I said that system() was the simplest call - so it is, and also the most portable.
anon
@Neil: That's if Unix has notepad :)
0A0D
@Seva: Neil is right.. though he mentions winapi in the tags because he uses WinExec, system is the most portable, though old, function.
0A0D
The only major problem with the `std::system` call is that it doesn't support unicode.
Billy ONeal
There's `_wsystem()`, but then there goes the portability aspect.
bk1e
+10  A: 

WinExec is no longer recommended. You can use CreateProcess and WaitForSingleObject as shown in this example on Creating Processes.

RickNotFred
+2  A: 

Strictly speaking, you can't. WinExec will return as soon as possible (see the Remarks section in the WinExec MSDN page) and, unlike CreateProcess, it will not even return a handle you can call WaitForSingleObject on.

Danilo Piazzalunga