mouse_event上的TypeLoadException

时间:2013-12-21 01:42:42

标签: c# pinvoke

每次我运行我的应用程序,我都会得到这个

enter image description here

我认为它发生在以下代码中:

DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;
private const uint MOUSEEVENTF_RIGHTDOWN = 0x08;
private const uint MOUSEEVENTF_RIGHTUP = 0x10;

编辑:我通过在mouse_event varaible上添加另一个DllImport并将所有常量切换为:

来修复它
    private const int MOUSEEVENTF_MOVE = 0x0001;
    private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const int MOUSEEVENTF_LEFTUP = 0x0004;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const int MOUSEEVENTF_RIGHTUP = 0x0010;
    private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
    private const int MOUSEEVENTF_ABSOLUTE = 0x8000;

2 个答案:

答案 0 :(得分:1)

请参阅此文档:P/Invoke

尝试复制粘贴此代码(或删除dllImport参数):

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

答案 1 :(得分:0)

如果您使用差异dll将[DllImport(@"user32.dll")]放在每个函数上方,请使用此dll。如果您使用C#dll,为什么不为它添加参考?