WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DllCall to GetExitCodeProcess

 Keywords: GetExitCodeProcess DllCall tListProc Process Handle Exit Code Exitcode IntControl 64 Run RunWait RunShell OpenProcess MsgWaitForMultipleObjects WaitForSingleObject

GetExitCodeProcess Function

Retrieves the termination status of the specified process.

Sample 1:

;; Create a test process
Pid = RunShell( "notepad.exe", "", "", @NORMAL, @GETPROCID)

; Boilerplate
nExitCode = 2147483647 ; Whatever
hKernel32  = DllLoad("kernel32")
If hKernel32 == "" Then Goto cleanup
hUser32    = DllLoad("User32")
If hUser32 == "" Then Goto cleanup
hExitCode  = BinaryAlloc(4)
If !hExitCode Then Goto cleanup
hhWaitProcess = BinaryAlloc(4)
If !hhWaitProcess Then Goto cleanup

; Need both query(1024) and sync(1048576) access
hWaitProcess = DllCall(hKernel32,long:"OpenProcess",long:1024|1048576,long:0,long:Pid)
If !hWaitProcess Then Goto cleanup
BinaryPoke4(hhWaitProcess, 0, hWaitProcess)

; Suspend until process exits
nResult = 0
While 1

   ; Wake up on any input event in the targeted process  (511 = any input)
   nResult = DllCall(hUser32 ,long:"MsgWaitForMultipleObjects", long:1, lpbinary:hhWaitProcess, long:0, long:-1, long:511)
   If nResult == -1  Then Break

   ; Check for process signaled (0 return means process signaled)
   If DllCall(hKernel32 ,long:"WaitForSingleObject", long:hWaitProcess, long:0) == 0 Then Break

   Yield() ; Give up the rest of a CPU slice to be on the safe side
EndWhile

; Check for sync errors
If nResult == -1 Then Message( "Error", "System error = ":DllLastError())
Then Goto cleanup

; Get the process exit code
DllCall(hKernel32,long:"GetExitCodeProcess",long:hWaitProcess,lpbinary:hExitCode)
nExitCode = BinaryPeek4(hExitCode,0)
:cleanup ; Tidy things up.

If IsDefined(hWaitProcess) Then If hWaitProcess Then DllCall(hKernel32 ,long:"CloseHandle", long:hWaitProcess)
If IsDefined(hhWaitProcess) Then If hhWaitProcess Then BinaryFree(hhWaitProcess)
If IsDefined(hExitCode) Then If hExitCode  Then BinaryFree(hExitCode)
If IsDefined(hUser32) Then If hUser32!="" Then DllFree(hUser32)
If IsDefined(hKernel32) Then If hKernel32!="" Then DllFree(hKernel32)

Message("Process terminated", "With this exit code ":nExitCode)

Exit


Sample 2: Process extender: Test with CMD.EXE ( Exitcode = -1073741510) http://msdn.microsoft.com/en-us/library/ms683189.aspx

;Load the Process Extender DLL
AddExtender("wwprc44I.dll")

Run('cmd','')

; Display a list of processes
proclist=tListProc()
procid = ItemExtract( 2, AskItemlist("Choose CMD from the list",proclist,@TAB,@UNSORTED,@SINGLE) , '|' )

; Get Process Handle to pass to GetExitProcess
ErrorMode(@OFF)
hProcess=tOpenProc(procid,2)
ErrorMode(@CANCEL)
If hProcess==0
   Message("Unable to open process",tGetLastError())
   Exit
EndIf

Pause('Notice', 'Close CMD')

; Allocate buffer ( size of a DWORD ) to store the result of GetExitProcess
bufsize = 4
binbuf = BinaryAlloc( bufsize )
dllname =  StrCat(DirWindows(1), 'Kernel32.dll')

; DllCall the GetExitProcess API
; http://msdn.microsoft.com/en-us/library/ms683189.aspx
rslt = DllCall( dllname, long:'GetExitCodeProcess', long:hProcess,  lpbinary:binbuf)
; Capture error if necessary
If rslt == 0
      err = DllLastError()
      Message('GetExitCodeProcess Failed', err )
      Exit
EndIf

; Read result of GetExitProcess from binary buffer
exitcode = BinaryPeek4(binbuf, 0 )

; Free buffer
BinaryFree( binbuf )

; Display result
Pause('ExitCode', exitcode )
Exit


Sample 3: Process extender: Test with Notepad ( Exitcode = 0)

;Load the Process Extender DLL
AddExtender("wwprc44I.dll")

Run('notepad.exe','')

; Display a list of processes
proclist=tListProc()
procid = ItemExtract( 2, AskItemlist("Choose Notepad from the list",proclist,@TAB,@UNSORTED,@SINGLE) , '|' )

; Get Process Handle to pass to GetExitProcess
ErrorMode(@OFF)
hProcess=tOpenProc(procid,2)
ErrorMode(@CANCEL)
If hProcess==0
   Message("Unable to open process",tGetLastError())
   Exit
EndIf

Pause('Notice', 'Close notepad')

; Allocate buffer ( size of a DWORD ) to store the result of GetExitProcess
bufsize = 4
binbuf = BinaryAlloc( bufsize )
dllname =  StrCat(DirWindows(1), 'Kernel32.dll')

; DllCall the GetExitProcess API
; http://msdn.microsoft.com/en-us/library/ms683189.aspx
rslt = DllCall( dllname, long:'GetExitCodeProcess', long:hProcess,  lpbinary:binbuf)
; Capture error if necessary
If rslt == 0
      err = DllLastError()
      Message('GetExitCodeProcess Failed', err )
      Exit
EndIf

; Read result of GetExitProcess from binary buffer
exitcode = BinaryPeek4(binbuf, 0 )

; Free buffer
BinaryFree( binbuf )

; Display result
Pause('ExitCode', exitcode )
Exit

Article ID:   W17788
Filename:   DllCall to GetExitCodeProcess.txt
File Created: 2013:04:01:09:14:38
Last Updated: 2013:04:01:09:14:38