Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Exit code + StdoutRead/StderrRead

exit run stdoutread stderrread

  • Please log in to reply
3 replies to this topic

#1 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

Posted 02 September 2011 - 10:31 PM

Is there any way to retrieve the exit code from a process while also being able to use StdoutRead/StderrRead?







#2 AdamUL

AdamUL

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 303 posts

Posted 03 September 2011 - 05:28 AM

Have a look at this example that pulled from this thread and edited.

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 therks

therks

    Witty quote

  • Active Members
  • PipPipPipPipPipPip
  • 2,168 posts

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.

#4 castens

castens

    Seeker

  • Normal Members
  • 1 posts

Posted 06 February 2014 - 09:38 AM

This a simpler quick and dirty way

FileDelete("C:\temp\errorlevel.txt") $file = fileopen(@TempDir & "\CMD.bat",2) FileWrite($file,"start /wait c:\temp\test2.exe" & @CRLF) FileWrite($file,"echo %errorlevel% > C:\temp\errorlevel.txt" & @CRLF) FileClose($file) $PID = run(@TempDir & "\CMD.bat") While ProcessExists($PID) wend $file = fileopen("C:\temp\errorlevel.txt",0) msgbox(0,"",fileread($file)) fileclose($file)

Edited by castens, 06 February 2014 - 09:40 AM.






Also tagged with one or more of these keywords: exit, run, stdoutread, stderrread

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users