C++ CLI System.String^ to MFC LPCTSTR
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释放分配的非托管字符串。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- C++ Singleton - Prevent ::instance() to variable
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- C++ MFC Libraries in Travis CI
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- Python str to C++ to Python str
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- C++ UTF-8/ASCII to UTF-16 in MFC
- MFC 错误仅在发布模式下"Failed to create an empty document"
- CString to LPWSTR in mfc
- MFC Web 浏览器.导航错误"A null reference pointer was passed to the stub."
- C++ CLI System.String^ to MFC LPCTSTR
- C++ MFC double to CString