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 captures the message before it reaches the target window, that is, the hook function gets 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. The recently installed hook is placed at the beginning of the chain, and the first installed hook is put at the end, that is, the first added hook gets 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), where the first parameter is the hook type, the second parameter is the hook function address, and the third parameter is the module handle containing the hook function; the fourth parameter specifies the monitored thread. If a specific thread is specified, it is a dedicated hook. If it is specified as null, it is a global hook. Among them, the global hook function must be included in the DLL (Dynamic Link Library), and the thread-specific Hook can also be included in the executable file. After the hook function obtains control, if you want to continue transmitting the message after processing the message, it must call the API function callnexthookex in another SDK to transmit it. The hook function can also directly return true to discard the message and prevent the message from being transmitted.
In the following article, the concepts of writing callback functions are clear. Callback functions are self-written functions, but cannot be explicitly called, instead, the address of the function is referenced as another function parameter. This function is used to handle events that can call the self-defined callback function to complete some processing.
Most callback functions define their own names. Most of the function bodies are defined by the system. They have a structure, generally, a function on behalf of a callback function has a parameter that is followed by your callback name. It transmits some values to the callback function (the function body includes parameters that are predefined and cannot be written by itself, unless all functions are written by you), then the callback function accepts the value. After the corresponding operation, the value is returned to the original function body (its parent function), and finally the original function returns a value.
We often use callback functions in C ++ design to make some applications (such as timer Event Callback processing and recording operation progress using callback functions) very convenient and logical, how is its internal mechanism defined? 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 called (usually an API function), the address of one of its own functions (this function is a callback function) is passed as a parameter to that function. The callback function is called using the passed address when needed. You can use this opportunity to process messages or complete certain operations in the callback function. How to define a callback function is related to the specific API function used. The parameters and return values of the callback function are generally 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 understood that the callback function is like an interrupt processing function, which is automatically called when the system meets your set conditions. To do this, you need to do three things:
1. Statement;
2. Definition;
3. Set the trigger condition to convert your callback function name into an address as a parameter in your function for system calling.
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 a programmer cannot explicitly call. It is called by passing the callback function address to the caller. It is necessary to use the callback function. When we want to implement different content through a unified interface, it is very appropriate to use the callback function. 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. Now we can use the function back. Void show (void (* PTR) (); different callback functions are called based on different input parameters.
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 parameters, and the other callback function contains parameters.
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 a function parameter
{
(* PTR )();
}
Void caller2 (int n, INT (* PTR) () // pointer to the function as a function parameter. Here the first parameter serves 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 the usage of the callback function with parameters. To implement callback, you must first define the function pointer. The definition of the function pointer is described here. For example:
INT (* PTR) (); PTR is a function pointer. The brackets of (* PTR) cannot be omitted because the priority of parentheses is higher than the asterisk, 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];
}