Tutorial :Sending mouse events to another windows, C# under Win 7


Is there a way to send mouse events to another window in Window 7 ?
I used to do this :
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

But this does not seem to work anymore in Win7.
Any ideas ?



Not work how?

One thing that might be relevant (it's hard because you've given so little detail) is that non-elevated (under UAC) apps are not allowed to communicate with elevated ones. So if one app is elevated and one not, you would say it "doesn't seem to work". But you can't fix that by changing the API you use.


The most likely reason that this is not working for you is the fact that the P/Invoke signature that you are using is incorrect. You have specified the arguments as long which in .NET represents a 64bit integer. The Win32 API decleration has the arguments defined as DWORD, which represents 32 bit integers, this will result in a stack imbalance. Change your signature to the following and you should have better luck.

[DllImport("user32.dll")]  public static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);  

Also, you should consider Kate's point and this might also have a bearing on your results once the signature is fixed.


SendMessage in user32.dll: would do the trick.

You'll also probably need FindWindow and the WM constants.

pinvoke.net is good for this sort of stuff.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »