Karim Kouznetsov
Karim Kouznetsov

Reputation: 525

SetWindowsHookEx for current thread returns null

I have an issue trying to get a keyboard hook for the current thread. Firstly, I needed to get keyboard input for all threads, so I used :

hHook = SetWindowsHookEx(WH_KEYBOARD_LL, mKeyboardProc, GetModuleHandle(NULL), 0);

This works fine, but when I try to set the dwThreadId parameter (the last one) to GetCurrentThreadId(), SetWindowsHookEx returns NULL.

What could be the problem ? Thanks.

Upvotes: 3

Views: 1981

Answers (1)

David Heffernan
David Heffernan

Reputation: 612964

WH_KEYBOARD_LL is a global hook, as stated in the documentation. That means that you must pass 0 for the thread ID parameter.

You could have diagnosed this yourself had you called GetLastError, as described by the documentation. This would have returned error code ERROR_GLOBAL_ONLY_HOOK, This hook procedure can only be set globally.

Upvotes: 5

Related Questions