Filtering Windows messages in a hook filter function

I am trying to get messages for another application using a Windows hook. I have set up the WH_GETMESSAGE hook using SetWindowsHookEx. This is done through a DLL. In my GetMsgProc function (which should be called whenever the target application receives a message), I want to take an action based on the message type. However, I am having problems with this if statement.

LRESULT CALLBACK MessageHookProcedure(int code, WPARAM wParam, LPARAM lParam){
    if(((MSG*)lParam)->message == WM_COMMAND){
        MessageBox(NULL,L"The hook procedure was called",L"Test Window",MB_OK);
    }

    return CallNextHookEx(g_MessageHook,code,wParam,lParam);
}

      

For some reason, the MessageBox is never created. I know the application is receiving WM_COMMAND messages from Spy ++. If I take out the IF statement, the MessageBox is created over and over as it receives many messages.

+1


a source to share


2 answers


Are you sure you have connected the correct window or the correct message, respectively? In some cases WM_COMMAND

, WM_SYSCOMMAND

or is created instead of WM_MENUCOMMAND

.



Your code looks good, have you tried dumping incoming messages to the console as well?

+1


a source


LPARAM here is a pointer to CWPSTRUCT, which in turn contains the message parameter. The following should work.



LRESULT CALLBACK MessageHookProcedure(int code, WPARAM wParam, LPARAM lParam){
    if(((CWPSTRUCT*)lParam)->message == WM_COMMAND){
        MessageBox(NULL,L"The hook procedure was called",L"Test Window",MB_OK);
    }

    return CallNextHookEx(g_MessageHook,code,wParam,lParam);
}

      

0


a source







All Articles