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

标签 c# .net string mfc c-strings

我在用 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/

相关文章:

objective-c - 可选字符串与空字符串

c# - 使用 EPPlus 返回 INT 的 Excel 日期列

c# - 从Xml文件中提取值

c# - 使用工作表作为数据源的 VSTO Excel 简单示例

c# - 如何修复 "The requested resource is in use. (Exception from HRESULT: 0x800700AA)"

python - 如何在数据框中查找其间带有下划线文本的字符串

c# - MVVM:加载大数据记录

c# - 使用.NET读取oracle表,一个LONG类型的列总是返回空字符串,如何解决?

c# - 绑定(bind)到 ListBox 中已排序的 ObservableCollection<T>

python - 从 Python 中的任何给定字符串类型中删除非 ascii 字符