Joakim Carlsson Joakim Carlsson - 1 day ago 4
Vb.net Question

VB GetAsyncKeyState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature.

I'm working on a small program but when running my program in visual studio I get the following error:


GetAsyncKeyState' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature.


But when I build it to an .exe it runs just fine. But that makes it a bit hard to debug the program.

This is the code snipper that throws the error:

If InGame And Not GetAsyncKeyState(Keys.Tab) Then
If Settings.SkinChangera Then SkinChanger.Skinchanger()
End If


and this is GetAsyncKeyState()

Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Answer

I advice you to never ever look for, or use, Declare Lib statements you got from the internet. Most of them out there are for VB6 and are therefore almost always not fully compatible with VB.NET.

Instead, stick to the solutions that use the DllImport attribute. The site pinvoke.net is a great place to look for P/Invoke declarations. If you can't find a VB.NET version of a P/Invoke declaration, take the C# version and run it through a converter such as Telerik.

Having that said, you are receiving the error because the parameter and the return value are not of the correct data types. The GetAsyncKeyState() function's parameter should be Integer and its return value should be Short.

Use the DllImport version of the function instead, with the correct data types, and it should work:

<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function

Note: The System.Windows.Forms.Keys enumeration is of type Integer.

Comments