0

我有点困惑我们是否可以使用 _bstr_t 在 c# 到 C++ 之间传递字符串,反之亦然。

我使用 C++/CLI 作为 C# 和 C++ 之间的中间层。

如果可能的话,还举例说明如何在 C# 端和 C++/CLI 端编组它,因为没有太多关于它的文档

还建议更好的数据类型/机制来传递字符串。

4

1 回答 1

0

根据 MSDN,这应该适用于将 String^ 转换为字符串(这是迄今为止最简单的方法)。但是我无法测试,我的 VC++2008 Express 没有找到 Marshal.h ...

// TestCppCli2008.cpp : main project file.

#include "stdafx.h"
#include <string>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>

using namespace System;
using namespace msclr::interop;

int main()
{
    System::String^ s = L"Hello World";

    std::string str = marshal_as<std::string>(s);
    return 0;
}
于 2013-08-26T20:30:04.480 回答