System::String から std::stringへの変換方法
C++でフォームアプリケーションを作る際に必要になる。
下のnoteの部分を普通のstringにする場合を考える。
テキストボックス1(textbox1)の内容を普通のstring形式に変換したい。
System::String^ note = this->textbox1->Text;
単純に考えて、string a = note.ToString();を試すが、普通のストリングには変換できない。
どうしたらよいか?
using namespace std;
using namespace System;
void MarshalString ( String^ sys_string, string& std_string ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(sys_string)).ToPointer();
std_string = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
////////フォームにテキストボックス1がある場合 ///////
string normal_string;
System::String^ note = this->textbox1->Text;
System::String^ app_text = gcnew System::String("abc" + note);//System::String同士を連結する
MarshalString(note,normal_string);
cout << normal_string << endl;
MarshalString(app_text,normal_string);
cout << normal_string << endl;