callback function and its use of Delphi

Source: Internet
Author: User
Tags function prototype prototype definition

1 Overview of the callback function
A callback function is a mechanism by which a caller initializes an object (where the object is generic, including objects in OOP, global functions, and so on), passes some parameters to the object, and passes the address of a function that the caller can access to the object. This function is a notification convention between the caller and the callee, and when the scheduled event occurs, the callee (which typically contains a worker thread) calls the function at the address of the callback function.
This way, the caller is in one thread, and the caller is in another thread.
News:
The message can also be thought of as a form of callback, because the message is also passed by the caller to the callee during initialization with a handle and a message number, and the caller 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 the event model, such as the OnCreate event for Tform, which is the principle of specifying event functions at design time and triggering events at run time, then invoking the event functions specified at design time.
On the mechanism, the Delphi event model is the same as the callback. But the specific form is somewhat different, the pure callback function is the form of the global function, and the Delphi event is the form of the object method, that is, the following callback function type can be defined

Typeprocedureofobject;

2 instructions for using the callback function

The callback function is mainly used in two situations, the first is that some Windows API requires the callback function as its parameter address, and the other is that a function defined by the user in a particular situation needs to use the callback function as its parameter address, for the user's defined function, This is typically used when invoking a function in a dynamic connection library.
There are several main steps to using a callback function:
1, the definition of a callback function is not different from the definition of the general Function procedure, but its definition must satisfy the function requirement of the callback function according to need, the only difference is that it must be declared as Windows standard call after the definition of the function or procedure;

Cases:

type   thdfunctionfunction(i:integer;s:stringstdcall;

2, and then according to this prototype definition of a corresponding function or procedure, for this function or procedure, the name does not have any requirements, the function of its parameters and the type of the return value must be the same as the type of the defined callback function, for the procedure, only the same parameter type is required.

Example: a corresponding function and a corresponding procedure are defined according to the original function and the process's prototype.
Function prototype Definition:

Function hdfunexample (k:integer,sexam:stringstdcall;

Process definition:

procedure Hdproexample (sexam:string); stdcall;

3, in the program to implement this callback function or process;

Function hdfunexample (k:integer,sexam:stringstdcall;   Begin  End;   procedure Hdproexample (sexam:string); stdcall ;    begin  End;

4, the call process;

The callback function is generally used as the entry address of a function of the system;
Based on the prototype of the calling function:
Enter discussion group discussion.
Suppose you have the following calling function:

function Dyhdfunexample (Hdfun:thdfunction;i:integer): boolean;

Note:

In the calling function, the function pointer can be directly called by the callback function (that is, the call function is the callback function type parameter, directly manipulate it), so that the callback function to perform certain operations. That is, the function of implementing the callback function in the calling function.
Call:

var   I:integer;   begin   i:=dyhdfunexample (@HdFunExample, I);   // ... ..   End;

3 Illustrative examples

Sample program in H:\ callback function example \ directory below.
The use of callback functions is mainly in the original Windows API functions, but the user's custom call function is generally in the Dynamic Connection library. Normally, a callback function is not required under the same project. (Personal opinion):

Transferred from: http://www.cnblogs.com/newtekboy/archive/2010/05/23/1742092.html

callback function and its use of Delphi

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.