1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

System::Stringから普通のstringへの変換

Last updated at Posted at 2017-02-21

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;



1
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?