C ++ .NET转换系统::字符串到std :: string的字符串、系统、NET、std

2023-09-02 21:54:02 作者:独特味道。

你怎么转换系统::字符串到std :: string的在C ++ .NET?

How do you convert System::String to std::string in C++ .NET?

推荐答案

有更清晰的语法,如果你使用的是最新版本的.NET

There is cleaner syntax if you're using a recent version of .net

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

#include <msclrmarshal_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;
}

这也为您提供了更好的清理工作异常的脸。

This also gives you better clean-up in the face of exceptions.

有一个 MSDN文章各种其他转换