如何将Delphi中的UTF-8字符串传递给DLL C extern函数?

rho*_*ody 2 c delphi string utf-8

我在Delphi中调用Visual Studio编译的DLL中的C extern函数.DLL方法又调用C++方法,该方法将C++字符串类型作为参数.Delphi端的字符串是UTF-8编码的(没有BOM).我需要确保采用字符串类型的C++方法获取UTF-8编码的字符串.

我可以修改DLL源代码.我的问题:

我在Delphi端的UTF-8字符串是string类型.C extern方法应该采用什么类型?PChar,PWideChar?以及如何将其转换为C++字符串类型?

注意:我无法首先将UTF-8字符串转换为AnsiString,因为编码存储了一些必须保留的希腊字母.C++端将复制Delphi字符串并处理任何已分配的内存.

Delphi结束(使用XE6):

mystr : string;

callCExternMethod (mystr) // cast to what?
Run Code Online (Sandbox Code Playgroud)

C++ End(使用VS 2013):

void callCExternMethod (????? mystr) {

  // convert mystr to C++ string type

  callCPlusPlusMethod (takes C++ string type)
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 6

在Delphi端,参数就是PAnsiChar你传递的:PAnsiChar(Utf8String(str)).

在C++端,您将收到参数as const char*.

显然,您需要确保调用约定匹配.