September 26th, 2018

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.

Feedback