예제 #1
0
        private static void TestMarshalStructAPIs()
        {
            Console.WriteLine("Testing Marshal APIs for structs");

            NonBlittableStruct ts = new NonBlittableStruct()
            {
                f1 = 100, f2 = true, f3 = false, f4 = true
            };
            int size = Marshal.SizeOf <NonBlittableStruct>(ts);

            ThrowIfNotEquals(16, size, "Marshal.SizeOf<NonBlittableStruct> failed");
            IntPtr memory = Marshal.AllocHGlobal(size);

            try
            {
                Marshal.StructureToPtr <NonBlittableStruct>(ts, memory, false);
                NonBlittableStruct ts2 = Marshal.PtrToStructure <NonBlittableStruct>(memory);
                ThrowIfNotEquals(true, ts2.f1 == 100 && ts2.f2 == true && ts2.f3 == false && ts2.f4 == true, "NonBlittableStruct marshalling Marshal API failed");

                IntPtr offset = Marshal.OffsetOf <NonBlittableStruct>("f2");
                ThrowIfNotEquals(new IntPtr(4), offset, "Struct marshalling OffsetOf failed.");
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            BlittableClass bc = new BlittableClass()
            {
                f1 = 100, f2 = 12345678, f3 = 999, f4 = -4
            };
            int bc_size = Marshal.SizeOf <BlittableClass>(bc);

            ThrowIfNotEquals(24, bc_size, "Marshal.SizeOf<BlittableClass> failed");
            IntPtr bc_memory = Marshal.AllocHGlobal(bc_size);

            try
            {
                Marshal.StructureToPtr <BlittableClass>(bc, bc_memory, false);
                BlittableClass bc2 = Marshal.PtrToStructure <BlittableClass>(bc_memory);
                ThrowIfNotEquals(true, bc2.f1 == 100 && bc2.f2 == 12345678 && bc2.f3 == 999 && bc2.f4 == -4, "BlittableClass marshalling Marshal API failed");
            }
            finally
            {
                Marshal.FreeHGlobal(bc_memory);
            }

            NonBlittableClass nbc = new NonBlittableClass()
            {
                f1 = false, f2 = true, f3 = 42
            };
            int nbc_size = Marshal.SizeOf <NonBlittableClass>(nbc);

            ThrowIfNotEquals(12, nbc_size, "Marshal.SizeOf<NonBlittableClass> failed");
            IntPtr nbc_memory = Marshal.AllocHGlobal(nbc_size);

            try
            {
                Marshal.StructureToPtr <NonBlittableClass>(nbc, nbc_memory, false);
                NonBlittableClass nbc2 = Marshal.PtrToStructure <NonBlittableClass>(nbc_memory);
                ThrowIfNotEquals(true, nbc2.f1 == false && nbc2.f2 == true && nbc2.f3 == 42, "NonBlittableClass marshalling Marshal API failed");
            }
            finally
            {
                Marshal.FreeHGlobal(nbc_memory);
            }
        }
예제 #2
0
파일: PInvoke.cs 프로젝트: yowl/corert
        private static void TestMarshalStructAPIs()
        {
            Console.WriteLine("Testing Marshal APIs for structs");

            BlittableStruct bs = new BlittableStruct()
            {
                FirstField = 1.0f, SecondField = 2.0f, ThirdField = 3
            };
            int bs_size = Marshal.SizeOf <BlittableStruct>(bs);

            ThrowIfNotEquals(40, bs_size, "Marshal.SizeOf<BlittableStruct> failed");
            IntPtr bs_memory = Marshal.AllocHGlobal(bs_size);

            try
            {
                Marshal.StructureToPtr <BlittableStruct>(bs, bs_memory, false);
                BlittableStruct bs2 = Marshal.PtrToStructure <BlittableStruct>(bs_memory);
                ThrowIfNotEquals(true, bs2.FirstField == 1.0f && bs2.SecondField == 2.0f && bs2.ThirdField == 3, "BlittableStruct marshalling Marshal API failed");

                IntPtr offset = Marshal.OffsetOf <BlittableStruct>("SecondField");
                ThrowIfNotEquals(new IntPtr(12), offset, "Struct marshalling OffsetOf failed.");
            }
            finally
            {
                Marshal.FreeHGlobal(bs_memory);
            }

            NonBlittableStruct ts = new NonBlittableStruct()
            {
                f1 = 100, f2 = true, f3 = false, f4 = true
            };
            int size = Marshal.SizeOf <NonBlittableStruct>(ts);

            ThrowIfNotEquals(16, size, "Marshal.SizeOf<NonBlittableStruct> failed");
            IntPtr memory = Marshal.AllocHGlobal(size);

            try
            {
                Marshal.StructureToPtr <NonBlittableStruct>(ts, memory, false);
                NonBlittableStruct ts2 = Marshal.PtrToStructure <NonBlittableStruct>(memory);
                ThrowIfNotEquals(true, ts2.f1 == 100 && ts2.f2 == true && ts2.f3 == false && ts2.f4 == true, "NonBlittableStruct marshalling Marshal API failed");

                IntPtr offset = Marshal.OffsetOf <NonBlittableStruct>("f2");
                ThrowIfNotEquals(new IntPtr(4), offset, "Struct marshalling OffsetOf failed.");
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            BlittableClass bc = new BlittableClass()
            {
                f1 = 100, f2 = 12345678, f3 = 999, f4 = -4
            };
            int bc_size = Marshal.SizeOf <BlittableClass>(bc);

            ThrowIfNotEquals(24, bc_size, "Marshal.SizeOf<BlittableClass> failed");
            IntPtr bc_memory = Marshal.AllocHGlobal(bc_size);

            try
            {
                Marshal.StructureToPtr <BlittableClass>(bc, bc_memory, false);
                BlittableClass bc2 = Marshal.PtrToStructure <BlittableClass>(bc_memory);
                ThrowIfNotEquals(true, bc2.f1 == 100 && bc2.f2 == 12345678 && bc2.f3 == 999 && bc2.f4 == -4, "BlittableClass marshalling Marshal API failed");
            }
            finally
            {
                Marshal.FreeHGlobal(bc_memory);
            }

            NonBlittableClass nbc = new NonBlittableClass()
            {
                f1 = false, f2 = true, f3 = 42
            };
            int nbc_size = Marshal.SizeOf <NonBlittableClass>(nbc);

            ThrowIfNotEquals(12, nbc_size, "Marshal.SizeOf<NonBlittableClass> failed");
            IntPtr nbc_memory = Marshal.AllocHGlobal(nbc_size);

            try
            {
                Marshal.StructureToPtr <NonBlittableClass>(nbc, nbc_memory, false);
                NonBlittableClass nbc2 = Marshal.PtrToStructure <NonBlittableClass>(nbc_memory);
                ThrowIfNotEquals(true, nbc2.f1 == false && nbc2.f2 == true && nbc2.f3 == 42, "NonBlittableClass marshalling Marshal API failed");
            }
            finally
            {
                Marshal.FreeHGlobal(nbc_memory);
            }

            int cftf_size = Marshal.SizeOf(typeof(ClassForTestingFlowAnalysis));

            ThrowIfNotEquals(4, cftf_size, "ClassForTestingFlowAnalysis marshalling Marshal API failed");
        }