Use the hook function [4]-return values of the hook chain and callnexthookex

Source: Internet
Author: User
The first parameter of the setwindowshookex function indicates the hook type. There are 14 Options in total. We have already used two options:
Wh_keyboard, wh_mouse.

The system creates a table (14 Tables) for each type of hook, for example, an application.ProgramAfter the keyboard hook is started, our program also starts the keyboard hook. Similarly, the keyboard hook will enter the same table. this table (there may be more than one table, there may also be mouse hooks, etc.) is the legendary "hook chain ".

Assume that a hook chain contains three hooks (for example, hook a, hook B, and hook C), and the final "hook C" is executed first.
Is it advanced? I think it should be said: "first in, first out! Is there a difference? Yes! Because the advanced ones may not be determined.
The last hook is executed first. If the previous hooks (hook a and hook B) can be executed, you have to say that, this requires the execution of the "hook C.
If the "hook C" function contains the callnexthookex statement, then "hook a, hook B" may be able to wait for the corresponding
Unhookwindowshookex to take them away (I think of Zhao Benshan's sketch ...).

At this time, you may think: This is too good. I will not add callnexthookex in the future, just let my hook "go"; but what if your hook is advanced?
Therefore, for Windows, it is recommended that the hook function call callnexthookex and treat its return value as its own return value.

Callnexthookex simultaneously transmits parameters to the next hook in the hook chain (or perhaps the previous one) (for example, which key is pressed in the Keyboard Message). Is the parameter of a keyboard hook the same as that of a mouse hook? Of course they are different, so they are not in a chain. The types of hooks in the same chain must be the same.

Let's talk about the return value of the hook function:
Before that, we followed the Windows convention to return the callnexthookex return value.
If callnexthookex succeeds, it returns the returned value of the next hook, which is a serial set;
If callnexthookex fails, 0 is returned, and the hook chain is broken. Only the current hook is still executing the task.

The return values of different types of hook functions are different. For keyboard hooks, if a non-0 value is returned, the messages are wiped out after processing.
In other words:
If the hook function result: = 0 is given to the keyboard, it indicates that the message is intercepted by the hook and then "put;
If result: = 1 is returned to the hook function on the keyboard, the message is intercepted by the hook and processed before being killed.

In the following example, we simply do not use callnexthookex (I am a hook for the moment) to directly return the value!

This is the demo animation in the following example:

In the animation, I input the letter to memo in three states.
When the hook is not started, memo can be input normally;
When the hook function returns 0 and is hooked up, the hook function (Return key value) is executed first, and the letter A can be entered successfully;
When the hook function returns a non-0 value (for example, 1), after it is hooked up, it only executes the function of the hook function (Return key value). Poor memo does not know what happened.

But there is a new problem here: How does the hook function return the key value?
Note: When you used beep for testing, did you find that the voice is not only once? This is a truth.
If you press the key once, two messages will be sent: wm_keydown and wm_keyup. If we do not specify which one to intercept, all messages will be intercepted.
So how can we differentiate these two messages? The secret lies in lparam, the third parameter of the keyboard hook function. Wait for the next topic to study it again.

// Example Code : Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; memo1: tmemo; procedure invoke (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; {hook function declaration} function mykeyhook (ncode: integer; wparam: wparam; lparam: lparam): lresult; stdcall; var form1: tform1; implementation {$ R *. DFM} var HOOK: hhook; function mykeyhook (ncode: integer; wparam: wparam; lparam: lparam): lresult; begin form1.memo1. lines. add (inttostr (wparam); {parameter 2 is the key value} result: = 0; {Both test return 0 or not 0} end; {dispatch hook} procedure tform1.button1click (Sender: tobject); begin HOOK: = setwindowshookex (wh_keyboard, mykeyhook, hinstance, getcurrentthreadid); memo1.clear; text: = 'hook Start'; end; {reclaim hook} procedure tform1.button2click (Sender: tobject); begin unhookwindowshookex (Hook); text: = 'hook close'; end; {if you forget to reclaim the hook ...} procedure tform1.formdestroy (Sender: tobject); begin if hook0 then unhookwindowshookex (Hook); end.
 
   
 

TIPS: No. This time, when setwindowshookex is used, the second parameter (function address) does not use @ or ADDR. How can this problem be solved?
Because the function name itself is an address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.