In .:
[mono.git] / mono / tests / marshal2.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 public class Test {
5
6
7         [StructLayout (LayoutKind.Sequential)]
8         public class SimpleObj {
9                 public int a;
10                 public int b;
11
12                 public void test () {}
13         }
14
15         [StructLayout (LayoutKind.Sequential)]
16         public struct SimpleStruct2 {
17                 public int a;
18                 public int b;
19         }
20         
21         [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Ansi)]
22         public struct SimpleStruct {
23                 public int a;
24                 public bool bool1;
25                 public bool bool2;
26                 public int b;
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;
31                 public string s2;
32                 public double x;
33         }
34         
35         public unsafe static int Main () {
36                 SimpleStruct ss = new SimpleStruct ();
37                 int size = Marshal.SizeOf (typeof (SimpleStruct));
38                 
39                 Console.WriteLine ("SimpleStruct:" + size);
40                 //if (size != 52)
41                 //return 1;
42                 
43                 IntPtr p = Marshal.AllocHGlobal (size);
44                 ss.a = 1;
45                 ss.bool1 = true;
46                 ss.bool2 = false;
47                 ss.b = 2;
48                 ss.a1 = new short [2];
49                 ss.a1 [0] = 6;
50                 ss.a1 [1] = 5;
51                 ss.s1 = "abcd";
52                 ss.emb1 = new SimpleStruct2 ();
53                 ss.emb1.a = 3;
54                 ss.emb1.b = 4;
55                 ss.emb2 = new SimpleObj ();
56                 ss.emb2.a = 10;
57                 ss.emb2.b = 11;
58                 ss.s2 = "just a test";
59                 ss.x = 1.5;
60                 
61                 Marshal.StructureToPtr (ss, p, false);
62                 Type t = ss.GetType ();
63                 
64                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "a")) != 1)
65                         return 1;
66                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool1")) != 1)
67                         return 2;
68                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "bool2")) != 0)
69                         return 3;
70                 if (Marshal.ReadInt32 (p, (int)Marshal.OffsetOf (t, "b")) != 2)
71                         return 4;
72                 if (Marshal.ReadInt16 (p, 16) != 6)
73                         return 5;
74                 if (Marshal.ReadInt16 (p, 18) != 5)
75                         return 6;
76                 if (Marshal.ReadByte (p, 20) != 97)
77                         return 7;
78                 if (Marshal.ReadByte (p, 21) != 98)
79                         return 8;
80                 if (Marshal.ReadByte (p, 22) != 99)
81                         return 9;
82                 if (Marshal.ReadByte (p, 23) != 0)
83                         return 10;
84                 if (Marshal.ReadInt32 (p, 24) != 3)
85                         return 11;
86                 if (Marshal.ReadInt32 (p, 28) != 4)
87                         return 12;
88                 if (Marshal.ReadInt32 (p, 32) != 10)
89                         return 13;
90                 if (Marshal.ReadInt32 (p, 36) != 11)
91                         return 14;
92
93                 SimpleStruct cp = (SimpleStruct)Marshal.PtrToStructure (p, ss.GetType ());
94
95                 if (cp.a != 1)
96                         return 16;
97
98                 if (cp.bool1 != true)
99                         return 17;
100
101                 if (cp.bool2 != false)
102                         return 18;
103
104                 if (cp.b != 2)
105                         return 19;
106
107                 if (cp.a1 [0] != 6)
108                         return 20;
109                 
110                 if (cp.a1 [1] != 5)
111                         return 21;
112
113                 if (cp.s1 != "abc")
114                         return 22;
115                 
116                 if (cp.emb1.a != 3)
117                         return 23;
118
119                 if (cp.emb1.b != 4)
120                         return 24;
121
122                 if (cp.emb2.a != 10)
123                         return 25;
124
125                 if (cp.emb2.b != 11)
126                         return 26;
127
128                 if (cp.s2 != "just a test")
129                         return 27;
130
131                 if (cp.x != 1.5)
132                         return 28;
133                 
134                 return 0;
135         }
136 }
137