×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PInvokeStackImbalance when Openprocess

PInvokeStackImbalance when Openprocess

PInvokeStackImbalance when Openprocess

(OP)
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)

RE: PInvokeStackImbalance when Openprocess

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

RE: PInvokeStackImbalance when Openprocess

(OP)
How i do to create a 32 bit long in .NET?

RE: PInvokeStackImbalance when Openprocess

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.

RE: PInvokeStackImbalance when Openprocess

(OP)
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.

RE: PInvokeStackImbalance when Openprocess

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 [b]integer/b] and see if this works.

Greetings,  
Rick

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close