gpt4 book ai didi

c# - 将 C# 中的 String^ 转换为 C++/CLI 中的 CString

转载 作者:可可西里 更新时间:2023-11-01 09:08:57 26 4
gpt4 key购买 nike

我在用 MFC 编写我的应用程序时遇到了一个问题,需要帮助。

我在我的应用程序中使用 CLRCommon 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 的支持。

我已经阅读了一些引用资料,例如

  1. How to: Extend the Marshaling Library
  2. Overview of Marshaling in C++
  3. For CString.Format()

包含头文件

#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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com