开发者

not able to install hooks for all threads in a process

I am hooking keyboard in application . Requirement is to hook keyboard in all threads in the process.

I used SetWindowsHookEx API

SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)::KeyboardHookProc, hInst, 0);

The hook is created for all the threads in process. This works fine until c开发者_StackOverflowalling thread exists .As soon as calling thread terminates hook stop working.

Is there any way to hook on process rather than threads in process.


This fine print in the SDK docs for LowLevelKeyboardProc is crucial:

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

In other words, you must keep the thread alive and the thread must pump a message loop. The behavior you see now is entirely by design.


As Per MSDN , WH__KEYBOARD___LL is applied globally not application specific.

If you want to install hook for a process specific threads use WH___KEYBOARD.


Just install the hook in the main function. If you need it only when a specific thread runs, you can use a volatile flag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜