[英]How to pass UTF-8 string from Delphi to a DLL C extern function?
我在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?
C ++ End(使用VS 2013):
void callCExternMethod (????? mystr) {
// convert mystr to C++ string type
callCPlusPlusMethod (takes C++ string type)
}
在Delphi端,參數是PAnsiChar
,你可以這樣傳遞: PAnsiChar(Utf8String(str))
。
在C ++端,您將獲得參數const char*
。
顯然,您需要確保調用約定匹配。
另一種選擇是使用UTF8String
類型:
mystr : string;
u8: UTF8String;
u8 := UTF8String(mystr);
callCExternMethod(PAnsiChar(u8));
注意:除非您使用補丁啟用,否則UTF8String
類型不能在Delphi XE5到10.0 Seattle的移動平台上使用:
http://andy.jgknet.de/blog/2013/10/the-return-of-the-byte-strings/
UTF8String
已重新啟用,可在德爾福10.1柏林的移動設備中使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.