2 using System.Runtime.InteropServices;
6 [StructLayout(LayoutKind.Sequential, Size=32)]
7 public class TestStruct1
12 [StructLayout(LayoutKind.Sequential, Size=32)]
13 public class TestStruct2
15 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
20 [StructLayout(LayoutKind.Sequential, Size=32)]
21 public class TestStruct3 : TestStruct2
26 [StructLayout (LayoutKind.Sequential)]
27 public class TestStruct4
29 [MarshalAs (UnmanagedType.Interface)]
33 [StructLayout (LayoutKind.Sequential)]
34 public class TestStruct5
36 [MarshalAs (UnmanagedType.IUnknown)]
40 [StructLayout (LayoutKind.Sequential)]
41 public class TestStruct6
43 [MarshalAs (UnmanagedType.IDispatch)]
47 [StructLayout (LayoutKind.Sequential)]
48 public class TestStruct7
50 [MarshalAs (UnmanagedType.Struct)]
54 // Size should be 12 in both 32 and 64 bits
55 [StructLayout (LayoutKind.Explicit)]
63 // Size should be 12 in both 32 and 64 bits
64 [StructLayout (LayoutKind.Explicit, Size=12)]
72 // Size should be 11 in both 32 and 64 bits
73 [StructLayout (LayoutKind.Explicit)]
81 // Size should be 11 in both 32 and 64 bits
82 [StructLayout (LayoutKind.Explicit, Size=11)]
90 [StructLayout (LayoutKind.Explicit, Pack=1)]
98 public unsafe static int Main ()
101 /// Testing simple struct size
103 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
108 TestStruct1 myStruct = new TestStruct1();
109 myStruct.a = 0x12345678;
111 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
112 Marshal.StructureToPtr(myStruct, p, false);
114 Type testType = typeof(TestStruct1);
115 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
118 Marshal.FreeHGlobal(p);
121 /// Testing struct size with ByValTStr string
123 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
126 TestStruct2 myStruct2 = new TestStruct2();
127 myStruct2.b = 0x12345678;
129 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
130 Marshal.StructureToPtr(myStruct2, p, false);
132 Type testType2 = typeof(TestStruct2);
133 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
136 Marshal.FreeHGlobal(p);
139 /// Test structure size and struct with inheritance
141 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
144 TestStruct3 myStruct3 = new TestStruct3();
145 myStruct3.b = 0x12345678;
146 myStruct3.c = 0x76543210;
147 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
148 Marshal.StructureToPtr(myStruct3, p, false);
150 Type testType3 = typeof(TestStruct3);
152 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
155 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c)
158 Marshal.FreeHGlobal(p);
161 /// Also make sure OffsetOf returns the correct Exception.
164 Marshal.OffsetOf(testType3, "blah");
167 catch(ArgumentException e) {
174 // test size of structs with objects
175 if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size)
177 if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size)
179 if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size)
182 if (Marshal.SizeOf (typeof (TestStruct7)) != 16)
184 if (Marshal.SizeOf (typeof (TestStruct8)) != 16)
186 if (Marshal.SizeOf (typeof (TestStruct9)) != 12)
188 if (Marshal.SizeOf (typeof (TestStruct10)) != 16)
190 if (Marshal.SizeOf (typeof (TestStruct11)) != 11)
192 if (Marshal.SizeOf (typeof (TestStruct12)) != 6)