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;
34 public unsafe static int Main () {
35 SimpleStruct ss = new SimpleStruct ();
36 int size = Marshal.SizeOf (typeof (SimpleStruct));
38 Console.WriteLine ("SimpleStruct:" + size);
42 IntPtr p = Marshal.AllocHGlobal (size);
47 ss.a1 = new short [2];
51 ss.emb1 = new SimpleStruct2 ();
54 ss.emb2 = new SimpleObj ();
57 ss.s2 = "just a test";
59 Marshal.StructureToPtr (ss, p, false);
60 Type t = ss.GetType ();
62 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
64 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
66 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
68 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
70 if (Marshal.ReadInt16 (p, 16) != 6)
72 if (Marshal.ReadInt16 (p, 18) != 5)
74 if (Marshal.ReadByte (p, 20) != 97)
76 if (Marshal.ReadByte (p, 21) != 98)
78 if (Marshal.ReadByte (p, 22) != 99)
80 if (Marshal.ReadByte (p, 23) != 0)
82 if (Marshal.ReadInt32 (p, 24) != 3)
84 if (Marshal.ReadInt32 (p, 28) != 4)
86 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "s2")) == 0)
89 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
97 if (cp.bool2 != false)
124 if (cp.s2 != "just a test")