C ++ .NET将System :: String转换为std :: string

时间:2009-08-19 15:28:57

标签: .net c++ string marshalling

如何在C ++ .NET中将System :: String转换为std :: string?

6 个答案:

答案 0 :(得分:63)

如果您使用的是最新版本的.net

,则会有更清晰的语法
#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::string standardString = context.marshal_as<std::string>(managedString);

    return 0;
}

这也可以让您在例外情况下更好地进行清理。

其他各种转化都有msdn article

答案 1 :(得分:27)

为了响应更高版本的C ++ / CLI中的“更简单的方法”,您可以在没有marshal_context的情况下执行此操作。我知道这适用于Visual Studio 2010;之前不确定。


#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace msclr::interop;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    std::string standardString = marshal_as<std::string>(managedString);

    return 0;
}

答案 2 :(得分:6)

stdString = toss(systemString);

  static std::string toss( System::String ^ s )
  {
    // convert .NET System::String to std::string
    const char* cstr = (const char*) (Marshal::StringToHGlobalAnsi(s)).ToPointer();
    std::string sstr = cstr;
    Marshal::FreeHGlobal(System::IntPtr((void*)cstr));
    return sstr;
  }

答案 3 :(得分:6)

C#使用UTF16格式作为字符串 因此,除了转换类型之外,您还应该注意字符串的实际格式。

编译多字节字符集时,Visual Studio和Win API采用UTF8(实际上是Windows-28591的Windows编码)。
编译 Unicode字符集时,Visual Studio和Win API采用UTF16。

因此,您必须将字符串从UTF16转换为UTF8格式,而不仅仅是转换为std :: string。 在使用多种字符格式(如某些非拉丁语言)时,这将是必要的。

我们的想法是确定std::wstring 始终代表 UTF16
std::string 始终代表 UTF8

这不是由编译器强制实施的,而是一个更好的策略。

#include "stdafx.h"
#include <string>

#include <msclr\marshal_cppstd.h>

using namespace System;

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;

    //Actual format is UTF16, so represent as wstring
    std::wstring utf16NativeString = context.marshal_as<std::wstring>(managedString); 

    //C++11 format converter
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;

    //convert to UTF8 and std::string
    std::string utf8NativeString = convert.to_bytes(utf16NativeString);

    return 0;
}

或者使用更紧凑的语法:

int main(array<System::String ^> ^args)
{
    System::String^ managedString = "test";

    msclr::interop::marshal_context context;
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;

    std::string utf8NativeString = convert.to_bytes(context.marshal_as<std::wstring>(managedString));

    return 0;
}

答案 4 :(得分:4)

出现上述答案时出现了太多含糊不清的错误(是的,我是C ++菜鸟)

这对我来说是有效的,可以将字符串从C#发送到C ++ CLI

C#

bool result;
result = mps.Import(mpsToolName);

C ++ CLI

功能:

bool ManagedMPS::Import(System::String^ mpsToolNameTest)
std::string mpsToolName;
mpsToolName = toStandardString(mpsToolNameTest);

从将String ^转换为std :: string

的函数
static std::string toStandardString(System::String^ string)
{
 using System::Runtime::InteropServices::Marshal;
 System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
 char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
 std::string returnString(charPointer, string->Length);
 Marshal::FreeHGlobal(pointer);
 return returnString;
}

在进一步研究中,似乎更清洁,更安全。

我转而使用这种方法。

std::string Utils::ToUnmanagedString(String^ stringIncoming)
{
   std::string unmanagedString = marshal_as<std::string>(stringIncoming);
   return unmanagedString;
}

答案 5 :(得分:0)

创建可以使用的Windows运行时组件:

String^ systemString = "Hello";
std::wstring ws1(systemString ->Data());
std::string standardString(ws1.begin(), ws1.end());