/// Copies the contents of a managed System::String^ into a std::string
/**
* @preconditions
* The provided managed string must be initialized.
* @postconditions
* None
* @param string
* The managed string to convert
* @return
* A std::string created from the managed string
* @throw
* This method will throw a NullReferenceException if the input is null
*/
static std::string toStandardString(System::String^ string)
{
using System::Runtime::InteropServices::Marshal;
System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
std::string returnString(charPointer, string->Length);
Marshal::FreeHGlobal(pointer);
return returnString;
}
Sunday, April 17, 2011
C++/CLI: System::String^ to std::string
Subscribe to:
Post Comments (Atom)
http://stackoverflow.com/questions/16113193/efficiency-with-c-cli-managed-string-conversion?noredirect=1#comment23017379_16113193
ReplyDelete