この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
ネイティブデータとclrの混在 [2009/02/01 08:21] 192.168.0.61 |
— (現在) | ||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== ネイティブデータとCLRの混在 ====== | ||
- | C++/CLIを使う理由の1つは既存のCライブラリを使うことです。このとき既存ライブラリのデータをCLRアプリでどう扱うか、という問題があります。 | ||
- | |||
- | ===== ref classの中にポインタで持つ ===== | ||
- | ref classはネイティブクラスの実体をもつことはできませんが、ポインタなら持つことができます。ただしref classはその開放方法が様々なので、そのポインタをどうやって破棄するのかを考えておかなければなりません。 | ||
- | <code cpp> | ||
- | #include <string.h> | ||
- | #include <stdlib.h> | ||
- | 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<System::String ^> ^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; | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | |||
- | ===== C++のclassの中にgcrootで持つ ===== | ||
- | gcrootを使うと、ネイティブクラスの中にハンドルを持つことができます。 | ||
- | <code cpp> | ||
- | #include <gcroot.h> | ||
- | using namespace System; | ||
- | |||
- | class NativeClass | ||
- | { | ||
- | public: | ||
- | gcroot<String^> clrstring; | ||
- | }; | ||
- | |||
- | |||
- | int main(array<System::String ^> ^args) | ||
- | { | ||
- | String^ s = L"AAA"; | ||
- | NativeClass n; | ||
- | n.clrstring = s; | ||
- | |||
- | Console::WriteLine(n.clrstring); | ||
- | return 0; | ||
- | } | ||
- | </code> | ||
- | gcrootはC++クラスでメンバはvoid* handle;だけです。これはSystem::Runtime::InteropServices::GCHandle::Alloc()の戻り値をキャストしたものです。 |