【发布时间】:2016-08-30 06:56:24
【问题描述】:
我正在从 Delphi 调用 Visual Studio 编译的 DLL 中的 C extern 函数。 DLL 方法又调用一个 C++ 方法,该方法将 C++ 字符串类型作为参数。 Delphi 端的字符串是 UTF-8 编码的(没有 BOM)。我需要确保采用字符串类型的 C++ 方法获取 UTF-8 编码的字符串。
我可以修改 DLL 源代码。我的问题:
我在 Delphi 端的 UTF-8 字符串是字符串类型。 C extern 方法应该采用什么类型? PChar,PWideChar?以及如何将其转换为 C++ 字符串类型?
注意:我不能先将 UTF-8 字符串转换为 AnsiString,因为编码存储了一些必须保留的希腊字母。 C++ 端将复制 Delphi 字符串并处理任何分配的内存。
Delphi 结束(使用 XE6):
mystr : string;
callCExternMethod (mystr) // cast to what?
C++ 结束(使用 VS 2013):
void callCExternMethod (????? mystr) {
// convert mystr to C++ string type
callCPlusPlusMethod (takes C++ string type)
}
【问题讨论】:
-
这个问题肯定已经被问了 100 次了.....你试过谷歌搜索吗?每次你调用一个使用文本的 Windows 函数时,你就是在做这件事。
-
德尔福端的PAnsiChar。 const char* 在 C++ 端。在调用该函数之前,您需要将其编码为空终止的 utf8 strong。 RTL 有这方面的功能。尝试搜索。
-
哦,确保调用约定正确。它必须是
cdecl或stdcall(可能是前者)。