Callback functions and the concept of hook functions

Source: Internet
Author: User

The hook function, just a special case of the callback function, is something of windows. Don't care about Windows just look at the following example.

A hook is actually a program segment that processes messages, and it is put into the system by a system call. Whenever a particular message is issued, the hook program captures the message before it reaches the destination window, i.e. the hook function gets control first. At this point the hook function can process (change) the message, or you can continue to pass the message without processing, and you can force the end of the message delivery.

For each type of hook the system maintains a chain of hooks, the most recently installed hooks are placed at the beginning of the chain, and the first installed hooks are placed at the end, that is, the first to gain control over the added. To implement WIN32 system hooks, you must call the API function SetWindowsHookEx in the SDK 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 type of the hook, the second parameter is the address of the hook function, and the third parameter is the module handle that contains the hook function; The fourth parameter specifies the monitored thread.

If you specify a thread that is defined, it is a thread-specific hook, or a global hook if NULL is specified. Where the global hook function must be included in the DLL (dynamic-link library), the thread-specific hooks can also be included in the executable file. The hook function that gets control after processing the message, if you want the message to continue to pass, it must call the API function CallNextHookEx in the other SDK to pass it. The hook function can also discard the message by directly returning true, and block the delivery of the message.

The following article writes the concept of a callback function is still relatively clear, the callback function is a function of its own writing, but can not be explicitly called, but instead of the address of the function as a function parameter to reference, so as to handle when some events occur when you can call this own definition of the callback function, to complete some processing.

Callback functions are mostly just their own definition of a name, the function body is mostly system-defined, it has a structure, the general function of a generation of callback function has a parameter is to pick up your callback name, it put some values into the callback function (the function body including parameters is that it is predetermined, not to write their own, Unless all of the functions are written by you, then the callback function accepts the value, returns the value to the original function body (its father function), and finally returns a value to the original function.

We often design in C + + by using a callback function can make some applications (such as timer event callback processing, the callback function to record an operation progress, etc.) becomes very convenient and logical, then its intrinsic mechanism, how to define it? How does it differ from other functions, such as the hook function? Here is a simple introduction that combines your own experience.

Using a callback function is actually when you call a function (usually an API function) and pass the address of one of your functions (the function as a callback function) as an argument to that function. And that function, when needed, invokes the callback function with the address that is passed in, and you can take advantage of this opportunity to process the message in the callback function or do something about it. As for how to define a callback function, it is related to the API function that is used, and usually has the parameter and return value of the callback function in the Help. In C + +, it is generally required to add CALLBACK (equivalent to Far PASCAL) before the callback function, which mainly explains how the function is called.

As for the hook function, it is just a special case of the callback function. The callback function used in conjunction with the SetWindowsHookEx function is called a hook function. Some people use VirtualQueryEx to install functions called hook functions, but this is not very popular.

It can also be easier to understand: The callback function is like an interrupt handler, which is called automatically when the system meets the conditions you set. To do this, you need to do three things:

1. Declaration;

2. Definition;

3. Setting the trigger condition is to convert your callback function name into an address in your function as an argument for system invocation.

The Declaration and definition should be aware that the callback function is called by the system, so you can think of it as a WINDOWS system, not as a member function of one of your classes

A callback function is a function that a programmer cannot explicitly invoke, and a call is implemented by passing the address of the callback function to the caller. callback function use is necessary, when we want to implement different content through a unified interface, it is very appropriate to use the back-off function.

For example, we wrote different display functions for several different devices:

void TVshow (); void Computershow (); void Notebookshow () ... Wait a minute.

This is where we want to use a uniform display function, and we can then use the back function. void Show (void (*PTR) ()); Different callback functions are called when used according to the parameters passed in.

Different programming languages may have different syntaxes, and here is an example of a callback function in C, where one callback function takes no arguments and the other callback function has parameters.

Example 1:

test.c    #include  <stdlib.h>   #include  <stdio.h>    int  Test1 ()   {  int i;   for  (i=0; i<30; i++)   {  printf ("the %d& Nbsp;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 '  + &NBSP;I%26));  }   return 0;  }    Void caller1 (void  (*PTR))//pointer to function as function parameter   {  (*ptr) ();  }   void& nbsp Caller2 (int n, int  (*PTR))//pointer to function as a function parameter, here the first parameter is a pointer to the function of the service,    { //cannot be written void  caller2 (int  (*ptr) (Int n)), which defines a syntax error.   (*PTR) (n);   return;  }   Int main ()   {    printf ("************************\n");   Caller1 (Test1);  //equivalent to calling Test1 ();   printf ("&&&&&&************************\n");   Caller2 (30,&NBSP;TEST2),  //equivalent to call TEST2 (30);   return 0;  } 

The above is achieved by passing the address of the callback function to the caller, but it is important to note the use of the parameter callback function. To implement a callback, you must first define a function pointer. The definition of a function pointer a little bit here. Like what:

Int (*ptr) (); Here ptr is a function pointer, where the parentheses (*PTR) cannot be omitted, because the parentheses have precedence over the asterisk, which is a function declaration with the return type integral type.

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.