All functions designed by you but called by the Windows system, collectively referred to as the callback function. Some API functions require callback as one of your parameters. Such as settimer, linedda, and enumobjects.
Callback functions are defined by developers according to a certain prototype (each callback function must follow this principle)
For example:
----------------------------------------
Bool callback dialogproc (
Hwnd hwnddlg, // handle of dialog box
Uint umsg, // message
Wparam, // first Message Parameter
Lparam // second Message Parameter
);
----------------------------------------
Note:
The callback function must have the keyword callback;
The callback function must be a global or static function and cannot be defined as a member function of a specific class.
2The callback function is not directly called and executed by the developer (only the system interface API function is used as the starting point)
3The callback function is usually passed to the system API as a parameter, which calls
4The callback function may be called once by the system API or cyclically multiple times.
Call the callback function to demonstrate enumobjects and find that the GDI obect of a device context conforms to our form.
Suppose we have a cmycalss:
Class cmyclass {
PRIVATE:
Int ncount;
Int callback _ Export
Enumobjectsproc (lpstr lplogobject, lpstr lpdata );
Public:
Void enumit (CDC & DC );
}
Void cmyclass: enumit (CDC & DC)
{
// Callback function
DC. enumobjects (obj_brush, enumobjectsproc, null );
}
The actual code of the C ++ compiler for cmyclass: enumit is equivalent:
Void cmyclass: enumit (CDC & DC)
{
// Callback function
CDC: enumobjects (obj_brush, enumobjectsproc, null, (CDC *) & DC );
}
The last parameter you see is actually the this pointer, and the class member function can catch the correct object information by relying on this pointer. while ncount = 0; actually this-> ncount = 0; based on the same principle, the enumobjectproc in the above example is a member function, the C ++ compiler will also prepare a hidden parameter for it. when the problem occurs, the callback function is used for Windows calls. Windows does not call this function through any object, so it does not need to pass this pointer to the callback function, it also causes a random parameter in the stack to become the this pointer, and the result is of course a program crash.
Therefore, to use a function as the callback function, you must tell the C ++ compiler not to put the this pointer as the last parameter of the function. The two methods can do this,
1. Do not use a class member function (that is, use a global function) as the callback function.
2. Use the static member function, that is, add the static modifier before the function.
The first approach is equivalent to using the callback function in the C language, and the second approach is similar to the OO spirit. furthermore, the static function feature in C ++ is that even if the object has not been generated, the static member already exists (both functions and parameters ). in other words, before an object is generated, you can call the static function of the class or use the static variable of the class. That is to say, anything declared as static (regardless of the function or variable) they are not combined with objects. They are part of a class and do not belong to objects.
Q Liu huyi:
Programming tools: C ++ builder 3.0
Operating System: Win98
I want to use the callback function in C ++. What is its internal mechanism and how to define it. How do I use the callback function when I use the dialogbox function? What is the difference between it and the hook function? Thank you for your advice !!! Please !!!
A:
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 is usually required before the callback function, which mainly describes the call method of the function. The dialogbox callback function is actually a window process used to process all messages. It is defined:
Bool callback dialogproc (
Hwnd hwnddlg, // handle of dialog box
Uint umsg, // message
Wparam, // first Message Parameter
Lparam // second Message Parameter
);
The Win32 API is described in detail. Generally, C ++ builder or MFC is not experienced in SDK programming. You are advised to read some SDK programming books. Otherwise, it is difficult to understand how to use the window process.
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.
Frank's opinion:
Although I have a rough understanding of the callback function, I think it is easier to understand: the callback function is equivalent to an interrupt processing function, which is automatically called by the system when it meets your set conditions. To do this, you need to do three things: 1, Declaration; 2, definition; 3, set the trigger condition, it is to convert your callback function name into an address as a parameter in your function for system calls.
Note: The callback function is called by the system, so it can be considered as a Windows system. Do not treat it as a member function of one of your classes.
Ping comments:
Frank said: the callback function belongs to the Windows system. I don't think the callback function belongs to the system. It should be said that the program will trigger this code by the system. This is the processing mechanism provided by windows, because the message is mastered by the system, and the system calls our program to process the message, which is more convenient. Otherwise, we have to try to read the message list again. (I don't know if I am right or not. I am not familiar with the system yet. Please advise me)