String^ to string 與 string to String^
String^ to string
//Convert std::string to System::String^,os is std::string String^ str = gcnew String(os.c_str());
string to String^
第一種方式
//In here source is System::String^ const char* chars = (const char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(source)).ToPointer(); string dest = chars;
第二種方式:若出現'^' 不能在類型'std::array'上使用這個間接取值之類的錯誤時,可以寫成函式來轉換
//In here source is System::String^
void MarshalString(System::String^ s, std::string& os)
{
using namespace System::Runtime::InteropServices;
const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(System::IntPtr((void*)chars));
}
留言
張貼留言