我在用 MFC
编写我的应用程序时遇到了一个问题,需要帮助。
我在我的应用程序中使用 CLR
即 Common Language Runtime
来集成 c# API。
但现在我坚持将 System::String^
转换为 CString
。
我做不到。
我正在使用以下代码。
String^ csPass = gcnew String(strPassword.GetBuffer());
array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass);
for (int i = 0; i < Value->Length; i++ )
{
csPass += String::Format( "{0:X2}", Value[ i ] );
}
现在我想将 csPass
转换为 CString
。
谁可以帮我这个事。
提前谢谢你。
最佳答案
得到我的答案。 感谢您对@Elliot Tereschuk 的支持。
我已经阅读了一些引用资料,例如
和
包含头文件
#include <msclr/marshal_windows.h>
#include <msclr/marshal.h>
使用库
使用命名空间 msclr::interop;
最后是我的源代码。
String^ csPass = gcnew String(strPassword.GetBuffer());
array<Byte>^ Value = Encoding::UTF8->GetBytes(csPass);
for (int i = 0; i < Value->Length; i++ )
{
csPass += String::Format( "{0:X2}", Value[ i ] );
}
marshal_context^ context = gcnew marshal_context();
const char* str = context->marshal_as<const char*>(csPass);
csMyPass.Format(str);
csMypass
是一个CString
类型的变量。
感谢您的支持。
关于c# - 将 C# 中的 String^ 转换为 C++/CLI 中的 CString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059950/