How WH_CALLWNDPROC Works

You also can use this hook to watch for any messages sent to a window. For instance, the Spy++ utility uses this hook when it watches for sent messages. This hook is also useful if you need to perform an action when a specific message is sent to one or more windows. For example, one application could broadcast a user-defined message. This message would then be picked up and acted upon by every other application that was using this hook to watch for that particular message.

You can use this hook as a thread-specific hook, or you can place it in a dynamic link library (DLL) and use it as a system-wide hook. A thread-specific hook intercepts all messages within the thread in which it is installed. When a hook is installed as a system-wide hook, it has to be placed in a DLL. This DLL is injected into every process so that the hook can operate on all messages in the system. Chapter 3 provides a thorough discussion of this technique.

Messages sent to windows through the SendMessage function are sent directly to the window procedure, bypassing both the system and thread message queues. They do not go through the message queue, as a posted message does. As a result, these types of messages are processed before posted messages are. The following shows the order in which a message loop processes messages:

  1. Sent messages

  2. Posted messages

  3. Input messages from hardware and internal system events

  4. Sent messages (a second time)

  5. WM_PAINT messages

  6. WM_TIMER messages

As you ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.