Concepts of callback and Hook Functions

Source: Internet
Author: User

A hook is actually a program segment for message processing. It is called by the system and mounted to the system. When a specific message is sent, the hook program first captures the message before it reaches the target form, that is, the hook function obtains control of the message first. In this case, the hook function can process (Change) the message, continue to transmit the message without processing it, and forcibly end the message transmission. For each type of Hook, the system maintains a hook chain. recently installed hooks are placed at the beginning of the chain, and the first installed hooks are placed at the end, that is, the later ones are added to gain control. To implement Win32 system hooks, you must call the SDK's API function setwindowshookex to install this hook function. The prototype of this function is hhook setwindowshookex (INT idhook, hookproc lpfn, hinstance hmod, DWORD dwthreadid); among them, the first vertex number is the hook type; the second vertex number is the hook function address; and the third vertex number is the module handle that includes the hook function; the fourth thread specifies the number of threads to be monitored. For example, if a specific thread is specified, that is, a dedicated hook for the thread; if it is null, that is, a global hook. Among them, global hook functions must be included in DLL (Dynamic Link Library), and thread-specific hooks can also be included in runable files. After processing the message, if you want to transfer the message, the hook function must call callnexthookex, an API function in another SDK. The hook function can also directly return true to discard the message and prevent the message from being transmitted.
In the following article, the concept of writing callback functions is clearer than that of callback. Callback functions are self-written functions, but they cannot be explicitly called, instead, the address of the function is referenced as the number of workers of another function. In this way, when some events occur, the user-defined callback function can be called to complete some processing.
Most callback functions are just User-Defined names. Most of the function bodies are defined by the system. They have a structure, generally, a callback function has a callback number followed by your callback name. it transfers some values to the callback function (the function contains the number of callback records, which is predefined, you cannot write it on your own, unless all functions are written by you), and then the callback function accepts the value. After the corresponding operation, the value is returned to the original function body (its parent function ), finally let the original function return a value
We often use the callback function in the C ++ design to make some applications (such as timer Event Callback processing and recording an operation progress using the callback function) very convenient and logical, how is its internal mechanism defined? How is it different from other functions (such as Hook Functions? Here is a brief introduction based on your usage experience.
When a callback function is used to call a function (generally an API function), the address of one of its own functions (this function is a callback function) is passed to the function as a callback. When necessary, you can call the callback function using the passed address. In this case, you can use this opportunity to process messages or complete some operations in the callback function. How to define a callback function is related to the API functions used in detail. The number of callback functions and return values are usually described in the help section. In C ++, callback (equivalent to far Pascal) must be added before the callback function. This mainly describes how the function is called.
As for the hook function, it is only a special case of the callback function. Traditionally, the callback function used with the setwindowshookex function is called a hook function. Some people call functions installed using virtualqueryex as hook functions, but such names are not very popular.
It can also be easier to understand: a callback function is like an interrupt processing function, and the system calls it when it meets your set conditions. To do this, you need to do three things:
1. Statement;
2. Definition;
3. Set the trigger condition, that is, convert your callback function name to the address as the number of calls in your function for the system to call.
Note the following when declaring and defining the callback function: the callback function is called by the system. Therefore, you can think of it as a Windows system. Do not treat it as a member function of your class.
A callback function is a function that cannot be explicitly called by a program. It is called by passing the callback function address to the caller. It is necessary to use callback functions. It is appropriate to use callback functions when we want to implement different content through a unified interface. For example, we have written different display functions for several different devices: void TVshow (), void computershow (), void notebookshow ()... and so on. We want to use a unified display function, and then we can use the function back. Void show (void (* PTR) (); different callback functions are called based on the number of passed-in records.
Different programming languages may have different syntaxes. The following is an example of a callback function in C language. One callback function does not contain the callback number, and the other callback function contains the callback number.
Example 1:
// Test. c
# Include <stdlib. h>
# Include <stdio. h>
Int test1 ()
{
Int I;
For (I = 0; I <30; I ++)
{
Printf ("the % d th charactor is: % C \ n", I, (char) ('A' + I % 26 ));

}
Return 0;
}
Int Test2 (INT num)
{
Int I;
For (I = 0; I <num; I ++)
{
Printf ("the % d th charactor is: % C \ n", I, (char) ('A' + I % 26 ));

}
Return 0;
}
Void caller1 (void (* PTR) () // pointer to the function as the number of functions
{
(* PTR )();
}
Void caller2 (int n, INT (* PTR) () // the pointer to the function serves as the number of functions. Here, the first number of functions serves as the pointer to the function,
{// Cannot be written as void caller2 (INT (* PTR) (int n). This definition syntax is incorrect.
(* PTR) (n );
Return;
}
Int main ()
{
Printf ("************************* \ n ");
Caller1 (test1); // call Test2 ();
Printf ("& *************************** \ n ");
Caller2 (30, Test2); // call Test2 (30 );
Return 0;
}
The callback function is called by passing the callback function address to the caller. However, you must note that the callback function is used with callback. To implement callback, you must first define the function pointer. The definition of the function pointer is mentioned here. For example:
INT (* PTR) (); PTR is a function pointer, and the brackets of (* PTR) cannot be omitted. Because the brackets have a higher priority than the asterisks, then it becomes a function declaration with an integer return type.

Callback function definition method:
The callback function definition method of C ++ has been used for countless times... Copy again here:
Example 1:
1 typedef int (* callback) (INT param1, char * param2 );
2
3 syntax
4 typedef return_code (* function_virtual_name) (parameters list );
5
6 Example
7 class cCall;
8 typedef int (* callback) (cCall * Call, int I );
9 class cCall {
10 public:
11 cCall (INT I = 0)
12: _ I (I)
13 {};
14 virtual ~ CCall (){}
15
16 int Becall (int I) {return call (I );}
17 virtual int call (int I) = 0;
18 private:
19 int _ I;
20 };
21
22 static int func (cCall * Call, int I)
23 {
24 return call-> Becall (I );
25}

Example 2:
Double Add1 (double I) {return I + 1 ;}
Double Add2 (double I) {return I + 2 ;}
Double add3 (double I) {return I + 3 ;}

Double (* adds [3]) (double) = {Add1, Add2, add3 };

Double (* get_add (int I) (double)
{
Return adds [I];
}

Concepts of callback and Hook Functions

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.