I'm attempting to better my understanding of some of the process APIs, namely GetExitCodeProcess and TerminateProcess. I've found a number of code examples on the internet, but not quite the explanation I'm looking for.
I understand that this will give me the exit code of a process.
lResult = GetExitCodeProcess(hProcess, lExitCode)
And this will terminate the process
lResult = TerminateProcess(hProcess, lExitCode)
However, it seems that this last exit code parameter does not have to be the exit code retrieved in the first line. In fact, I've come across quite a few examples where 0
has been substituted for the exit code.
lResult = TerminateProcess(hProcess, 0)
I thought lExitCode was a specific value I had to provide to the TerminateProcess function, but apparently that's not the case - it seems that almost any number will work. Could someone give me an explanation on this?
From perspective of shelling various external applications from within Excel macro there are basically two scenarios:
The later case should be used carefully because
...The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess...
...A process cannot prevent itself from being terminated
Source: MSDN: Windows → Dev Center-Desktop → TerminateProcess function
Also note that the exit code parameter is treated as input in
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
and as output (ByRef) in
Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, ByRef lpExitCode As Long) As Long
Both functions follow same success/error calling convention
Return value
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError
(Visual Basic: Applications should call err.LastDllError instead of GetLastError)
Source: MSDN: TerminateProcess function, GetExitCodeProcess function, GetLastError function
Yes you can pass any number you want as the second parameter to TerminateProcess()
. There is no correct value to pass. Windows does not care what this value is. It's only purpose is for other applications to check what value was passed in using GetExitCodeProcess()
.
What the values passed to TerminateProcess()
are, are application defined. So they can mean whatever you want them to mean. For example you could define 0 as meaning that the application completed successfully, 1 if there was no Internet connection, 2 if the server was down, etc. If no one is listening for the exit code of your application it is irrelevant want you send, since no one will ever check it.
Also in your examples, for
lResult = GetExitCodeProcess(hProcess, lExitCode)
lExitCode
is an output parameter, it will be filled in with the processes exit code on return. However in
lResult = TerminateProcess(hProcess, lExitCode)
lExitCode
in an input parameter, so whatever value is stored in lExitCode
will be passed in. So for example these two statements are identical:
lExitCode = 0
lResult = TerminateProcess(hProcess, lExitCode)
and
lResult = TerminateProcess(hProcess, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With