How to correctly use SetWindowsHookEx & CallNextHookEx How to correctly use SetWindowsHookEx & CallNextHookEx windows windows

How to correctly use SetWindowsHookEx & CallNextHookEx


According to the docs, the proper thing to do is pass the arguments you received directly to CallNextHookEx, exactly as you received them. You should also call CallNextHookEx regardless of whether you decided to handle the hook message.

According to MSDN, the first parameter to CallNextHookEx is ignored on on NT/XP/2003, and for older Win95-based operating systems it should be the HHOOK you received when you registered your hook with SetWindowsHookEx. The docs don't specify a value for Windows 2000, but since it's part of the NT family, a reasonable guess is that it's ignored there as well.

Given all that, a good way to code the method for NT-family operating systems might be this:

LRESULT CALLBACK CBTProc( int code, WPARAM wp, LPARAM lp ){    if( code == HCBT_CREATEWND )        ProcessCreateWnd( wp, lp );    return CallNextHookEx( 0, code, wp, lp );}void ProcessCreateWnd( WPARAM wp, LPARAM lp ){    // my code here}

This makes sure that you always call the hook at the end of your processing, and makes it hard to accidentally add a return that will bypass CallNextHookEx.