views:

1080

answers:

6

I'm having an interesting problem implementing a global keyboard hook.

I wrote a dll which is used to set the hook and then an application (Delphi) which loads the dll and processes the results of the hook. This was done this afternoon on my PC at work and after some testing I figured it was working 100%.

I've just tested the same app and dll here at home and I'm not getting any errors, but the application does not appear to be getting any data either.

Both machines are WinXP, although my work machine is SP2 and this one is SP3.

Has there been some change in the Win32 API which would cause this to malfunction, or could the problem be related to some A/V / Spyware / MS Update that has been released recently?

I'm hoping somebody here will know of an obvious reason that this may happen before I spend hours debugging.

Thanks!

A: 

Do you have a debugger on your home computer? Do you receive any messages via the hook at all? Can it be that some other application is hooking, and don't pass the message on down the hook-chain?

BTW: I love virtual machines for this kind of testing. Keep a clean XP install. Install SP2, and test your application. Roll back to clean install again, and install SP3. Try your application again. This way you will know if its SP3, since there is nothing else to mess things up. I like to keep a set of snapshots around with different configurations.

Vegar
+2  A: 

This is trickier than it sounds. Here is information about writing global hooks in Delphi, along with sample source code.

Craig Stuntz
A: 

Yeah, I could. I haven't installed Delphi on this machine, but I think I might have to. I'm going for the low hanging fruit here. If there's an obvious answer, there's no need to go through all the trouble of debugging and hoping to find what might be the problem.

My first suspicion is that there's been a change in the API somewhere.

As I mentioned, this app works absolutely perfectly on my work machine.

+2  A: 

Actually some A/Vs don't like homemade hooks. I've got the same problem with my mouse hooker on some machines, and it doesn't depend on service pack version.

akalenuk
A: 

Which kind of hook are you using? I once used the WH_CBT-type and encountered problems when certain other applications where running. One case I could trace back to Trillian, which seems to do also some kind of hooking (and maybe screws up).

Apart from that I am currently working on an application that uses the WH_KEYBOARD-hook and this works on SP2 and SP3 equally well. The MSDN also doesn't mention any service-pack related changes.

What you can do to trace the bug on your home machine:

  • make sure to check all result values of all system api calls (and use GetLastError in case of error)
  • provide some kind of debug output in case of error (e.g. as message box or to a text file)
  • optional: log some status messages so you know whats going on internally
Heinrich Ulbricht
A: 

One alternative is to use a low level keyboardhook. (Just a different param to SetWindowsHookEx). The hook is processed in the message loop of the registering thread, and thus does not need to inject a dll everywhere. And for some odd reason VirusScanners/Firewalls interfere much less with it. They often silently block dllinjection or normal keyboardhooks. Also removes the need to share the hHook across processes if you want it to work in older windows versions. And if you abuse a keyboardhook to implement global hotkeys(Have seen that a lot) use RegisterHotkey/http://msdn.microsoft.com/en-us/library/ms646309.aspx) instead.