Is there any way to retrieve the exit code from a process while also being able to use StdoutRead/StderrRead?
Exit code + StdoutRead/StderrRead
Started by
therks
, Sep 02 2011 10:31 PM
exit run stdoutread stderrread
2 replies to this topic
#2
Posted 03 September 2011 - 05:28 AM
Have a look at this example that pulled from this thread and edited.
Example.au3
ProcessExitCode.au3
Adam
Example.au3
Plain Text
#include <Constants.au3> #include "ProcessExitCode.au3" $iPid = Run(@ComSpec & ' /c dir /g', @WindowsDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD) $hHandle = _ProcessGetHandle($iPid) ShowStdOutErr($hHandle) $iExitCode = _ProcessGetExitCode($iPid) _ProcessCloseHandle($hHandle) MsgBox(0, "Program returned with exit code:", $iExitCode) $iPid = Run(@ComSpec & ' /c dir', @WindowsDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD) $hHandle = _ProcessGetHandle($iPid) ShowStdOutErr($hHandle) $iExitCode = _ProcessGetExitCode($hHandle) _ProcessCloseHandle($hHandle) MsgBox(0, "Program returned with exit code:", $iExitCode) Func ShowStdOutErr($l_Handle, $ShowConsole = 1) Local $Line, $tot_out, $err1 = 0, $err2 = 0 Do Sleep(10) $Line = StdoutRead($l_Handle) $err1 = @error $tot_out &= $Line If $ShowConsole Then ConsoleWrite($Line) $Line = StderrRead($l_Handle) $err2 = @error $tot_out &= $Line If $ShowConsole Then ConsoleWrite($Line) Until $err1 And $err2 Return $tot_out EndFunc ;==>ShowStdOutErr
ProcessExitCode.au3
Plain Text
#include-once ; #FUNCTION# ==================================================================================================================== ; Name...........: _ProcessGetHandle() ; Description ...: Returns a handle from use of Run(). ; Syntax.........: _ProcessGetHandle($iPID) ; Parameters ....: $iPID - ProcessID returned from a Run() execution ; Return values .: On Success - Returns Process handle while Run() is executing (use above directly after Run() line with only PID parameter) ; On Failure - 0 ; Author ........: MHz (Thanks to DaveF for posting these DllCalls in Support Forum) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _ProcessGetHandle($iPID) ;Return handle of given PID Local Const $PROCESS_QUERY_INFORMATION = 0x0400 Local $avRET = DllCall("kernel32.dll", "ptr", "OpenProcess", "int", $PROCESS_QUERY_INFORMATION, "int", 0, "int", $iPID) If @error Then Return SetError(1, 0, 0) Else Return $avRET[0] EndIf EndFunc ;==>_ProcessGetHandle ; #FUNCTION# ==================================================================================================================== ; Name...........: _ProcessCloseHandle() ; Description ...: Closes a handle from use of Run(). ; Syntax.........: _ProcessCloseHandle($hProc) ; Parameters ....: $hProc - Process handle ; Return values .: On Success - Closes Process handle after a Run() has executed.; ; On Failure - 0 ; Author ........: MHz (Thanks to DaveF for posting these DllCalls in Support Forum), PsaltyDS ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _ProcessCloseHandle($hProc) ;Close process handle Local $avRET = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hProc) If @error Then Return SetError(1, 0, 0) Else Return 1 EndIf EndFunc ;==>_ProcessCloseHandle ; #FUNCTION# ==================================================================================================================== ; Name...........: _ProcessGetExitCode() ; Description ...: Returns a handle/exitcode from use of Run(). ; Syntax.........: _ProcessGetExitCode($hProc) ; Parameters ....: $hProc - Process handle ; Return values .: On Success - Returns Process Exitcode when Process does not exist. ; On Failure - 0 ; Author ........: MHz (Thanks to DaveF for posting these DllCalls in Support Forum), PsaltyDS ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _ProcessGetExitCode($hProc) ;Get process exit code from handle Local $t_ExitCode = DllStructCreate("int") Local $avRET = DllCall("kernel32.dll", "int", "GetExitCodeProcess", "ptr", $hProc, "ptr", DllStructGetPtr($t_ExitCode)) If @error Then Return SetError(1, 0, 0) Else Return DllStructGetData($t_ExitCode, 1) EndIf EndFunc ;==>_ProcessGetExitCode
Adam
#3
Posted 17 October 2011 - 09:12 AM
Thanks, that works perfectly. And I can use _WinAPI_OpenProcess() and _WinAPI_CloseHandle() from the WinAPI.au3 include. Sorry for the horrendous reply time, I don't get back here often.
Also tagged with one or more of these keywords: exit, run, stdoutread, stderrread
AutoIt v3 →
General Help and Support →
"Run" to launch java app seems to be architecture dependant (32/64 bit)Started by dleigh , 15 Jun 2013 |
|
|
||
AutoIt v3 →
General Help and Support →
Redirect exit code of the AutoIt windowStarted by FireFox , 29 Apr 2013 |
|
|
||
AutoIt v3 →
General Help and Support →
Run Program without admin rightsStarted by GajjarTejas , 23 Apr 2013 |
|
|
||
AutoIt v3 →
General Help and Support →
Running Matlab .m fileStarted by afzalw , 17 Apr 2013 |
|
|
||
AutoIt v3 →
General Help and Support →
Plink.exe and StdoutReadStarted by lavascript , 20 Feb 2013 |
|
|
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users




