2013年6月27日 星期四

[C++/CLI]System::String^ to std::string 與 std::string to System::String^

前言

當我們在開發c++/cli視窗程式時,會遇到需要把System::String^ 格式轉成std::string 或是相反的情況,在這邊紀錄一下

System::String^ to std::string

以下式轉換的程式碼:
  • std::string to System::String^
  •  
    //Convert std::string to System::String^,os is std::string
    String^ str = gcnew String(os.c_str());
  • System::String^ to std::string
  • const char* chars = (const char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(source)).ToPointer();
     string dest = chars;

    參考資料

    沒有留言:

    張貼留言