October 30, 2025, 05:53:14 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


ShellExecuteExA return code

Started by LarryMc, December 29, 2010, 07:50:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Kian

Larry,
Passing the process handle to the 'GetExitCodeProcess' function should do what you're after.
Kian

sapero

December 30, 2010, 01:45:39 AM #2 Last Edit: December 30, 2010, 01:50:02 AM by sapero
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

LarryMc

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library