字符串对话:从LPCTSTR到托管字符串 [英] String conversation: from LPCTSTR to managed String
本文介绍了字符串对话:从LPCTSTR到托管字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
如何将LPCTSTR转换为String ^?
最好的问候:Delphi 2005
Hello,
how can I convert an LPCTSTR to String^?
Best regards: Delphi 2005
推荐答案
尝试: ^ ]
最好的问候
Espen Harlinn
Try: How to: Convert Standard String to System::String[^]
Best regards
Espen Harlinn
除了Espen的基本正确答案:
他引用的Microsoft文章并未解释所有内容,也无法回答OP提出的问题的100%.本文的标题也是值得怀疑的:它没有显示什么是标准字符串"(System :: String也已由ECMA和ISO标准化).
1)OP正确提到了LPCTSTR
,但是C ++项目应使用UNICODE选项进行编译,以使该类型与System::String
兼容.
2)问题与std::string
无关.答案是,当用UNICODE编译C ++代码时,使用C ++类型为wchar_t
,WCHAR
或TCHAR
的构造函数System::String(char*)
.在所引用的Microsoft文章中,还通过功能c_str()
从std::string
到LPCTSTR
的转换.这与OP的问题无关.有一篇很好的CodeProject文章介绍了涉及的字符和字符串类型:《 C ++字符串完整指南,第一部分-Win32字符编码》 [ ^ ],另请参见 http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr [ ^ ].
3)此System::String
构造函数不符合CLS,请参阅 http://msdn.microsoft.com/zh-cn/library/6y4za026.aspx [ ^ ].
—SA
In addition to basically correct answer by Espen:
The Microsoft article he references does not explain everything and does not answer 100% of OP''s question. What is also questionable in this article, is its title: it does not show what is "Standard String" (System::String is also standardized by ECMA and ISO).
1) OP correctly mentionedLPCTSTR
, but C++ project should be compiled with UNICODE option to make this type compatible withSystem::String
.
2) The question has nothing to do withstd::string
. The answer is use the constructorSystem::String(char*)
with the C++ typewchar_t
,WCHAR
orTCHAR
when C++ code is compiled with UNICODE. In the referenced Microsoft article, there is also a conversion fromstd::string
toLPCTSTR
via the functionc_str()
; it is irrelevant to the OP''s question. There is a good CodeProject article explaining character and string type involved: The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^], see also http://stackoverflow.com/questions/321413/lpcstr-lpctstr-and-lptstr[^].
3) ThisSystem::String
constructor is not CLS-compliant, see http://msdn.microsoft.com/en-us/library/6y4za026.aspx[^].
—SA
这篇关于字符串对话:从LPCTSTR到托管字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文