Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WndProc functions in Win32

Tags:

c++

winapi

This might be a dumb question, but can you register multiple WndProc functions in Win32? e.g. a framework catches some messages and my app is interested in other ones - how do I catch them without modifying the framework code?

like image 620
Mr. Boy Avatar asked Sep 24 '11 12:09

Mr. Boy


2 Answers

If I understand your intention correctly, you can do that by setting a hook. Assuming you know the thread whose message loop you'd like to hook, you can do something along these lines (unchecked):

SetWindowsHookEx(WH_CALLWNDPROC, yourHOOKPROC, NULL, theThreadId);
like image 130
eran Avatar answered Nov 03 '22 00:11

eran


You can chain multiple message handling functions by using the function CallWindowProc instead of DefWindowProc.

Here is an example:

pfOriginalProc = SetWindowLong( hwnd, GWL_WNDPROC, (long) wndproc1 );    // First WNDPROC

pfOriginalProc2 = SetWindowLong( hwnd, GWL_WNDPROC, (long) wndproc2);   // Second WNDPROC, WILL EXECUTE FIRST!!


LRESULT wndproc1( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch ( uMsg )
    {
      ...
      default:
         return CallWindowProc( pfOriginalProc, hwnd, uMsg, wParam, lParam );
    }


}


LRESULT wndproc2( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{

    switch ( uMsg )
    {
        ...
        default:
            return CallWindowProc( pfOriginalProc2, hwnd, uMsg, wParam, lParam );
    }
}
like image 28
AnderG Avatar answered Nov 03 '22 01:11

AnderG