2

I am trying to emulate "hardware" mouse clicks as it appears that some software blocks input from PostMessage for instance. I know there's SendInput, but that's not an option as I need this to be compatible in background windows as well. The solution seems to be a low-level mouse hook but I've searched around and couldn't find anything other than just the loggers, no manipulation of moving mouse, clicking etc. I'd like this to happen without having to write some sort of C++/C wrapper to use as a fake mouse driver.

http://support.microsoft.com/kb/318804, I found this but it doesn't seem to be of any further help.

Any help appreciated :)

miceiken
  • 21
  • 1
  • 5
  • Low level mouse hook does not emulate new mouse events, it just allows to handle existing events. – Alex F Nov 14 '10 at 13:47
  • 1
    Sorry, I don't know about any way to have the mouse driver send a genuine click to the system without the user actually pressing the button, and I guess that makes sense, from a security standpoint. You might want to, I don't know, train a cat to click that button... – Frédéric Hamidi Nov 14 '10 at 13:48

4 Answers4

3

Not sure what 'some software' might be, but sure, UAC stops you from poking messages into the windows of elevated programs. It is called UIPI, User Interface Privilege Isolation.

In general, faking input with PostMessage doesn't work well at all. It is especially a problem for keyboard input but mouse input has trouble too. There is no good way to alter the keyboard state for another process. That matters when the program checks the state of the Shift, Ctrl and Alt keys when it processes the input message. Many do.

The only real solution is to emulate input with SendInput(). Now you got a focus problem to solve.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Games often don't use the message loop to get mouse and keyboard info. PostMessage() indeed won't work for them, SendInput should. – Hans Passant Nov 14 '10 at 19:23
1

mouse_event or SendInput used to inject mouse input. But just like a real mouse it's global input and can't work on hidden windows.

A low-level-mouse-hook is global too, but it is used to intercept and manipulate mouse-input, not to inject input.

When targeting a specific window you'll need to use SendMessage, but as you noted it doesn't work for everything.

You can also use dll hooking(for example an IAT hook) to intercept calls to APIs which return the gobal cursor position or the state of the mousebuttons. But for that you need to inject a dll into the target application, and that dll shouldn't use .net.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • How would I go about making an IAT hook? I already made a bootstrapper and injected a managed assembly so I guess I would be able to implement that. – miceiken Nov 14 '10 at 18:29
  • Using IAT hooking is a last resort. Check if you really can't get it to work with sending messages. Messages should work in most programs(typically only games won't work). And don't use managed code. For one you'll pull in the whole .net runtime with it's overhead. And since you can't load several .net versions in parallel(at least true for .net 1 to 3, might work with 4) it can lead to problems too. http://blogs.msdn.com/b/oldnewthing/archive/2006/12/18/1317290.aspx – CodesInChaos Nov 14 '10 at 18:35
  • To write an IAT hook you need to inject a dll, then inspect the import address table of the target module and replace the pointer to the functions you want to hook with a pointer to your replacement function. I can provide Delphi code if you really want to go this way. – CodesInChaos Nov 14 '10 at 18:37
  • I'll give it a shot, I also found this picture, http://tinyurl.com/34japoo - that is doing what I want, I'm just wondering if you could tell what method it's doing it by. Also, I'd appreciate the code, thanks :) – miceiken Nov 14 '10 at 18:39
  • But I wouldn't risk this stuff with a game that has a decent antihack(for example WoW and SC2 with Warden) – CodesInChaos Nov 14 '10 at 18:47
  • I'm not afraid of that, as far as the research I've read says Warden is pretty harmless, especially to non-commercial software that stays private. – miceiken Nov 14 '10 at 19:31
1

When I have to simulate mouse input I first try with SendMessage but sometimes some control or the application could eat the message. In that situations I use spy++ to intercept messages of the window that holds the control, I do exactly what I want to simulate and then, I just use:

GetWindowLong(hwnd, GWL_WNDPROC);

to get window proc and then call the wnd proc(process) directly with:

CallWindowProc(WNDPROC lpPrevWndFunc, HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

Sending exactly those messages that I saw using Spy++. That always work because the window proc is called immediately instead of queued in the message loop.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Paul Exchange
  • 2,637
  • 3
  • 26
  • 33
0

Take a look at this library http://globalmousekeyhook.codeplex.com/. It is 100% managed c# code to install global mouse and keyboard hooks.

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • Thanks, but I was actually looking for the opposite. A way to "send" keys and mouseclicks as if it was the hardware. – miceiken Oct 21 '11 at 16:12