Delphi callback function call from C ++ DLL

I have a C ++ DLL that I wrote that has one public function that takes a function pointer (callback function) as a parameter.

#define DllExport extern "C" __declspec( dllexport ) DllExport bool RegisterCallbackGetProperty( bool (*GetProperty)( UINT object_type, UINT object_instnace, UINT property_identifer, UINT device_identifier, float * value ) ) { // Do something. } 

I want to be able to call this open C ++ DLL function from a Delphi application and register a callback function that will be used in the future. But I'm not sure how to make a function pointer in Delphi, which will work with an open C ++ DLL function.

I have a Delphi application that calls simple open C ++ DLL functions from the help I received in this question.

I am creating a C ++ DLL and I can change its parameters if necessary.

My questions:

  • How to create a function pointer in Delphi
  • How to properly call an open C ++ DLL function from a Delphi application so that the C ++ DLL function can use the function pointer.
+4
source share
1 answer

Declare a function pointer in Delphi by declaring a function type. For example, the type of function for your callback can be defined as follows:

 type TGetProperty = function(object_type, object_instnace, property_identifier, device_identifier: UInt; value: PSingle): Boolean; cdecl; 

Note that the cdecl calling cdecl is because your C ++ code does not specify a calling convention, and cdecl is the usual standard default convention for C ++ compilers.

Then you can use this type to define the function of the DLL:

 function RegisterCallbackGetProperty(GetProperty: TGetProperty): Boolean; cdecl; external 'dllname'; 

Replace 'dllname' with the name of your DLL.

To call a DLL function, you must first have a Delphi function with a signature that matches the type of the callback. For instance:

 function Callback(object_type, object_instnace, property_identifier, device_identifier: UInt; value: PSingle): Boolean cdecl; begin Result := False; end; 

Then you can call the DLL function and pass the callback just like any other variable:

 RegisterCallbackGetProperty(Callback); 
+11
source

All Articles