在Delphi中从C ++ DLL调用回调函数 [英] Calling a callback function in Delphi from a C++ DLL
问题描述
#define DllExport externC__declspec(dllexport)
DllExport bool RegisterCallbackGetProperty(bool(* GetProperty)(UINT object_type,UINT object_instnace,UINT property_identifer,UINT device_identifier,float * value)){
//执行某些操作。
}
我想能够从Delphi内调用这个暴露的C ++ DLL函数应用并注册将来使用的回调函数。但我不确定如何在Delphi中创建一个函数指针,该函数指针可以使用暴露的C ++ DLL函数。
我有 Delphi应用程序调用一个简单的暴露的c ++ DLL函数从我在这个问题的帮助。
我正在构建C ++ DLL,如果需要,我可以更改其参数。
我的问题是:
- 如何在Delphi中创建一个函数指针
- 如何从Delphi应用程序中正确调用暴露的C ++ DLL函数,以便C ++ DLL函数可以使用函数指针。
通过声明函数类型在Delphi中声明一个函数指针。例如,您的回调的函数类型可以这样定义:
type
TGetProperty = function(object_type,object_instnace,property_identifier,device_identifier:UInt; value:PSingle):Boolean; CDECL;
请注意,调用约定为 cdecl
,因为您的C ++代码指定没有调用约定,而cdecl是C ++编译器通常的默认调用约定。
然后可以使用该类型定义DLL函数:
function RegisterCallbackGetProperty(GetProperty:TGetProperty):Boolean; CDECL;外部'dllname';
将'dllname'
替换为您的DLL。
要调用DLL函数,您应该首先具有与回调类型匹配的签名的Delphi函数。例如:
function Callback(object_type,object_instnace,property_identifier,device_identifier:UInt; value:PSingle ):Boolean cdecl;
begin
结果:= False;
结束
然后可以调用DLL函数并传递回调,就像其他变量一样: p>
RegisterCallbackGetProperty(Callback);
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 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 exposed C++ DLL function from within a Delphi application and register the callback function to be used at a future date. But I am unsure of how to make a function pointer in Delphi that will work with the exposed C++ DLL function.
I have the Delphi application calling a simple exposed c++ DLL functions from the help I got in this question.
I am building the C++ DLL and I can change its parameters if needed.
My questions are:
- How to I create a function pointer in Delphi
- How to I correctly call the exposed C++ DLL function from within a Delphi application so that the C++ DLL function can use the function pointer.
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);
这篇关于在Delphi中从C ++ DLL调用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!