views:

215

answers:

3

I've already saw many tutorials and articles about hooking, yet I don't quite understand it. Mainly because every single example uses different solution.

I know I will have to implement something that will keep the hook alive. Usually it's some kind of while cycle. Q1: If this loop was in some class with callbacks, will it prevent the thread from executing them?

I know it will take a while, but I would highly appreciate some well explained example of global keyboard hook. Or simply link me to some working example with binaries. (Trust me, I've been trying to google it last few hours).

Thank you

+1  A: 

There's a skeletal keyboard hook with downloadable code on my web site here (note the "download" button at the bottom of the page). The article discusses some things you need to understand like shared sections, and informs you of Kyle Marsh's excellent article on Win32 hooks (if some idiot at MSDN hasn't taken it down by now, for being insufficiently .netty).

The source code is in the example, it just needs a makefile/sln building for it (I don't know what compiler/version you'll be using). Code spookily similar to that has been in a shipping commercial product for a decade, so I know it works.

Note that integrity level issues can reduce the utility of hooking in Vista and W7.

Bob Moore
+1  A: 

your program will need to stay alive during the hook. Your program is the one that calls SetWindowsHookEx / UnSetWindowsHookEx (or whatever it's called). Between those calls, you will indeed have a message loop (this is probably the while loop you're talking about) just like any typical windows program.

But because your program is a different process than the ones you're hooking, your message loop will not cause other processes to hang. It's called multitasking :)

tenfour
+2  A: 

I know I will have to implement something that will keep the hook alive

No, that's not a concern. A global hook requires a DLL with the callback. That DLL gets injected in all running processes. It will stay loaded in the process until you call UnHookWindowsHookEx() or the process terminates, whichever comes first.

Do note that you can also hook the keyboard with WH_KEYBOARD_LL. That's not a global hook, Windows will switch context to your program and make the callback. It is much easier to use since you don't need an IPC mechanism with the injected DLL that the global hook requires. The low level hook stays active until you unhook, the thread that owns the message queue terminates or your process terminates, whichever comes first.

Hans Passant
I've tried to use WH_KEYBOARD_LL, yet it stops calling the callback after few seconds. Is there any trick to keep it alive besides the loop?
Mikulas Dite
That's automatic if your callback doesn't complete in a reasonable amount of time. Windows decides something is wrong with it and disables the hook. Something wrong with your callback code, can't guess what it might be.
Hans Passant
I guess Beep(0x500, 300); is way too slow even for debugging then...
Mikulas Dite