Converting between std::string and .NET System::String in C++/CLI

In the software development world it seems that we spend most of our time converting from one data representation to another…..

So with that in mind, Here is a vary handy cheat sheet for how to go about converting to and from standard strings and .NET System::String when using C++/CLI/CLR:

Note that the include paths must be pre-pended by “msclr”, e.g.:


And the symbols live in this namespace: msclr::interop

std::string std_str = msclr::interop::marshal_as(sys_str);

When you #include these headers you may get loads of crazy compile errors, some involve ‘IServiceProvider’, to get rid of these try changing the include order of your headers, you may have to move these marshal includes up closer to the top of your include list.