如何将unicode字符串从C++传递到delphi

How to pass unicode string from C++ to delphi?

本文关键字:delphi C++ unicode 字符串      更新时间:2023-10-16

我发现了许多关于从Delphi传递到C++的主题,但仍然感到困惑。

std::string s1(" look    here ");

将它传递给delphi代码的正确方法是什么?

这些都不起作用,产生错误的字符

char * s = (char *)s1.c_str();
Call_Delphi_func(s);
.......
Memo1.Lines.Add(UTF8String(PChar(pointer(s))));

您没有说明您使用的是哪个版本的Delphi,但您使用UTF8String的方式意味着您使用的是Delphi 2009或更高版本。如果是,则PCharPWideChar(C和C++中的wchar_t*(。显式使用PAnsiChar(在C和C++中为char*(,并去掉不必要的Pointer类型转换:

std::string s1 = u8" look    here ";
char * s = const_cast<char*>(s1.c_str());
Delphi_func(s);
procedure Delphi_func(s: PAnsiChar); stdcall;
begin
Memo1.Lines.Add(UTF8String(s));
end;

或者,将std::wstringPWideChar一起使用:

std::wstring s1 = L" look    here ";
wchar_t * s = const_cast<wchar_t*>(s1.c_str());
Delphi_func(s);
procedure Delphi_func(s: PWideChar); stdcall;
begin
Memo1.Lines.Add(s);
end;