What does the thread parameter to Set­Windows­Hook­Ex actually mean?


The Set­Windows­Hook­Ex function has a dwThreadId parameter for which the documentation says

The identifier of the thread with which the hook procedure is to be associated.

What does it mean for a hook procedure to be "associated" with a thread?

Recall that when an event occurs on a thread, the window hook is called from the same thread that the event occurred on. For example, a WH_CALL­WND­PROC hook procedure is called when a window procedure is about to be called, and the call occurs on the thread that is about to call the window procedure.

Okay, there are some window hooks that are global in scope, like the low-level input hooks, but you can't install those per-thread anyway, so the issue is moot.

Anyway, if you have a window hook that can be installed per-thread, then it will be installed only for events on that thread. In the above example, it means that only window procedures on that thread will trigger the hook.

Skip to main content