且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++消费delphi DLL

更新时间:2023-02-07 18:26:59

为了开始您当前的代码不能希望成功,因为我假设 string std :: string 。这是Delphi代码不能提供或消费的C ++数据类型。要匹配Delphi的 WideString ,您需要使用COM BSTR 数据类型。

For a start your current code can't hope to succeed since I presume string is std::string. That's a C++ data type which Delphi code cannot either provide or consume. To match up against Delphi's WideString you need to use the COM BSTR data type.

您的代码的另一个问题是它在C ++端使用 cdecl ,而 stdcall 在Delphi端。您将需要调整调用约定。

Another problem with your code as it stands is that it uses cdecl in the C++ side, and stdcall on the Delphi side. You'll need to align the calling conventions.

但是,这也将失败,因为Delphi的ABI返回值和平台标准之间的差异。该主题在此详细讨论:为什么不能将WideString用作interop的函数返回值?

However, that will also fail because of a difference between Delphi's ABI for return values, and the platform standard. That topic was covered in detail here: Why can a WideString not be used as a function return value for interop?

***的办法是停止使用 WideString 作为返回值,并将其转换为C ++参考参数。

Your best bet is to stop using WideString as a return value and convert it into a C++ reference parameter. You'll want to convert the Delphi to match.

您正在查找类似如下的内容:

You are looking at something like this:

strong> Delphi

Delphi

procedure rData(ID: Cardinal; queue: WideString; var Result: WideString); stdcall;

C ++

typedef void (__stdcall *ReturnDataSPL)(DWORD, BSTR, BSTR&);