2 // Tests for Marshal.StructureToPtr and PtrToStructure
6 using System.Runtime.InteropServices;
11 [StructLayout (LayoutKind.Sequential)]
12 public class SimpleObj {
16 public void test () {}
19 [StructLayout (LayoutKind.Sequential)]
20 public struct SimpleStruct2 {
25 [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Ansi)]
26 public struct SimpleStruct {
31 [MarshalAs (UnmanagedType.ByValArray, SizeConst=2)] public short[] a1;
32 [MarshalAs (UnmanagedType.ByValTStr, SizeConst=4)] public string s1;
33 public SimpleStruct2 emb1;
34 public SimpleObj emb2;
37 [MarshalAs (UnmanagedType.ByValArray, SizeConst=2)] public char[] a2;
40 [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]
41 public struct ByValWStrStruct {
42 [MarshalAs (UnmanagedType.ByValTStr, SizeConst=4)] public string s1;
46 [StructLayout (LayoutKind.Sequential, Pack=1)]
47 public struct PackStruct1 {
51 [StructLayout (LayoutKind.Sequential)]
52 public struct PackStruct2 {
57 public unsafe static int Main (String[] args) {
58 if (TestDriver.RunTests (typeof (Tests), args) != 0)
63 public static int test_0_structure_to_ptr () {
64 SimpleStruct ss = new SimpleStruct ();
65 int size = Marshal.SizeOf (typeof (SimpleStruct));
70 IntPtr p = Marshal.AllocHGlobal (size);
75 ss.a1 = new short [2];
79 ss.emb1 = new SimpleStruct2 ();
82 ss.emb2 = new SimpleObj ();
85 ss.s2 = "just a test";
91 Marshal.StructureToPtr (ss, p, false);
92 Type t = ss.GetType ();
94 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
96 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
98 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
100 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
102 if (Marshal.ReadInt16 (p, 16) != 6)
104 if (Marshal.ReadInt16 (p, 18) != 5)
106 if (Marshal.ReadByte (p, 20) != 97)
108 if (Marshal.ReadByte (p, 21) != 98)
110 if (Marshal.ReadByte (p, 22) != 99)
112 if (Marshal.ReadByte (p, 23) != 0)
114 if (Marshal.ReadInt32 (p, 24) != 3)
116 if (Marshal.ReadInt32 (p, 28) != 4)
118 if (Marshal.ReadInt32 (p, 32) != 10)
120 if (Marshal.ReadInt32 (p, 36) != 11)
122 if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2")) != 97)
124 if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2") + 1) != 98)
127 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
132 if (cp.bool1 != true)
135 if (cp.bool2 != false)
162 if (cp.s2 != "just a test")
168 if (cp.a2 [0] != 'a')
171 if (cp.a2 [1] != 'b')
176 [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
177 public struct Struct1
179 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
180 public string Field1;
181 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
182 public string Field2;
183 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
184 public string Field3;
187 public static int test_0_byvaltstr_unicode () {
188 ByValWStrStruct s = new ByValWStrStruct ();
190 IntPtr p2 = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (ByValWStrStruct)));
191 Marshal.StructureToPtr(s, p2, false);
193 /* Check that the ByValWStr is initialized correctly */
194 for (int i = 0; i < 8; ++i)
195 if (Marshal.ReadByte (p2, i) != 0)
201 Marshal.StructureToPtr(s, p2, false);
203 ByValWStrStruct s2 = (ByValWStrStruct)Marshal.PtrToStructure (p2, typeof (ByValWStrStruct));
205 /* The fourth char is lost because of null-termination */
214 public static int test_0_byvaltstr_max_size () {
215 string buffer = "12345678123456789012345678901234";
217 IntPtr ptr = Marshal.StringToBSTR (buffer);
219 Struct1 data = (Struct1)Marshal.PtrToStructure (ptr, typeof (Struct1));
220 if (data.Field1 != "12345678")
222 if (data.Field2 != "1234567890")
224 if (data.Field3 != "12345678901234")
229 // Check that the 'Pack' directive on a struct changes the min alignment of the struct as well (#12110)
230 public static int test_0_struct_pack () {
231 if (Marshal.OffsetOf (typeof (PackStruct2), "s") != new IntPtr (1))