I want to convert to std::string to System::String^ in Visual C++ environment. I know that we can convert System::String to std::string by the MarshalString
Function as below:
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
I can't find the way to convert std::string to System::String but I found that System::String has constructor with argument as below :
System::String(Char* value, Int32 startIndex, Int32 length)
and i try to use code like below, but it can not give me a correct solution :
std::string str1 = "MyString";
System::String^ str = new System::String(str1.c_str(), 0, str1.length());
What wrong happen in my code?