tags:

views:

314

answers:

2

For tracking user activity, I am using a Windows Hook for the main application thread, and monitor (among others) WM_COMMAND messages.

I receive them as expected from dialog buttons, toolbar buttons, accelerators and popup menus, but I do NOT receive them from the main menu.

Strangely enough, Spy++ does show the main window receiving them. What could be wrong?

Installing the hook:

currentHook = SetWindowsHookEx(WH_CALLWNDPROC, 
                               HookProc, 0, GetCurrentThreadId());

HookProc, minimalistic:

LRESULT CALLBACK HookProc(int nCode, WPARAM wp, LPARAM lp)
{
   CWPSTRUCT cwp = *(CWPSTRUCT *)lp;
   if (cwp.message == WM_COMMAND)
   {
      ATLTRACE("[hook!] WM_COMMAND id=%d\n", LOWORD(cwp.wParam));
   }
   return CallNextHookEx(currentHook, nCode, wp, lp);
}

(The actual code is more complex, and needs to check for reentrancy etc., but I've remvoed it for this test)

Any ideas?

[Edit] the main window I test is an MFC application, but the instrumentation code does not use any MFC.

A: 

Did you try to track WM_MENUCOMMAND?

luc
requires the MNS_NOTIFYBYPOS set for the menu - but I want to avoid code/ressource changes in the project to instrument. Also, I'd have to figure out which WM_MENUCOMMAND messages are not followed by a WM__COMMAND message - sounds ugly
peterchen
+3  A: 

Menu commands are posted, not sent (yes, the documentation is rather unclear on this - but Spy++ tells the truth). And WH_CALLWNDPROC hooks only catch sent messages.

You should be able to use a WH_GETMESSAGE hook to intercept posted messages. You'll need both if you want to handle both forms of WM_COMMAND.

Shog9
that works! Thanks!
peterchen