Let's talk about the callback function [reprinted]

Source: Internet
Author: User
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.

Specify the callback function as a parameter of a function. This function will be called by that function, for example

...

Enumchildwindows (hwnd, enumchildproc, null );

...

Bool callback enumchildproc (hwnd hwndchild, lparam)

{

// Handle your code here

Getwindowtext (hwndchild, Buf, bufsize );

Return true;

}

The second parameter of enumchildwindows is a function pointer. This function will be called by enumchildwindows, but you do not know when to call it,

The enumchildproc function is defined in the Code. How to control the callback function behavior is yours. The code here gets the title of each subwindow.

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.