Process-wide hook using SetWindowsHookEx

Posted by mfya on Stack Overflow See other posts from Stack Overflow or by mfya
Published on 2010-06-10T19:34:09Z Indexed on 2010/06/11 2:02 UTC
Read the original article Hit count: 344

I need to inject a dll into one or more external processes, from which I also want to intercept keybord events. That's why using SetWindowsHookEx with WH_KEYBOARD looks like an easy way to achieve both things in a single step.

Now I really don't want to install a global hook when I'm only interested in a few selected processes, but Windows hooks seem to be either global or thread-only.

My question is now how I would properly go about setting up a process-wide hook.

I guess one way would be to set up the hook on the target process' main thread from my application, and then doing the same from inside my dll on DLL_PROCESS_ATTACH for all other running threads (plus on DLL_THREAD_ATTACH for threads started later). But is this really a good way? And more important, aren't there any simpler ways to setup process-wide hooks? My idea looks quite cumbersome und ugly, but I wasn't able to find any information about doing this anywhere.

© Stack Overflow or respective owner

Related posts about Windows

Related posts about winapi