Callback functions and their uses
1 Overview of callback functions
The callback function is a mechanism in which the caller passes some parameters to the object when initializing an object (the object here refers to objects in OOP, global functions, etc.), and at the same time passes a function that the caller can access. The address is passed to the object. This function is a notification agreement between the caller and the callee. When the agreed event occurs, the callee (usually including a working thread) will call the function according to the callback function address.
This way, the caller is in one thread and the callee is in another thread. Message: Message can also be regarded as some form of callback, because the message is also passed by the caller to the callee during initialization. A handle and a message number, the callee sends a message to the caller when the agreed event occurs.
In this way, the caller is in the main thread and the callee is in the main thread or worker thread. Delphi event model:
There are many visual components in Delphi's VCL that use event models, such as TForm's OnCreate event. The principle is: specify the event function at design time, and when the event is triggered at runtime, the event function specified at design time will be called. Mechanically, the Delphi event model is the same as callbacks. However, there are some differences in the specific forms. A pure callback function is in the form of a global function, while a Delphi event is in the form of an object method. That is, the following callback function type can be defined: type TCallBackFunc = PRocedure (pData: Pointer) of object;
2 Instructions for using callback functions
The callback function is mainly used in two situations. The first is that some Windows APIs require the callback function as its parameter address, and the other is that a function defined by the user in a specific occasion requires the callback function as its parameter. Address, for user-defined functions, is generally used when calling functions in dynamic link libraries. There are several main steps for using a callback function: 1. Defining a callback function type is no different from the definition of a general function process, but its definition must meet the function requirements of the callback function as needed. The only difference is that in The definition of a function or procedure must be followed by a declaration that it is a Windows standard call; for example: typeTHDFunction= function(I:integer;s:string):integer; stdcall; for the declaration of a procedure: type THDProcedure=procedure(s:string); stdcall;2. Then define a corresponding function or process based on this prototype. There are no requirements for the name of this function or process. The type of the function's parameters and the type of the return value must be completely consistent with the defined callback function type. For For procedures, it only needs to be the same parameter type. Example: Define a corresponding function and a corresponding process based on the prototype of the above function and process. Function prototype definition: Function HdFunExample(k:integer,sExam:string):integer; stdcall; Process definition: procedure HdProExample(sExam:string);stdcall; 3. Implement this callback function or procedure in the program; Function HdFunExample(k :integer,sExam:string):integer; stdcall;BeginEnd;procedure HdProExample(sExam:string);stdcall;beginend; 4. Calling process; the callback function is generally used as the entry address of a certain function in the system; according to the prototype of the calling function: Assume that there is the following calling function: function DyHdFunExample(HdFun:THDFunction;I: integer):boolean; Note: In the calling function, the callback function can be directly called by processing the function pointer (that is, the parameter in the calling function is the callback function type parameter, and it is directly operated), so that the callback function performs certain operations. That is, the function of the callback function is implemented in the calling function. Call: varI:integer;beginI:=DyHdFunExample(@HdFunExample,i);//…….End;
3 Examples
The sample program is under the directory H:/callback function examples/. The use of callback functions mainly lies in the original API functions of Windows, but the user-defined calling functions are generally located in dynamic link libraries. Generally, there is no need to use callback functions in the same project. (Personally think)...