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, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用 RealGetWindowClass 使用以下格式。
我收到“PInvokeStackImbalance”异常/错误。但是当我将 CallingConvention 更改为
错误时,错误就消失了。我在64位机器上使用VS2010。
I was using RealGetWindowClass using the following format.
and i was geting the 'PInvokeStackImbalance' exception/error. But when i changed the CallingConvention to
the error went away. I am using VS2010 on a 64 bit machine.
要修复 PInvokeStackImbalance 错误:
按:CTRL + ALT + E
在“托管调试助手”下取消选中 PInvokeStackImbalance
To fix PInvokeStackImbalance error:
Press: CTRL + ALT + E
Under "Managed Debugging Assistants" uncheck PInvokeStackImbalance
我将“Cdecl”更改为“StdCall”,但它不起作用。
正如 Scott Weinstein 提到的,我们应该使用 int 类型。
I changed "Cdecl" to "StdCall" but it didn't work.
As Scott Weinstein mentioned we should use int type.
看起来您的 DllImport 声明是错误的。特别是使用 Int64(长整型),而不是 UInt32。
以下是 PInvoke 参考中的一些详细信息:
http://www.pinvoke.net/default.aspx/user32.mouse_event
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
我找到了这个声明
I found this declaration