Search code examples
c++managed-c++

Convert from System::String^ to System::String


I have a standard library string, and I would like to make the following conversion:

System::String^ to std::string


Solution

  • Code fragment to convert std::string to System::String^:

    #include <msclr/marshal.h>
    
    std::string str = "Hello World";
    System::String^ result = msclr::interop::marshal_as<System::String^>(str.c_str());
    

    Code fragment to convert System::String^ to std::string:

    #include <msclr/marshal.h>
    
    System::String^ str = "Hello World";
    std::string result = msclr::interop::marshal_as<std::string>(str);