Your Win32 API declaration is incorrect: 'long' maps to Int64 in the .NET Framework, which is almost always incorrect for Windows API calls.
Replacing long with int should work:
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
For future reference, you may want to check pinvoke.net whenever you're looking for the correct way to invoke API functions -- although it's not perfect, it would have shown the correct declaration for mouse_event.
(EDIT, 26 March 2012): And although the declaration I provided indeed works, replacing long
with uint
would be even better, as Win32's DWORD
is a 32-bit unsigned integer. In this case, you'll get away with using a signed integer instead (as neither the flags nor the other arguments will ever be large enough to cause sign overflow), but this is definitely not always the case. The pinvoke.net declaration is correct, as is the following:
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
Another answer to this question already provided this correct declaration, and the uint
issue was also pointed out in comments. I edited my own answer to make this more obvious; other SO participants should always feel free to edit incorrect posts as well, BTW.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…