In Delphi, a function can be passed as a parameter through a function pointer and then called in another function.
1) First, declare the function pointer type tfunctionparameter.
Type
Tfunctionparameter = function (const value: integer): string;
2) define the function to be passed as a parameter
Function one (const value: integer): string;
Begin
Result: = inttostr (value );
End;
Function two (const value: integer): string;
Begin
Result: = inttostr (2 * value );
End;
3) define the function that will use the dynamic function pointer Parameter
Function dynamicfunction (F: tfunctionparameter; const value: integer): string;
Begin
Result: = f (value );
End;
4) Examples of using the above Dynamic Function
VaR
S: string;
Begin
S: = dynamicfunction (one, 2006 );
Showmessage (s); // will display "2006"
S: = dynamicfunction (two, 2006 );
Showmessage (s); // will display "4012"
End;
A pointer to a function does not need to display the address of the function when the value is assigned to the function.
Example:
VaR F: function (X: integer): integer;
...
Function AA (X: integer): integer;
Not required: F: = ^ AA;
As long as: F: = AA.
F: = AA may be a function type variable value assignment, or it may be the process of calling the AA function.
If F is not a function procedure type, it is a function call.
But as long as it appears in the expression, it must be called in the function process.
Example: If a: = fun then
A: = fun must be a function process call. Assign the return value to.
Note that if fun is a process (it does not return values) or it requires parameters (you need to write parameters), a syntax error will occur.
If you want to display it, it indicates that it is a value assignment statement rather than a call to the function process, you can write it like this.
If @ A: = @ fun then
@ A is to convert a to a non-type pointer (it itself exists as a pointer)
@ Fun is the address for getting fun in the function process.
You can obtain the address of the function variable in the process through @ A, instead of the address of the function process to which it points.
Void setprocessdataproc (notisfydataisinclued processdataproc)
Parameter: processdataproc -- callback function pointer.
Return Value: None
Function: sets the data Arrival notification function. The dynamic Connection Library proactively notifies the user of the arrival of new data, and then the user can immediately query the relevant data. After the user calls this interface function to set the notification function, the dynamic Connection Library notifies the user whenever new data arrives. The notification function prototype must be as follows: (the function name and parameter name can be different)
Void processdataproc (INT ibedno, unsigned char ucdatatype, wparam );
The ibedno parameter indicates the device number corresponding to the arrival data.
Ucdatatype indicates the data type. Table 1 lists the data packet types and commonly called interface functions of ucdatatype. For data packet types not listed in the table, there is no useful data in the data packet.
Wparam is used to pass other information. It is retained and is not used yet.
Therefore, the parameter type of the setprocessdataproc interface function notisfydataisinclued can be defined as follows:
Typedef void (* notisfydataisinclued) (INT ibedno, unsigned char ucdatatype, wparam );
This is a function in the dynamic connection library. I want to call this function in Delphi. How can I define the above types and functions.
Void processdataproc (INT ibedno, unsigned char ucdatatype, wparam );
->
Procedure processdataproc (ibedno: integer; ucdatatype: byte; wparam: wparam); stdcall;
Typedef void (* notisfydataisinclued) (INT ibedno, unsigned char ucdatatype, wparam );
->
Type notisfydataisinclued = procedure of (ibedno: integer; ucdatatype: byte; wparam: wparam) of object ;///
Use of Delphi function pointers)