Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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;



takamon9
C/C++,ATL/MFC,C#(.NET),Python,OpenCv,VBAが得意。他にOpenSSL,ICU,SQL Serverなどを利用したWindowsアプリケーション作成などを嗜む。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした