This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / tests / pinvoke3.cs
1 //
2 // pinvoke3.cs:
3 //
4 //  Tests for native->managed marshalling
5 //
6
7 using System;
8 using System.Runtime.InteropServices;
9
10 public class Tests {
11
12         [StructLayout (LayoutKind.Sequential)]
13         public struct SimpleStruct {
14                 public bool a;
15                 public bool b;
16                 public bool c;
17                 public string d;
18         }
19
20         [StructLayout (LayoutKind.Sequential)]
21         public class SimpleClass {
22                 public bool a;
23                 public bool b;
24                 public bool c;
25                 public string d;
26         }
27
28         public static SimpleStruct delegate_test_struct (SimpleStruct ss)
29         {
30                 SimpleStruct res;
31
32                 res.a = !ss.a;
33                 res.b = !ss.b;
34                 res.c = !ss.c;
35                 res.d = ss.d + "-RES";
36
37                 return res;
38         }
39
40         public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
41         {
42                 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
43                         ss.a = true;
44                         ss.b = true;
45                         ss.c = true;
46                         ss.d = "TEST3";
47                         return 0;
48                 }
49
50                 return 1;
51         }
52
53         public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
54         {
55                 ss.a = true;
56                 ss.b = true;
57                 ss.c = true;
58                 ss.d = "TEST3";
59
60                 return 0;
61         }
62
63         public static SimpleClass delegate_test_class (SimpleClass ss)
64         {
65                 if (ss == null)
66                         return null;
67
68                 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
69                         return null;
70
71                 SimpleClass res = ss;
72
73                 return res;
74         }
75
76         public static int delegate_test_class_byref (ref SimpleClass ss)
77         {
78                 if (ss == null)
79                         return -1;
80
81                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
82                         ss.a = true;
83                         ss.b = false;
84                         ss.c = true;
85                         ss.d = "RES";
86
87                         return 0;
88                 }
89
90                 return 1;
91         }
92
93         public static int delegate_test_class_out (out SimpleClass ss)
94         {
95                 ss = new SimpleClass ();
96                 ss.a = true;
97                 ss.b = false;
98                 ss.c = true;
99                 ss.d = "RES";
100
101                 return 0;
102         }
103
104         public static int delegate_test_primitive_byref (ref int i)
105         {
106                 if (i != 1)
107                         return 1;
108                 
109                 i = 2;
110                 return 0;
111         }
112
113         public static int delegate_test_string_marshalling (string s)
114         {
115                 return s == "ABC" ? 0 : 1;
116         }
117
118         [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
119         public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
120
121         public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
122
123         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
124         public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
125         
126         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
127         public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
128
129         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
130         public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
131
132         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
133         public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
134
135         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
136         public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
137
138         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
139         public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
140
141         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
142         public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
143
144         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
145         public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
146
147         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
148         public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
149
150         [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
151         public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
152
153         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
154         public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
155
156         public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
157
158         public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
159
160         public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
161
162         public delegate int SimpleDelegate5 (ref SimpleClass ss);
163
164         public delegate int SimpleDelegate7 (out SimpleClass ss);
165
166         public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
167
168         public delegate int return_int_delegate (int i);
169
170         public delegate int SimpleDelegate9 (return_int_delegate del);
171
172         public delegate int PrimitiveByrefDelegate (ref int i);
173
174         public delegate return_int_delegate ReturnDelegateDelegate ();
175
176         public static int Main () {
177                 return TestDriver.RunTests (typeof (Tests));
178         }
179
180         /* Test structures as arguments and return values of delegates */
181         static int test_0_marshal_struct_delegate () {
182                 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
183
184                 return mono_test_marshal_delegate2 (d);
185         }
186
187         /* Test structures as byref arguments of delegates */
188         static int test_0_marshal_byref_struct_delegate () {
189                 SimpleStruct ss = new SimpleStruct ();
190                 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
191                 
192                 ss.b = true;
193                 ss.d = "TEST1";
194
195                 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
196                         return 1;
197
198                 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
199                         return 2;
200                 
201                 return 0;
202         }
203
204         /* Test structures as out arguments of delegates */
205         static int test_0_marshal_out_struct_delegate () {
206                 SimpleStruct ss = new SimpleStruct ();
207                 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
208
209                 return mono_test_marshal_out_struct (1, out ss, 2, d);
210         }
211
212         /* Test classes as arguments and return values of delegates */
213         static int test_0_marshal_class_delegate () {
214                 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
215
216                 return mono_test_marshal_delegate4 (d);
217         }
218
219         /* Test classes as byref arguments of delegates */
220         static int test_0_marshal_byref_class_delegate () {
221                 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
222
223                 return mono_test_marshal_delegate5 (d);
224         }
225
226         /* Test classes as out arguments of delegates */
227         static int test_0_marshal_out_class_delegate () {
228                 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
229
230                 return mono_test_marshal_delegate7 (d);
231         }
232
233         /* Test string marshalling with delegates */
234         static int test_0_marshal_string_delegate () {
235                 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
236
237                 return mono_test_marshal_delegate8 (d, "ABC");
238         }
239
240         /* Test that the delegate wrapper correctly catches null byref arguments */
241         static int test_0_marshal_byref_class_delegate_null () {
242                 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
243                 
244                 try {
245                         mono_test_marshal_delegate6 (d);
246                         return 1;
247                 }
248                 catch (ArgumentNullException ex) {
249                         return 0;
250                 }
251         }
252
253         static int return_self (int i) {
254                 return i;
255         }
256
257         static int call_int_delegate (return_int_delegate d) {
258                 return d (55);
259         }
260
261         static int test_55_marshal_delegate_delegate () {
262                 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
263
264                 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
265         }
266
267         static int test_0_marshal_delegate_delegate_unmanaged_ftn () {
268                 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
269
270                 try {
271                         mono_test_marshal_delegate10 (d);
272                         return 1;
273                 }
274                 catch (ArgumentException) {
275                         return 0;
276                 }
277
278                 return 2;
279         }
280
281         static int test_0_marshal_primitive_byref_delegate () {
282                 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
283
284                 return mono_test_marshal_primitive_byref_delegate (d);
285         }
286
287         public static return_int_delegate return_delegate () {
288                 return new return_int_delegate (return_self);
289         }
290
291         static int test_55_marshal_return_delegate_delegate () {
292                 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
293         }
294
295 }