Calling SendInput() from C# using MOUSEINPUT Structure

 

I couldn't find any samples (of course after only searching breifly) that show how to call SendInput from C# using the MOUSEINPUT struct. After getting it to work I figured I'd post it so other folks can make use of it:

       [DllImport("User32.dll", SetLastError=true)]

       public static extern int SendInput(int nInputs, ref INPUT pInputs, int cbSize);

       public struct INPUT

       {

              public int type;

              public MOUSEINPUT mi;

       }

       public struct MOUSEINPUT

       {

              public int dx;

              public int dy;

              public int mouseData;

              public int dwFlags;

              public int time;

              public int dwExtraInfo;

       }

       // Call the API

       int resSendInput;

       resSendInput = SendInput(1, ref input, Marshal.SizeOf(input));

       if (resSendInput == 0 || Marshal.GetLastWin32Error() != 0)

              System.Diagnostics.Debug.WriteLine(Marshal.GetLastWin32Error());