C# – PInvokeStackImbalance在一个记录良好的函数中检测到?

前端之家收集整理的这篇文章主要介绍了C# – PInvokeStackImbalance在一个记录良好的函数中检测到?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的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);

    }

由于某些原因,当我的程序来到这个代码,它会抛出这个错误信息:

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.

请帮忙?

解决方法

看起来你的DllImport声明是错误的.特别是使用Int64(longs),而不是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);
原文链接:https://www.f2er.com/csharp/94532.html

猜你在找的C#相关文章