简体   繁体   English

在Delphi应用程序中使用C ++ DLL回调函数

[英]Use C++ DLL callback function in a Delphi application

I try to use C++ DLL function in my Delphi application. 我尝试在我的Delphi应用程序中使用C ++ DLL函数。 I've the problem in using call back function like that: 我在使用像这样的回调函数时遇到问题:

Exported functions definitions in C++: 用C ++导出的函数定义:

typedef void (__CDECL__* RegisterCallBacksFunction)(AudioChunkCallBackFunction, CaptureEventCallBackFunction, LogMngr*);

Callback definitions: 回调定义:

typedef void (__CDECL__*AudioChunkCallBackFunction)(AudioChunkRef, CStdString& capturePort);
typedef void (__CDECL__*CaptureEventCallBackFunction)(CaptureEventRef, CStdString& capturePort);

AudioChunkRef and CaptureEventRef : AudioChunkRef和CaptureEventRef:

typedef boost::shared_ptr<AudioChunk> AudioChunkRef;
typedef boost::shared_ptr<CaptureEvent> CaptureEventRef;

And AudioChunk and CaptureEvent are C++ class: AudioChunk和CaptureEvent是C ++类:

class __declspec( dllexport ) AudioChunk // or CaptureEvent (both are similar)
{
public:
//some functions and variables
private:
//some functions and variables
};

C++ library exports: C ++库导出:

extern "C"
{
__declspec( dllexport ) void __CDECL__  RegisterCallBacks(AudioChunkCallBackFunction, CaptureEventCallBackFunction, LogMngr*);
}

How can use RegisterCallBacks function in my Delphi app. 如何在我的Delphi应用程序中使用RegisterCallBacks函数。 ?

AFAIK you can not import C++ classes directly to map Delphi classes. AFAIK,您不能直接导入C ++类来映射Delphi类。 That is, even if you get a pointer to a AudioChunk instance, you won't be able to access it directly from Delphi. 也就是说,即使您获得了指向AudioChunk实例的指针,也将无法直接从Delphi中访问它。 So use of your callbacks won't be possible directly. 因此,将无法直接使用回调。

So you'll have to define by hand a "flat" C interface to be imported in Delphi, which will implement the callback in C++, then expose the interface as C functions, with parameters as C struct instead of C++ boost classes. 因此,您必须手动定义要在Delphi中导入的“平面” C接口 ,该接口将在C ++中实现回调,然后将该接口公开为C函数,并将参数作为C struct而不是C ++ boost类。

The naming conventions of different languages (like a Pascal and C++) is very complicated topic. 不同语言(例如Pascal和C ++)的命名约定是非常复杂的主题。 At least C++ doesn't grant that migration from one vendor to another will keep same export name for particular methods. 至少C ++不允许从一个供应商迁移到另一供应商将为特定方法保留相同的导出名称。 For example: Eleven@SomeClass@@QAEHXZ - is declaration of int SomeClass::Eleven() for MSVC. 例如: Eleven@SomeClass@@QAEHXZ是MSVC的int SomeClass::Eleven()声明。 The only workable way - is C declaration, using extern "C" granted by standard conventions. 唯一可行的方法是使用标准约定授予的extern "C" C声明。

But what about classes? 但是上课呢? To avoid wheel reinvention - for case Delphi-C++ use OLE/COM, in general you will declare interface and could be able to do the same on both sides. 为了避免重新发明轮子-对于Delphi-C ++使用OLE / COM的情况,通常,您将声明接口,并且可以在两边都做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM