Filtering Windows Messages in a Hook Filter Function

别说谁变了你拦得住时间么 提交于 2019-12-11 12:26:56

问题


I am trying to retrieve messages for another application with a Windows hook. I have setup a WH_GETMESSAGE hook with SetWindowsHookEx. This is done via a DLL. In my GetMsgProc function (that should be called whenever the target application receives a message) I want to take action based on the type of message. However I am having trouble 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 a variety of messages.


回答1:


Are you sure that you're hooking the correct window or the correct message, respectively? Under some circumstances WM_SYSCOMMAND or WM_MENUCOMMAND is generated instead of WM_COMMAND.

Your code looks fine, have you also tried dumping the incoming messages into console?




回答2:


The LPARAM here is a pointer to CWPSTRUCT which in turn contains 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);
}


来源:https://stackoverflow.com/questions/875950/filtering-windows-messages-in-a-hook-filter-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!