简体   繁体   English

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

[英]How to pass unicode string from C++ to delphi?

Ive found many topics about passing from Delphi to C++ but still confused about. Ive找到了许多有关从Delphi传递到C ++的话题,但仍然感到困惑。

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

What is the correct way to pass it to a delphi code? 将其传递给delphi代码的正确方法是什么?

none of this is not working, producing wrong chars 所有这些都不起作用,产生错误的字符

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

You did not say which version of Delphi you are using, but the fact that you use UTF8String the way you are implies that you are using Delphi 2009 or later. 您没有说要使用哪个版本的Delphi,而是您使用的是UTF8String的事实表明您使用的是Delphi 2009或更高版本。 If so, PChar is PWideChar ( wchar_t* in C and C++). 如果是这样,则PCharPWideChar (在C和C ++中为wchar_t* )。 Use PAnsiChar ( char* in C and C++) explicitly instead, and get rid of your unnecessary Pointer typecast: 改用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;

Alternatively, use std::wstring with PWideChar instead: 或者,将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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM