Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

windows - How to correctly use SetWindowsHookEx & CallNextHookEx

I can correctly setup up a windows hook, but I get confused by the line in MSDN that says "Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.".

I want to be a good programming citizen and call the next hook. But, my hook procedure looks like this:

LRESULT CALLBACK CBTProc(int code, WPARAM wp, LPARAM lp)
{
    if (code != HCBT_CREATEWND)
        {
                // What do I do here? It's not the event I requested so how do I pass it on?
        return 0;
    }

    // It's the code we want (create window)
        CallNextHookEx(...);
        ...
}

So, what happens in the hook procedure if the code isn't the one I'm interested in? How do I call the next hook?

Edit: The main problem is that a HHOOK is returned from the SetWindowsHookEx, and that needs to be passed to the CallNextHookEx function.

Update: It seems the hook parameter is ignored on the NT platforms:
http://msdn.microsoft.com/en-us/library/ms644974.aspx
http://www.klenotic.com/pl/null_hhook/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...