.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

C++/CLI std::string to System::String


Does anybody know of a way to convert a C++ std:string to a .NET System::String^ , obviously in C++/CLI . I've tried

std::string ns("Hello");
String^ ms(ns.c_str());


std::wstring ns("Hello");
String^ ms(ns.c_str());

just in case it's a unicode issue. But to no avail. There's loads of examples on how to go the other way managed->native but I can't see anything for native->managed.

I'd really appreciate it if anybody has the answer!!


Thursday, August 16, 2007
Yeah I had to research that for a project I just did.

std::string ns("Hello");
String^ ms(ns.c_str());

ms = Marshal::PtrToStringAnsi(static_cast<IntPtr>(const_cast<char *>(ns.c_str())))
Thursday, August 16, 2007
forgot to add

using namespace System::Runtime::InteropServices;
Thursday, August 16, 2007
This works for me, and sees regular use in production code :

System::String^ NativeToDotNet( const std::string& input )
return System::Runtime::InteropServices::Marshal::PtrToStringAnsi( static_cast<LPVOID>( input.c_str() ) );

Should be the same as nullptr's example, really.
Ritchie Swann Send private email
Friday, August 17, 2007
works a charm, thanks guys.

Why MS didn't include string conversion functions as standard in the interop library is a mystery.

Friday, August 17, 2007

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz