我在Visual C#中创建了一个表单应用程序,使用一个函数来生成鼠标点击,但是我得到了以下错误信息:
A call to PInvoke function '...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("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 int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
...
void GenerateMouseClick(int x, int y)
{
Cursor.Position = new Point((int)x, (int)y);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}
mouse_event
函数使用了Cdecl
调用约定。 - Masood Khaari