Sign in to follow this  
moeron

Win32 Hook - Mimicking PreTranslateMessage

Recommended Posts

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 );
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this