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
2010-02-12 22:43:34
WinExec is needed
SomeUser
2010-02-12 22:43:55
@AndrewSmith Why would that be?
anon
2010-02-12 22:45:13
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
2010-02-12 22:55:05
Actually ShellExecute() is better
0A0D
2010-02-12 23:03:35
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
2010-02-12 23:07:20
@Neil: That's if Unix has notepad :)
0A0D
2010-02-13 00:44:01
@Seva: Neil is right.. though he mentions winapi in the tags because he uses WinExec, system is the most portable, though old, function.
0A0D
2010-02-13 00:45:59
The only major problem with the `std::system` call is that it doesn't support unicode.
Billy ONeal
2010-02-13 02:43:53
There's `_wsystem()`, but then there goes the portability aspect.
bk1e
2010-02-13 04:51:51
+10
A:
WinExec is no longer recommended. You can use CreateProcess
and WaitForSingleObject
as shown in this example on Creating Processes.
RickNotFred
2010-02-12 22:46:05
+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
2010-02-13 00:08:35