字符串对话:从LPCTSTR到托管字符串 [英] String conversation: from LPCTSTR to managed String

查看:90
本文介绍了字符串对话:从LPCTSTR到托管字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

如何将LPCTSTR转换为String ^?

最好的问候:Delphi 2005

Hello,

how can I convert an LPCTSTR to String^?

Best regards: Delphi 2005

推荐答案

尝试:
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_tWCHARTCHAR的构造函数System::String(char*).在所引用的Microsoft文章中,还通过功能c_str()std::stringLPCTSTR的转换.这与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 mentioned LPCTSTR, but C++ project should be compiled with UNICODE option to make this type compatible with System::String.

2) The question has nothing to do with std::string. The answer is use the constructor System::String(char*) with the C++ type wchar_t, WCHAR or TCHAR when C++ code is compiled with UNICODE. In the referenced Microsoft article, there is also a conversion from std::string to LPCTSTR via the function c_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) This System::String constructor is not CLS-compliant, see http://msdn.microsoft.com/en-us/library/6y4za026.aspx[^].

—SA


这篇关于字符串对话:从LPCTSTR到托管字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆