bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

C++ CLIの文字列変換System::Stringからstd::stringへ

System::Stringをstd::stringに変換する必要が出たので調べた。
パクリ元
http://vene.wankuma.com/prog/CppCli_strings.aspx#ss2mb

下記を追加

using namespace System::Runtime::InteropServices;

こんな感じのコード

std::string tmp;
IntPtr mptr = Marshal::StringToHGlobalAnsi(textBox_MyIP->Text);
tmp = static_cast<const char*>(mptr.ToPointer());
m_RecvAddr->sin_addr.S_un.S_addr = inet_addr(tmp.c_str());
Marshal::FreeHGlobal(mptr);