一般的には以下の基本的な変換時間よりもC#のコードを書いのみ++の使用のみで、DLLの動的ライブラリCを呼び出します
構造体のターンのIntPtr
パブリック 静的のIntPtr StructToIntPtr <T>(T REQ)ここで、 T:構造体 { int型のサイズ= Marshal.SizeOf(REQ)。 バイト []バイト= 新しい バイト[サイズ]。 IntPtr structPtr = Marshal.AllocHGlobal(サイズ)。 Marshal.StructureToPtr(REQ、structPtr、偽); Marshal.Copy(structPtr、バイト、0 、サイズ)。 返すstructPtrを。 }
(Marshal.FreeHGlobal(のIntPtr);)私は、このアプリケーションメモリの解放のIntPtr不足して覚えています
IntPtrターン構造体
パブリック 静的 T IntPtrToStruct <T>(のIntPtr PTR)T:構造体 { オブジェクト T = Marshal.PtrToStructure(PTR、typeof演算(T))。 Marshal.FreeHGlobal(PTR)。場合(TはあるT T1) { 戻りT1。 } 他 { 戻りデフォルト(T)。 } }