如何将系统(.net) C++\CLI String^转换为MFC C++ LPCTSTR字符串。
将LPCTSTR放入String^非常容易,但到目前为止还没有找到相反的方法。
发布于 2012-03-20 15:54:57
如果您有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上的类型之间进行封送处理的更多信息:Overview of Marshaling in C++
发布于 2012-03-20 15:32:05
您可能想尝试Marshal::StringToHGlobalUni、Marshal::StringToHGlobalAuto或Marshal::StringToHGlobalAnsi。
请记住,分配的非托管字符串需要使用Marshal::FreeHGlobal释放。
https://stackoverflow.com/questions/9782669
复制相似问题