====== ネイティブデータとCLRの混在 ====== C++/CLIを使う理由の1つは既存のCライブラリを使うことです。このとき既存ライブラリのデータをCLRアプリでどう扱うか、という問題があります。 ===== ref classの中にポインタで持つ ===== ref classはネイティブクラスの実体をもつことはできませんが、ポインタなら持つことができます。ただしref classはその開放方法が様々なので、そのポインタをどうやって破棄するのかを考えておかなければなりません。 #include #include using namespace System; ref class REFCLASS { void clearp() { if (p) { free((void*)p); p = NULL; } } public: char* p; ~REFCLASS() { clearp(); } !REFCLASS() { clearp(); } }; int main(array ^args) { { REFCLASS c; c.p = strdup("AAA"); Console::WriteLine(gcnew String(c.p)); REFCLASS^ hc = gcnew REFCLASS; hc->p = strdup("BBB"); Console::WriteLine(gcnew String(hc->p)); } return 0; } ===== C++のclassの中にgcrootで持つ ===== gcrootを使うと、ネイティブクラスの中にハンドルを持つことができます。 #include using namespace System; class NativeClass { public: gcroot clrstring; }; int main(array ^args) { String^ s = L"AAA"; NativeClass n; n.clrstring = s; Console::WriteLine(n.clrstring); return 0; } gcrootはC++クラスでメンバはvoid* handle;だけです。これはSystem::Runtime::InteropServices::GCHandle::Alloc()の戻り値をキャストしたものです。