Tune function and its use
1 Overview of the callback function
The callback function is such a mechanism: the caller is initialized an object (here the object is a general referred to, including objects, global functions, etc.), passes some parameters to the object, and the function that the caller can access The address is passed to the object. This function is a notification agreement between the caller and the caller. When the agreed event occurs, the called is called by the callback function address when the agreed event occurs. This way, the caller is in a thread, the caller being in another thread.
news:
The message can also be seen as some form of callback because the message is also initialized by the caller to pass a handle and a message number to the caller, and send a message to the caller when the agreed event occurs. In this way, the caller is in the main thread, and the caller is in the main thread or the working thread.
Delphi Event Model: There are many visual components in the VCL of Delphi, are all using event models, such as TForm's oncreate events. The principle is: Specify event functions when designing, triggered at runtime events, will be called in design Event function.
In mechanisms, the Delphi event model is the same as the callback. However, some differences in the specific form, the pure callback function is in the form of a global function, and the Delphi event is in the form of an object method, that is, the following adjustment function type can be defined.
Type
Tcallbackfunc = procedure (PDATA: POINTER) OF Object;
2 instructions for callback function
The callback function is mainly used in two occasions. The first is that some Windows API requirements use the callback function as its parameter address, and the other is a function of a user to use a callback function as its parameters. Address, for functions of the user's definition, usually used when the function in the dynamic connection library is called.
There are several steps to use a callback function:
1. Define a callback function type, and there is no difference in the definition of the general function process, but its definition must meet the function requirements of the callback function as needed, the only difference is that it must be declared behind the Windows standard after the function or process definition. transfer;
example:
Type
THDFUNCTION = Function (I: Integer; s: string): integer; stdcall;
For the declaration of the process:
Type
THDPROCEDURE = procedure (s: string); stdcall;
2, then define a corresponding function or process according to this prototype, for this function or process, the name is not required, the type of the function thereof and the type of return value must be completely consistent with the type of callback function, for the process Say, just like the type of parameter is required.
Example: Defines a corresponding function and a corresponding process based on the original shape of the above functions and procedures.
Function prototype definition:
Function HDFunexample (K: Integer, Sexam: String): Integer; stdcall;
Process definition:
Procedure hdproexample (sexam: string); stdcall;
3. Implement this callback function or process in the program;
Function HDFunexample (K: Integer, Sexam: String): Integer; stdcall;
Begin
END;
Procedure hdproexample (sexam: string); stdcall;
Begin
END;
4, call the process; the callback function is generally the entrance address of a function of the system;
According to the original shape of the call function:
Assume that there is a call function:
Function DYHDFUNEXAMPLE (HDFun: THDFunction; i: integer): boolean;
Note:
In the call function, by the processing of the function pointer can directly call the callback function (that is, the one in the callback function is the parameter of the callback function type, directly operates it), and performs a certain operation. That is, the function of the callback function is implemented in the call function.
transfer:
VAR
I: integer;
Begin
I: = DyhdFunexample (@ hdfunexample, i);
// .......
END;
3 Example
The sample program is in the H: / Pickup Function Example / Directory.
The use of the callback function is primarily in the original API function of Windows, but is generally in the dynamic connection library for user custom call functions. The regular basis will generally do not need to use a callback function. (In my opinion)..