I have this block of code to execute another exe from within my app.
This code waits for the other program to finish before continuing.
ShellExecuteExA(info)
do
int retxd2 = WaitForSingleObject(info.hprocess,0)
until retxd2 <> WAITTIMEOUT
The exe returns an exit code, how do I determine what it was?
LarryMc
Larry,
Passing the process handle to the 'GetExitCodeProcess' function should do what you're after.
Kian
This example shows how to discharge an accumulator :) Looping with zero timeout in a wait function is very bad, you must have found this example code from the bad guys.
If you really want to wait for the process, and your thread has no opened windows, change your example to:
' part 1
if (!ShellExecuteExA(info))
' todo: failed, call GetLastError,FormatMessage and show the error
return FALSE
endif
if (!info.hProcess) ' for non-exe
' todo: In some cases, such as when execution is satisfied through a DDE conversation, no handle will be returned
return FALSE
endif
' part 2
WaitForSingleObject(info.hProcess, INFINITE)
' part 3
int exitcode
GetExitCodeProcess(info.hProcess, &exitcode)
CloseHandle(info.hProcess)
If the executed process can take more time, and the thread executing it has a window:
' part 2
int i = 1
while (i=1)
i = MsgWaitForMultipleObjectsEx(1, &info.hProcess, INFINITE, QS_ALLINPUT, 0)
' i=0: process exited
' i=1: got messages to dispatch
' i=WAIT_FAILED: invalid info.hProcess
' i=WAIT_TIMEOUT: not possible with dwMillisecond=INFINITE
' i=WAIT_IO_COMPLETION: requires MWMO_ALERTABLE bit in dwFlags
if (i = 1)
MSG msg
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
'if (!IsDialogMessage(d1.hWnd, &msg)) ' process TAB ?
TranslateMessage(&msg)
DispatchMessage(&msg)
'endif
endwhile
endif
endwhile
thanks to both
LarryMc