我尝试在我的 Delphi 应用程序中使用 C++ DLL 函数。我在使用这样的回调函数时遇到了问题:
C++ 中的导出函数定义:
typedef void (__CDECL__* RegisterCallBacksFunction)(AudioChunkCallBackFunction, CaptureEventCallBackFunction, LogMngr*);
回调定义:
typedef void (__CDECL__*AudioChunkCallBackFunction)(AudioChunkRef, CStdString& capturePort);
typedef void (__CDECL__*CaptureEventCallBackFunction)(CaptureEventRef, CStdString& capturePort);
AudioChunkRef 和 CaptureEventRef :
typedef boost::shared_ptr<AudioChunk> AudioChunkRef;
typedef boost::shared_ptr<CaptureEvent> CaptureEventRef;
AudioChunk和CaptureEvent是C++类:
class __declspec( dllexport ) AudioChunk // or CaptureEvent (both are similar)
{
public:
//some functions and variables
private:
//some functions and variables
};
C++ 库导出:
extern "C"
{
__declspec( dllexport ) void __CDECL__ RegisterCallBacks(AudioChunkCallBackFunction, CaptureEventCallBackFunction, LogMngr*);
}
如何在我的 Delphi 应用程序中使用 RegisterCallBacks 函数。 ?
最佳答案
据我所知,您不能直接导入 C++ 类来映射 Delphi 类。也就是说,即使您获得了指向 AudioChunk
实例的指针,您也无法直接从 Delphi 访问它。因此无法直接使用您的回调。
所以你必须手动定义一个"flat" C interface在 Delphi 中导入,它将在 C++ 中实现回调,然后将接口(interface)公开为 C 函数,参数为 C struct
而不是 C++ boost 类。
关于c++ - 在 Delphi 应用程序中使用 C++ DLL 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8294267/