2 using System.Reflection;
3 using System.Runtime.InteropServices;
7 [StructLayout(LayoutKind.Sequential, Size=32)]
8 public class TestStruct1
13 [StructLayout(LayoutKind.Sequential, Size=32)]
14 public class TestStruct2
16 [MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
21 [StructLayout(LayoutKind.Sequential, Size=32)]
22 public class TestStruct3 : TestStruct2
27 [StructLayout (LayoutKind.Sequential)]
28 public class TestStruct4
30 [MarshalAs (UnmanagedType.Interface)]
34 [StructLayout (LayoutKind.Sequential)]
35 public class TestStruct5
37 [MarshalAs (UnmanagedType.IUnknown)]
41 [StructLayout (LayoutKind.Sequential)]
42 public class TestStruct6
44 [MarshalAs (UnmanagedType.IDispatch)]
48 [StructLayout (LayoutKind.Sequential)]
49 public class TestStruct7
51 [MarshalAs (UnmanagedType.Struct)]
55 // Size should be 16 in both 32 and 64 bits win/linux
56 // Size should be 12 on 32bits OSX size alignment of long is 4
57 [StructLayout (LayoutKind.Explicit)]
65 // Size should be 12 in both 32 and 64 bits
66 [StructLayout (LayoutKind.Explicit, Size=12)]
74 // Size should be 16 in both 32 and 64 bits
75 // Size should be 12 on 32bits OSX size alignment of long is 4
76 [StructLayout (LayoutKind.Explicit)]
84 // Size should be 11 in both 32 and 64 bits
85 [StructLayout (LayoutKind.Explicit, Size=11)]
93 [StructLayout (LayoutKind.Explicit, Pack=1)]
101 // Size should always be 12, since pack = 0, size = 0 and min alignment = 4
102 //When pack is not set, we default to 8, so min (8, min alignment) -> 4
103 [StructLayout (LayoutKind.Explicit)]
104 struct TestStruct13 {
113 // Size should always be 12, since pack = 8, size = 0 and min alignment = 4
114 //It's aligned to min (pack, min alignment) -> 4
115 [StructLayout (LayoutKind.Explicit)]
116 struct TestStruct14 {
126 return (int)typeof (Environment).GetMethod ("get_Platform", BindingFlags.Static | BindingFlags.NonPublic).Invoke (null, null) == 6;
129 public unsafe static int Main ()
132 /// Testing simple struct size
134 if(Marshal.SizeOf(typeof(TestStruct1)) != 32)
139 TestStruct1 myStruct = new TestStruct1();
140 myStruct.a = 0x12345678;
142 IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1)));
143 Marshal.StructureToPtr(myStruct, p, false);
145 Type testType = typeof(TestStruct1);
146 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a)
149 Marshal.FreeHGlobal(p);
152 /// Testing struct size with ByValTStr string
154 if(Marshal.SizeOf(typeof(TestStruct2)) != 32)
157 TestStruct2 myStruct2 = new TestStruct2();
158 myStruct2.b = 0x12345678;
160 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2)));
161 Marshal.StructureToPtr(myStruct2, p, false);
163 Type testType2 = typeof(TestStruct2);
164 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b)
167 Marshal.FreeHGlobal(p);
170 /// Test structure size and struct with inheritance
172 if(Marshal.SizeOf(typeof(TestStruct3)) != 64)
175 TestStruct3 myStruct3 = new TestStruct3();
176 myStruct3.b = 0x12345678;
177 myStruct3.c = 0x76543210;
178 p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3)));
179 Marshal.StructureToPtr(myStruct3, p, false);
181 Type testType3 = typeof(TestStruct3);
183 if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b)
186 if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c)
189 Marshal.FreeHGlobal(p);
192 /// Also make sure OffsetOf returns the correct Exception.
195 Marshal.OffsetOf(testType3, "blah");
198 catch(ArgumentException e) {
205 // test size of structs with objects
206 if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size)
208 if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size)
210 if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size)
213 if (Marshal.SizeOf (typeof (TestStruct7)) != 16)
215 if (IsOSX () && IntPtr.Size == 4) {
216 if (Marshal.SizeOf (typeof (TestStruct8)) != 12)
218 if (Marshal.SizeOf (typeof (TestStruct10)) != 12)
221 if (Marshal.SizeOf (typeof (TestStruct8)) != 16 && Marshal.SizeOf (typeof (TestStruct8)) != 12)
223 if (Marshal.SizeOf (typeof (TestStruct10)) != 16 && Marshal.SizeOf (typeof (TestStruct10)) != 12)
226 if (Marshal.SizeOf (typeof (TestStruct9)) != 12)
228 if (Marshal.SizeOf (typeof (TestStruct11)) != 11)
230 if (Marshal.SizeOf (typeof (TestStruct12)) != 6)
232 if (Marshal.SizeOf (typeof (TestStruct13)) != 12)
234 if (Marshal.SizeOf (typeof (TestStruct14)) != 12)