Aaron Hammond
Aaron Hammond

Reputation: 191

C# -- PInvokeStackImbalance detected on a well documented function?

Here is my code for a ClickMouse() function:

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);


    private const long MOUSEEVENTF_LEFTDOWN = 0x02;
    private const long MOUSEEVENTF_LEFTUP = 0x04;
    private const long MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const long MOUSEEVENTF_RIGHTUP = 0x10;
    private void ClickMouse()
    {
        long X = Cursor.Position.X;
        long Y = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

    }

For some reason, when my program comes to this code, it throws this error message:

PInvokeStackImbalance was detected Message: A call to PInvoke function 'WindowsFormsApplication1!WindowsFormsApplication1.Form1::mouse_event' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Please help?

Upvotes: 9

Views: 18199

Answers (5)

Nadim Ahmad
Nadim Ahmad

Reputation: 47

To fix PInvokeStackImbalance error:

Press: CTRL + ALT + E

Under "Managed Debugging Assistants" uncheck PInvokeStackImbalance

Upvotes: 2

Behzad
Behzad

Reputation: 2200

I changed "Cdecl" to "StdCall" but it didn't work.

As Scott Weinstein mentioned we should use int type.

[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

[Flags]
public enum MouseEventFlags
{
    LEFTDOWN = 0x00000002,
    LEFTUP = 0x00000004,
    MIDDLEDOWN = 0x00000020,
    MIDDLEUP = 0x00000040,
    MOVE = 0x00000001,
    ABSOLUTE = 0x00008000,
    RIGHTDOWN = 0x00000008,
    RIGHTUP = 0x00000010
}

public static void LeftClick(int x, int y)
{
    Cursor.Position = new System.Drawing.Point(x, y);
    mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
    mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
}

Upvotes: 1

James Poulose
James Poulose

Reputation: 3833

I was using RealGetWindowClass using the following format.

[DllImport("User32", CallingConvention=CallingConvention.Cdecl)]
public static extern int RealGetWindowClass(IntPtr hwnd, StringBuilder pszType, int cchType);

and i was geting the 'PInvokeStackImbalance' exception/error. But when i changed the CallingConvention to

[DllImport("User32", CallingConvention=CallingConvention.StdCall)]

the error went away. I am using VS2010 on a 64 bit machine.

Upvotes: 3

Arseny
Arseny

Reputation: 7351

I found this declaration

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, 
    uint dx, 
    uint dy, 
    uint dwData, 
    IntPtr dwExtraInfo);

Upvotes: 3

Scott Weinstein
Scott Weinstein

Reputation: 19117

Looks like your DllImport declaration is wrong. In particular the use of Int64 (longs), instead of UInt32.

Here's some detail from the PInvoke reference:
http://www.pinvoke.net/default.aspx/user32.mouse_event

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, 
                               uint dwData,UIntPtr dwExtraInfo);

Upvotes: 13

Related Questions