C#——在有据可查的函数上检测到 PInvokeStackImbalance?

发布于 2024-09-03 18:59:18 字数 951 浏览 1 评论 0原文

这是我的 ClickMouse() 函数的代码:

    [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);

    }

由于某种原因,当我的程序到达此代码时,它会抛出此错误消息:

检测到 PInvokeStackImbalance 消息:对 PInvoke 函数的调用 'WindowsFormsApplication1!WindowsFormsApplication1.Form1::mouse_event' 堆栈不平衡。这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。检查一下 PInvoke 签名的调用约定和参数匹配 目标非托管签名。

请帮忙?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

×眷恋的温暖 2024-09-10 18:59:19

我使用 RealGetWindowClass 使用以下格式。

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

我收到“PInvokeStackImbalance”异常/错误。但是当我将 CallingConvention 更改为

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

错误时,错误就消失了。我在64位机器上使用VS2010。

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.

忆依然 2024-09-10 18:59:19

要修复 PInvokeStackImbalance 错误:

按:CTRL + ALT + E

在“托管调试助手”下取消选中 PInvokeStackImbalance

To fix PInvokeStackImbalance error:

Press: CTRL + ALT + E

Under "Managed Debugging Assistants" uncheck PInvokeStackImbalance

故事↓在人 2024-09-10 18:59:19

我将“Cdecl”更改为“StdCall”,但它不起作用。

正如 Scott Weinstein 提到的,我们应该使用 int 类型。

[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);
}

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);
}
π浅易 2024-09-10 18:59:18

看起来您的 DllImport 声明是错误的。特别是使用 Int64(长整型),而不是 UInt32。

以下是 PInvoke 参考中的一些详细信息:
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);

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);
牵强ㄟ 2024-09-10 18:59:18

我找到了这个声明

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

I found this declaration

[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, 
    uint dx, 
    uint dy, 
    uint dwData, 
    IntPtr dwExtraInfo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文