Microsoft KB Archive/166212

= INFO: ProcessMessageFilter gets called only for messages posted =

Article ID: 166212

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 5.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Professional Edition</li></ul>

<ul> <li>Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q166212

<div class="summary_section">

SUMMARY
ProcessMessageFilter is only called for messages that are posted to the message queue and is not called for messages that are send directly to the window procedure. Therefore, this function cannot be used to monitor messages that are sent (by "Windows" or by the user) to a dialog box, message box, menu, or a scroll bar.

ProcessMessageFilter is called from _AfxMsgFilterHook, which is the MFC hook procedure for WH_MSGFILTER hook. This hook procedure will be called by the system after a message generated by an input event (in a dialog box, message box, menu, or scroll bar) is retrieved from the message queue. Since messages sent directly to the window procedure (by SendMessage) do not go through the message queue, this hook procedure is not called by Windows. This in turn blocks the call to ProcessMessageFilter.

<div class="moreinformation_section">

MORE INFORMATION
The WH_MSGFILTER hook is a task-specific hook that enables an application to monitor messages passed to a menu, scroll bar, message box, or dialog box created by the application that installed the hook procedure. In a typical MFC application the WH_MSGFILTER is set in the global function AfxWinInit, and in _AfxThreadEntry for multithreaded applications, with _AfxMsgFilterHook as the callback or the hook procedure.

The callback function for this hook is called after these messages are retrieved from the queue, just before dispatching them, performing special processing as appropriate. Since messages sent directly to the window procedure (by SendMessage) do not go through the message queue, the hook procedure (_AfxMsgFilterHook) is not called by Windows.

ProcessMessageFilter of CWinThread (in MFC 3.x and up) and CWinApp (in MFC 2.x) is called from the _AfxMsgFilterHook hook procedure. Since the hook procedure will not be called for messages sent directly to the window procedure, ProcessMessageFilter will not get called and none of the special code inside will execute.

In general, all the keyboard and mouse messages, along with the WM_PAINT and WM_TIMER messages, are posted to the message queue. ProcessMessageFilter will not be called for messages like WM_SETFOCUS, WM_KILLFOCUS, WM_SETCURSOR, WM_COMMAND, WM_CTLCOLOR, WM_ACTIVATE, etc., since these are sent directly to the window procedure.

To monitor messages that are sent to a menu, message box, dialog box or a scroll bar, set a WH_CALLWNDPROC hook. This hook is called whenever a message is sent directly to a window procedure. Note that there will be a tradeoff with performance since this hook procedure is called for every message that are sent through SendMessage.

<div class="references_section">