Win32 Hook - Mimicking PreTranslateMessage

Started by
3 comments, last by moeron 17 years, 10 months ago
I'm writing a custom WH_GETMESSAGE hook so I can mimick PreTranslateMessage functionality ( my program is a MFC dll so none of those functions are called ). Everything is going pretty well except I'm not quite sure how I should go about signalling that a message should not be processed any further. MSDN is pretty clear that I should definately continue the CallNextHookEx chain. So currently I grab the return value from CallNextHookEx and then after that I monkey with the MSG struct setting the hwnd member to 0 and then returning out of the function. Now this works in that it will prevent the message from being handled again after I process it, and I don't mess up any other hooks because I make sure they are called before the change, but I still feel like I'm doing something very dirty here. Is there some more elegant way to handle something like this? This is essentially what I'm doing (btw the_hook is a global handle to my hook )

LRESULT CALLBACK GetProcMsg( int code, WPARAM wParam, LPARAM lParam ){
  if( code >= 0 ){
    MSG* pMSG = reinterpret_cast<MSG*>(lParam);
    if( pMSG ){
       if( DoCustomHandling(pMSG) ){
         // continue the chain of hooks even though I processed the msg
         LRESULT retval = CallNextHookEx( the_hook, code, wParam, lParam );
         // this will make sure the message isn't processed again
         pMSG->hwnd = 0;
         return(retval);
       }
    }
  }
  return CallNextHookEx( the_hook, code, wParam, lParam );
}
moe.ron
Advertisement
If you're hooking something inside the same process (or don't mind dll injection), use GetWindowLongPtr to retrieve the window procedure you want to 'filter' for, then SetWindowLongPtr the message procedure for that window to your own function that only calls the 'real' windows procedure (using CallWindowProc) when you want it to handle the message.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Ok, that sounds better than what I'm doing now. I'll give it a shot - thanks

[edit]
Oh wait, no thats not what I'm trying to do. I'm not trying to get a wndProc for just one window, I'm trying to hook into the message loop for the application I'm writing a plugin for. Or am I misunderstanding what GetWindowLongPtr does?
moe.ron
Quote:Original post by moeron
[...]Oh wait, no thats not what I'm trying to do. I'm not trying to get a wndProc for just one window, I'm trying to hook into the message loop for the application I'm writing a plugin for. Or am I misunderstanding what GetWindowLongPtr does?
Well, in that case, your method might be better, but instead of setting HWND to 0, there is a certain message that means 'do nothing' but I can't remeber the name/number and I couldn't find it in a quick search =-/ I've never used it, but I ran across it once and it said it was specifically for the kind of thing you're doing.

Edit: After a more thorough search (going through a list of all messages) I found it - set the message to WM_NULL to have it ignored.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Great, that feels less dirty than changing the hwnd. Thanks for your help
moe.ron

This topic is closed to new replies.

Advertisement