Problem with GetExitCodeProcess API

G

Gustav Bergquist

Hi,

I'm developing a PocketPC-application which needs to launch and get the
exitcode(Errorlevel) of a thirdparty application.
Now the problem is that I can't get the api-declaration right or maybe I'm
reading the results wrong... please help.

My guess is that lpExitCode in the API-declaration is bogus... because lp
stands for Long Pointer, shouldn't declaration then use IntPtr?
And second, how do I access the value which the pointer points to? I know
how to do this i C# but not VB.NET...

---API declarations---
<DllImport("coredll.dll")> Function CreateProcess(ByVal imageName As String,
ByVal cmdLine As String, ByVal lpProcessAttributes As IntPtr, ByVal
lpThreadAttributes As IntPtr, ByVal boolInheritHandles As Int32, ByVal
dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpCurrentDir
As IntPtr, ByVal si() As Byte, ByVal pi As ProcessInfo) As Int32
End Function
<DllImport("coredll.dll")> Function GetExitCodeProcess(ByVal hProcess As
Int32, ByVal lpExitCode As Int32) As Int32
End Function
<DllImport("coredll.dll")> Function TerminateProcess(ByVal hProcess As
Int32, ByVal iExitCode As Int32) As Boolean
End Function
<DllImport("coredll.dll")> Sub Sleep(ByVal iMilliseconds As Int32)
End Sub
<DllImport("coredll.dll")> Function WaitForSingleObject(ByVal hHandle As
Int32, ByVal dwMilliseconds As Int32) As Int32
End Function

---END API-declarations---

---CODE---
Dim STILL_ACTIVE As Int32
Dim si(128) As Byte
Dim pi As New ProcessInfo
Dim iRetVal As Int32
Dim iRet2Val As Int32
iRetVal = 0
STILL_ACTIVE = &H103
iRetVal = CreateProcess("thirdparty.exe", "", IntPtr.Zero, IntPtr.Zero, 0,
0, IntPtr.Zero, IntPtr.Zero, si, pi)
If iRetVal <> 0 Then
WaitForSingleObject(pi.hProcess, 10000)
iRetVal = GetExitCodeProcess(pi.hProcess, iRet2Val)
If iRetVal = 0 Then MsgBox("Error calling GetExitCodeProcess")
Do While iRet2Val = STILL_ACTIVE
Sleep(1000)
GetExitCodeProcess(pi.hProcess, iRet2Val)
Loop
MsgBox("Errorlevel: " & iRet2Val)
---END CODE---

Thanks for your input!
/Gustav Bergquist
 
P

Peter Foot [MVP]

As the parameter is a long pointer to a DWord (32bit integer) you should
pass by reference rather than by value, as the function places the value at
the address identified in the lp parameter - .NETCF will perform the
marshalling necessary for you.

<DllImport("coredll.dll")>
Function GetExitCodeProcess(ByVal hProcess As Int32, ByRef lpExitCode As
Int32) As Int32
End Function

You will need to create an Int32 variable

Dim ExitCode As Int32 = 0

pass this byref into the function, and after calling the function it will
contain the exit code.

Peter
 
G

Gustav Bergquist

Thanks Peter!

Now it works perfectly!
But now is seems as the thirdparty application doesn't set any errorlevels,
in contradiction to their documentation.... but hey that's life - right..

/Gustav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top