2 using System.Runtime.InteropServices;
7 [StructLayout (LayoutKind.Sequential)]
8 public class SimpleObj {
12 public void test () {}
15 [StructLayout (LayoutKind.Sequential)]
16 public struct SimpleStruct2 {
21 [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Ansi)]
22 public struct SimpleStruct {
27 [MarshalAs (UnmanagedType.ByValArray, SizeConst=2)] public short[] a1;
28 [MarshalAs (UnmanagedType.ByValTStr, SizeConst=4)] public string s1;
29 public SimpleStruct2 emb1;
30 public SimpleObj emb2;
35 public unsafe static int Main () {
36 SimpleStruct ss = new SimpleStruct ();
37 int size = Marshal.SizeOf (typeof (SimpleStruct));
39 Console.WriteLine ("SimpleStruct:" + size);
43 IntPtr p = Marshal.AllocHGlobal (size);
48 ss.a1 = new short [2];
52 ss.emb1 = new SimpleStruct2 ();
55 ss.emb2 = new SimpleObj ();
58 ss.s2 = "just a test";
61 Marshal.StructureToPtr (ss, p, false);
62 Type t = ss.GetType ();
64 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
66 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
68 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
70 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
72 if (Marshal.ReadInt16 (p, 16) != 6)
74 if (Marshal.ReadInt16 (p, 18) != 5)
76 if (Marshal.ReadByte (p, 20) != 97)
78 if (Marshal.ReadByte (p, 21) != 98)
80 if (Marshal.ReadByte (p, 22) != 99)
82 if (Marshal.ReadByte (p, 23) != 0)
84 if (Marshal.ReadInt32 (p, 24) != 3)
86 if (Marshal.ReadInt32 (p, 28) != 4)
88 if (Marshal.ReadInt32 (p, 32) != 10)
90 if (Marshal.ReadInt32 (p, 36) != 11)
92 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0)
95 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
100 if (cp.bool1 != true)
103 if (cp.bool2 != false)
130 if (cp.s2 != "just a test")