Calling a callback function in Delphi from a C++ DLL

前端 未结 1 759
灰色年华
灰色年华 2020-12-11 18:18

I have a C++ DLL that I wrote that has a single exposed function, that takes a function pointer (callback function) as a parameter.

#define DllExport   exte         


        
相关标签:
1条回答
  • 2020-12-11 18:54

    Declare a function pointer in Delphi by declaring a function type. For example, the function type for your callback could be defined like this:

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

    Note the calling convention is cdecl because your C++ code specified no calling convention, and cdecl is the usual default calling convention for C++ compilers.

    Then you can use that type to define the DLL function:

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

    Replace 'dllname' with the name of your DLL.

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

    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 as you would any other variable:

    RegisterCallbackGetProperty(Callback);
    
    0 讨论(0)
提交回复
热议问题