[英]C++ CLI System.String^ to MFC LPCTSTR
How would I convert a System (.net) C++\\CLI
String^
into a MFC C++ LPCTSTR
string. 如何将系统(.net) C++\\CLI
String^
转换为MFC C ++ LPCTSTR
字符串。
It is very easy to get a LPCTSTR
into String^
, but so far found nothing on doing it the other way around. 将LPCTSTR
变为String^
非常容易,但到目前为止还没有找到相反的方法。
If you have Visual Studio 2008 or above, you should be able to do this using the C++/CLI marshaling library, like so: 如果您使用的是Visual Studio 2008或更高版本,则可以使用C ++ / CLI封送库执行此操作,如下所示:
#include <msclr\marshal.h>
using namespace System;
using namespace msclr::interop;
...
String^ cliString;
marshal_context context;
LPCTSTR cstr = context.marshal_as<const TCHAR*>(cliString);
More information on marshaling between types on MSDN: Overview of Marshaling in C++ 有关MSDN上类型之间编组的更多信息: C ++中的编组概述
You might want to try Marshal::StringToHGlobalUni , Marshal::StringToHGlobalAuto or Marshal::StringToHGlobalAnsi . 您可能想要尝试Marshal :: StringToHGlobalUni , Marshal :: StringToHGlobalAuto或Marshal :: StringToHGlobalAnsi 。
Remember the allocated unmanaged strings will need to be freed with Marshal::FreeHGlobal . 请记住,需要使用Marshal :: FreeHGlobal释放分配的非托管字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.