Your union declaration is incomplete. Because of that, Marshal.SizeOf(typeof(INPUT))
returns an incorrect value. You do at least need to define the MOUSEINPUT
part of the INPUT
union since that has the largest size. The pinvoke.net
declaration should suffice.
I expect that SendInput
would have told you of the problem had you checked the return value. I expect that SendInput
returns false
, and then a call to GetLastError
should yield ERROR_INVALID_PARAMETER
. Of course, you should not call GetLastError
directly. Use SetLastError = true
, and Marshal.GetLastWin32Error()
. You simply have to perform accurate error checking when you call Win32 API functions.
[DllImport("user32.dll", SetLastError=true)]
...
This part of the declaration is wrong at pinvoke.net where they omit SetLastError
.
You are using SendInput
in a clumsy way. The SendInput
function exists so that you can place multiple input messages in the queue without having them interleaved by others. The documentation makes this clear when it discusses SendInput
in relation to the now deprecated keybd_event
and mouse_event
functions. Call SendInput
once passing an array of length 2.