Пример #1
0
        public static IntPtr AllocateIntPtr <T>(T t)
        {
            int    num    = Marshal.SizeOf(t);
            IntPtr intPtr = Marshal.AllocHGlobal(num);

            if (intPtr != IntPtr.Zero)
            {
                MemoryWrapper.FillMemory(intPtr, (uint)num, 0);
                Marshal.StructureToPtr(t, intPtr, false);
            }
            return(intPtr);
        }
Пример #2
0
        public static byte[] StructToByteArray <T>(T t, int byteArraySize)
        {
            IntPtr intPtr = IntPtr.Zero;

            if (Marshal.SizeOf(t) > byteArraySize)
            {
                return(null);
            }
            byte[] array = new byte[byteArraySize];
            intPtr = Marshal.AllocHGlobal(byteArraySize);
            MemoryWrapper.FillMemory(intPtr, (uint)byteArraySize, 0);
            Marshal.StructureToPtr(t, intPtr, false);
            Marshal.Copy(intPtr, array, 0, byteArraySize);
            Marshal.FreeHGlobal(intPtr);
            return(array);
        }