C++ CLI System.String^ to MFC LPCTSTR



如何将System(.net)C++CLI String^转换为MFC C++LPCTSTR字符串。

LPCTSTR转换为String^非常容易,但到目前为止,还没有发现其他方法。

如果您有Visual Studio 2008或更高版本,您应该能够使用C++/CLI封送处理库来完成此操作,如下所示:

#include <msclrmarshal.h>
using namespace System;
using namespace msclr::interop;
...
String^ cliString;
marshal_context context;
LPCTSTR cstr = context.marshal_as<const TCHAR*>(cliString);

MSDN上关于类型之间封送的更多信息:C++中封送概述

您可能想尝试Marshal::StringToHGlobalUni、Marshal::StringToHGglobalAuto或Marshal:。

请记住,需要使用Marshal::FreeHGlobal释放分配的非托管字符串。

最新更新