IonicWind Software

IWBasic => General Questions => Topic started by: LarryMc on December 29, 2010, 07:50:45 PM

Title: ShellExecuteExA return code
Post by: LarryMc on December 29, 2010, 07:50:45 PM
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
Title: Re: ShellExecuteExA return code
Post by: Kian on December 30, 2010, 01:18:07 AM
Larry,
Passing the process handle to the 'GetExitCodeProcess' function should do what you're after.
Kian
Title: Re: ShellExecuteExA return code
Post by: sapero on December 30, 2010, 01:45:39 AM
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
Title: Re: ShellExecuteExA return code
Post by: LarryMc on December 30, 2010, 09:14:20 AM
thanks to both

LarryMc