Casting from managed to unmanaged types and v.s (Managed C++)

  • Thread starter Thread starter Marlon R
  • Start date Start date
M

Marlon R

How can I go from :
1) .NET String to char *
1) .NET String to char[]
2) char[] to .NET String
2) char* to .NET String
 
System::String * ToNetString(const std::string & ss)
{
if (ss.empty())
{
return(new System::String(S""));
}
System::IntPtr ptr(static_cast<System::IntPtr>(static_cast<void
*>(const_cast<char *>(ss.c_str()))));
System::String *
ret(System::Runtime::InteropServices::Marshal::PtrToStringAnsi(ptr));
return(ret);
}

std::string ToCppString(System::String * str)
{
if (str == 0)
{
return(std::string());
}
System::IntPtr
ptr(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str));
std::string ret(static_cast<const char *>(static_cast<void *>(ptr)));
System::Runtime::InteropServices::Marshal::FreeCoTaskMem(ptr);
return(ret);
}
 
Back
Top