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());


 


 


 


Comments (1)

  1. Adam Nathan says:

    I just wanted to note that you can find these definitions at http://www.pinvoke.net. Unfortunately, the site contents are not very searchable by search engines.

    But whenever you feel like sharing this kind of info, feel free to contribute it at the site!