IntPtr、構造体の変換

一般的には以下の基本的な変換時間よりも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)。
            } 
        }
            
            
                
            
                 

おすすめ

転載: www.cnblogs.com/wuyaxiansheng/p/12107859.html