INFO: ProcessMessageFilter gets called only for messages posted (166212)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.52
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 2.1
    • Microsoft Visual C++, 32-bit Editions 2.2
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 4.0a
    • Microsoft Visual C++, 32-bit Editions 4.1
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
    • Microsoft Visual C++, 32-bit Professional Edition 4.2
    • Microsoft Visual C++, 32-bit Enterprise Edition 4.2b
    • Microsoft Visual C++, 32-bit Professional Edition 4.2b
    • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
    • Microsoft Visual C++, 32-bit Professional Edition 5.0
    • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
    • Microsoft Visual C++, 32-bit Professional Edition 6.0
    • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q166212

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.

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.

REFERENCES

Additional information on SetWindowsHookEx, MessageProc, WH_MSGFILTER can be found in the Windows API documentation.

Modification Type:MajorLast Reviewed:12/2/2003
Keywords:kbcode kbHook kbinfo KB166212