1

I'm a big fan of automation, so whenever I'm given the chance I like to try and automate tasks that I have to repeat. The following code usually allows me to click the mouse wherever it is on the screen (and in whatever application that may be):

Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long

Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long

Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down

Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down

Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up

Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down

Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

This usually works no problem. I've used it in a range of applications to great effect. However I'm now trying to do some mouse clicking (and key stroke sending) to an application that just seems to completely ignore the mouse and keyboard commands. Once the mouse has move into the area of the screen covered by the program it doesn't move, click etc, but it I move the mouse away manually it continue with the process as if it had been working correctly.

So is there any other way to control the mouse programmatically, that would simulate the mouse in a way that is indistinguishable from me moving it about by hand?

J...
  • 30,968
  • 6
  • 66
  • 143
FraserOfSmeg
  • 1,128
  • 2
  • 23
  • 41

1 Answers1

4

What version of windows are you using?

If you are using Vista+, is the target application running with elevated permissions?

If yes, is your application also running with elevated permissions?

If no, see : UIPI

Newer versions of Windows actively disallow an application with a lower privilege sending messages to an application with higher privilege. There are a few ways around this, either by making your application run with elevated permissions or by including an application manifest that sets uiAccess to true. In the latter case, the application must be authenticode signed and must be executed from a trusted location (ie: Program Files directory, etc.).

Otherwise, there is no way to simulate mouse activity at the low level without introducing a new hardware driver. Hardware drivers run in Ring-0 and to completely simulate real mouse activity (not just 'fake' messages) you would need to do it at this level.

J...
  • 30,968
  • 6
  • 66
  • 143
  • Thanks for your help. This looks like the answer. I'm trying to change the application manifest but then I install and run the application after changing the uiAccess to true the application fails to run. Most Likely because I can't install to a trusted location. Any advice on how to install to a trusted location? – FraserOfSmeg Mar 28 '14 at 15:38
  • @FraserOfSmeg if you add `uiAccess=true` to your manifest you must also authenticode sign the application. Paying for this service (ie: Verisign, etc) is expensive but you can self-sign as well, with the caveat that you must manually distribute the certificate (rather than it being available from a trusted third-party). See : http://msdn.microsoft.com/en-us/library/ms537364%28v=vs.85%29.aspx As for getting your application into `Program Files`, have a read about making an installer for your package : http://support.microsoft.com/kb/307353 (etc... search will help). – J... Mar 28 '14 at 16:52
  • @FraserOfSmeg ...obviously, simply running your program as administrator is the easiest solution. Perhaps not the best solution, but certainly the easiest. – J... Mar 28 '14 at 16:53
  • 1
    Just a quick update: I've got this fully working now. I just had to spend some time learning to use the installer so I could install it to program files. Thanks again! – FraserOfSmeg Apr 02 '14 at 11:19
  • Since you defiantly know what you're talking about with these things I'll link you to my new question that's related: http://stackoverflow.com/questions/22810290/a-different-method-to-send-key-presses-to-an-application-in-vb-net – FraserOfSmeg Apr 02 '14 at 11:41