Quick use of callback Functions

Source: Internet
Author: User
Tags response code

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.
 
VC callback function and usage

The callback function is an Event Response Program. Every message in windows can be understood as an event, and the event response code should be defined by the user. The user defines the event response code, but windows also needs to know the location of the Code (otherwise Windows does not know how to call it, which is useless ), the user needs to tell windows the pointer of the callback function. The most typical example is to assign the callback function pointer value to the lpfnwndproc component in the wndclass structure of the window class.
The parameter format of a callback function is defined by the caller of the callback function (generally in Windows), and the implementer of the callback function must follow this format. Windows programs are based on the event-driven model. Therefore, callback functions must be used.
For more information about callback functions, see SDK samples. The message ing mechanism in MFC has hidden the callback function of the window message response, which is also in line with the c ++ programming philosophy. The callback function is a global function after all, it cannot be implemented in the class, and the message ing mechanism aims to encapsulate the message response code in the window class (subclass of the cwnd class.

If you have time, check the message_map macro. Message ing is a callback function, but the usage of this callback function is different. A common callback function requires you to provide an address and upload it to a function for calling. However, the message ing function is defined by you and the message_map macro gets the address, and implement its call.

A callback function is a function that a programmer cannot explicitly call. It is called by passing the callback function address to the caller. To implement callback, you must first define the function pointer. Although the defined syntax is a bit incredible, if you are familiar with the general method of function declaration, you will find that the declaration of function pointers is very similar to that of function declaration.
Code:
# Include <stdio. h>
Void (* p) (); // P is a pointer to a function.
Void func1 ()
{
/* Do something */
Printf ("from func1 (), hello World! /N ");
}
Void caller (void (* ptrfunc1 )())
{
Ptrfunc1 ();/* call the function pointed to by PTR */
}
INT (* q) (int *); // Q is a pointer to a function.
Int func2 (int * t_ I)
{
/* Do something */
Printf ("from func2 () = % d, hello World! /N ", (* t_ I) ++ );
Return 1;
}
Void caller2 (INT (* ptrfunc2) (int *), int * I)
{
Ptrfunc2 (I);/* call the function pointed to by PTR */
}
Int main (INT argc, char * argv [])
{
Printf ("from main (), hello World! /N ");
Printf ("/N ");
// Call without Parameters
P = func1;/* Transfer Function address */
Caller (p);/* pass the function address to the caller */
// Call with Parameters
Int I = 0;
Int J;
For (j = 0; j <10; j ++)
{
Caller2 (func2, & I); // * pass the function address to the caller */
}

// Second call with Parameters
I = 0;
Q = func2;/* Transfer Function address */
For (j = 0; j <10; j ++)
{
Caller2 (Q, & I);/* pass the function address to the caller */
}
Printf ("/N ");
Printf ("from main (), hello World! /N ");
Getchar ();
Return 0;
}

Running result:

From Main (), hello World!
From func1 (), hello World!
From func2 () = 0, hello World!
From func2 () = 1, hello World!
From func2 () = 2, hello World!
From func2 () = 3, hello World!
From func2 () = 4, hello World!
From func2 () = 5, hello World!
From func2 () = 6, hello World!
From func2 () = 7, hello World!
From func2 () = 8, hello World!
From func2 () = 9, hello World!
From func2 () = 0, hello World!
From func2 () = 1, hello World!
From func2 () = 2, hello World!
From func2 () = 3, hello World!
From func2 () = 4, hello World!
From func2 () = 5, hello World!
From func2 () = 6, hello World!
From func2 () = 7, hello World!
From func2 () = 8, hello World!
From func2 () = 9, hello World!
From Main (), hello World!

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.