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
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 to share