Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PInvokeStackImbalance when Openprocess

Status
Not open for further replies.

sebastiannielsen

Programmer
Jun 1, 2003
25
0
0
SE
I always get the error PInvokeStackImbalance when im trying to call OpenProcess in my VB 2005 application
I have declared Openprocess as following:

Private Declare Function OpenProcess lib "kernel32.dll" (ByVal dwDesiredAccess as long, ByVal bInheritHandle as boolean, ByVal dwProcessId as long) as long

Im using it in this way:
(ExitPid is a "long" return value from the Shell function earlier in the program)

dim hh as long
hh = OpenProcess(1, false, ExitPid)
TerminateProcess(hh, 0)
CloseHandle(hh)
 
VB 2005, as in VB.NET?

API longs are 32 bit, .NET longs are 64 bit, this will create problems on the stack. Maybe you've got things mixed up somewhere....

Greetings,
Rick
 
Maybe your process statement should be

Code:
  hh = OpenProcess(PROCESS_ALL_ACCESS, 0, ExitPid)

I would think that the long API and the long .NET would be resolved by VB.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
im using the value for the constant PROCESS_TERMINATE which evaluates to "1".

But if that was the wrong, the function would only return a zero value (function failed), not crash application and return the error PInvokeStackImbalance.
 
VB.NET does not automatically resolve the long API and the long .NET. This not not so strange a behaviour, after all: isn't the declare you have to make just for that; telling .NET /VB6 what kind of parameters to expect/send?

So what happens: You declare your OpenProcess() as receiving parameters of type long. .NET will use 64 bits for those parameters and the windows library expects them to be 32 bits; hence your stack gets corrupted there, hence the error....

Change your declares from long to integer/b] and see if this works.

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top