Your Win32 API declaration is incorrect: long cards for 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 can check pinvoke.net whenever you are looking for the right way to call API functions - although this is not ideal, the correct declaration for mouse_event would be shown.
(EDIT, March 26, 2012): And although the declaration I provided does work, replacing long
with uint
will be even better since Win32 DWORD
is a 32-bit unsigned integer. In this case, you will avoid using a signed integer (since neither flags nor other arguments will ever be large enough to cause a sign to overflow), but this is definitely not always the case. The pinvoke.net declaration is correct, as shown below:
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
Another answer to this question has already been given by this correct announcement, and the uint
problem has also been noted in the comments. I edited my own answer to make this more obvious; other SO members should also always freely edit incorrect messages, BTW.
source share