简体   繁体   中英

Use C++ DLL callback function in a Delphi application

I try to use C++ DLL function in my Delphi application. I've the problem in using call back function like that:

Exported functions definitions in 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 :

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

And AudioChunk and CaptureEvent are C++ class:

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

C++ library exports:

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

How can use RegisterCallBacks function in my Delphi app. ?

AFAIK you can not import C++ classes directly to map Delphi classes. That is, even if you get a pointer to a AudioChunk instance, you won't be able to access it directly from 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.

The naming conventions of different languages (like a Pascal and C++) is very complicated topic. At least C++ doesn't grant that migration from one vendor to another will keep same export name for particular methods. For example: Eleven@SomeClass@@QAEHXZ - is declaration of int SomeClass::Eleven() for MSVC. The only workable way - is C declaration, using extern "C" granted by standard conventions.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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