Global Keyboard Hook from windows service

The documentation for SetWindowsHookEx says:

or with all threads in the same desktop as the calling thread.

So you need to be associated with the same desktop (and there will be multiple desktops even without considering terminal services: the normal desktop, the secure desktop (used for UAC and login) and the screen saver).

Since services are not associated with an interactive session (and, from Windows V6, cannot be) you’ll need a process within the user’s session to do the hooking, and communicate with the backend service (eg. via a named pipe). And don’t forget to test with no-one logged in, and multiple users logged in.

Leave a Comment