C++/CLI Converting from System::String^ to std::string

sivabudh picture sivabudh · Jun 3, 2009 · Viewed 148.6k times · Source

Can someone please post a simple code that would convert,

System::String^

To,

C++ std::string

I.e., I just want to assign the value of,

String^ originalString;

To,

std::string newString;

Answer

tragomaskhalos picture tragomaskhalos · Sep 10, 2009

Don't roll your own, use these handy (and extensible) wrappers provided by Microsoft.

For example:

#include <msclr\marshal_cppstd.h>

System::String^ managed = "test";
std::string unmanaged = msclr::interop::marshal_as<std::string>(managed);