如何将系统 (.net)C++\CLI
String^
转换为 MFC C++LPCTSTR
字符串。
LPCTSTR
进入非常容易String^
,但到目前为止,还没有发现相反的方法。
如果您有 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);
有关 MSDN 上类型间编组的更多信息:C++ 中的编组概述
您可能想尝试Marshal::StringToHGlobalUni、Marshal::StringToHGlobalAuto或Marshal::StringToHGlobalAnsi。
请记住,分配的非托管字符串需要使用Marshal::FreeHGlobal释放。