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 public unsafe static int Main (String[] args) {
47 if (TestDriver.RunTests (typeof (Tests), args) != 0)
52 public static int test_0_structure_to_ptr () {
53 SimpleStruct ss = new SimpleStruct ();
54 int size = Marshal.SizeOf (typeof (SimpleStruct));
59 IntPtr p = Marshal.AllocHGlobal (size);
64 ss.a1 = new short [2];
68 ss.emb1 = new SimpleStruct2 ();
71 ss.emb2 = new SimpleObj ();
74 ss.s2 = "just a test";
80 Marshal.StructureToPtr (ss, p, false);
81 Type t = ss.GetType ();
83 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
85 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
87 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
89 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
91 if (Marshal.ReadInt16 (p, 16) != 6)
93 if (Marshal.ReadInt16 (p, 18) != 5)
95 if (Marshal.ReadByte (p, 20) != 97)
97 if (Marshal.ReadByte (p, 21) != 98)
99 if (Marshal.ReadByte (p, 22) != 99)
101 if (Marshal.ReadByte (p, 23) != 0)
103 if (Marshal.ReadInt32 (p, 24) != 3)
105 if (Marshal.ReadInt32 (p, 28) != 4)
107 if (Marshal.ReadInt32 (p, 32) != 10)
109 if (Marshal.ReadInt32 (p, 36) != 11)
111 if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2")) != 97)
113 if (Marshal.ReadByte (p, (int)Marshal.OffsetOf (t, "a2") + 1) != 98)
116 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
121 if (cp.bool1 != true)
124 if (cp.bool2 != false)
151 if (cp.s2 != "just a test")
157 if (cp.a2 [0] != 'a')
160 if (cp.a2 [1] != 'b')
165 [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
166 public struct Struct1
168 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
169 public string Field1;
170 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
171 public string Field2;
172 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
173 public string Field3;
176 public static int test_0_byvaltstr_unicode () {
177 ByValWStrStruct s = new ByValWStrStruct ();
179 IntPtr p2 = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (ByValWStrStruct)));
180 Marshal.StructureToPtr(s, p2, false);
182 /* Check that the ByValWStr is initialized correctly */
183 for (int i = 0; i < 8; ++i)
184 if (Marshal.ReadByte (p2, i) != 0)
190 Marshal.StructureToPtr(s, p2, false);
192 ByValWStrStruct s2 = (ByValWStrStruct)Marshal.PtrToStructure (p2, typeof (ByValWStrStruct));
194 /* The fourth char is lost because of null-termination */
203 public static int test_0_byvaltstr_max_size () {
204 string buffer = "12345678123456789012345678901234";
206 IntPtr ptr = Marshal.StringToBSTR (buffer);
208 Struct1 data = (Struct1)Marshal.PtrToStructure (ptr, typeof (Struct1));
209 if (data.Field1 != "12345678")
211 if (data.Field2 != "1234567890")
213 if (data.Field3 != "12345678901234")