46176ec5d33159ba6a9b46ff159fd3d0c8363658
[mono.git] / mono / tests / pinvoke2.cs
1 //
2 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
3 //
4
5 using System;
6 using System.Text;
7 using System.Runtime.InteropServices;
8 using System.Runtime.CompilerServices;
9 using System.Reflection.Emit;
10
11 public class Tests {
12
13         public int int_field;
14
15         public static int delegate_test (int a)
16         {
17                 if (a == 2)
18                         return 0;
19
20                 return 1;
21         }
22
23         public int delegate_test_instance (int a)
24         {
25                 return int_field + a;
26         }
27         
28         [StructLayout (LayoutKind.Sequential)]
29         public struct SimpleStruct {
30                 public bool a;
31                 public bool b;
32                 public bool c;
33                 public string d;
34                 [MarshalAs(UnmanagedType.LPWStr)]
35                 public string d2;
36         }
37
38         [StructLayout (LayoutKind.Sequential)]
39         public struct SimpleStructGen<T> {
40                 public bool a;
41                 public bool b;
42                 public bool c;
43                 public string d;
44                 [MarshalAs(UnmanagedType.LPWStr)]
45                 public string d2;
46         }
47
48         [StructLayout (LayoutKind.Sequential)]
49         public struct SimpleStruct2 {
50                 public bool a;
51                 public bool b;
52                 public bool c;
53                 public string d;
54                 public byte e;
55                 public double f;
56                 public byte g;
57                 public long h;
58         }
59
60         [StructLayout (LayoutKind.Sequential, Size=0)]
61         public struct EmptyStruct {
62         }
63
64         [StructLayout (LayoutKind.Sequential, Size=1)]
65         public struct EmptyStructCpp {
66         }
67
68         [StructLayout (LayoutKind.Sequential)]
69         public struct DelegateStruct {
70                 public int a;
71                 public SimpleDelegate del;
72                 [MarshalAs(UnmanagedType.FunctionPtr)] 
73                 public SimpleDelegate del2;
74                 [MarshalAs(UnmanagedType.FunctionPtr)] 
75                 public SimpleDelegate del3;
76         }
77
78         [StructLayout (LayoutKind.Sequential)]
79         public struct SingleDoubleStruct {
80                 public double d;
81         }
82
83         /* sparcv9 has complex conventions when passing structs with doubles in them 
84            by value, some simple tests for them */
85         [StructLayout (LayoutKind.Sequential)]
86         public struct Point {
87                 public double x;
88                 public double y;
89         }
90
91         [StructLayout (LayoutKind.Sequential)]
92         public struct MixedPoint {
93                 public int x;
94                 public double y;
95         }
96         
97         [StructLayout (LayoutKind.Sequential)]
98         public struct TinyStruct {
99                 public TinyStruct (int i)
100                 {
101                         this.i = i;
102                 }
103                 public int i;
104         }
105
106         [StructLayout (LayoutKind.Sequential)]
107         public class SimpleClass {
108                 public bool a;
109                 public bool b;
110                 public bool c;
111                 public string d;
112                 public byte e;
113                 public double f;
114                 public byte g;
115                 public long h;
116         }
117
118         [StructLayout (LayoutKind.Sequential)]
119         public class EmptyClass {
120         }
121
122         [StructLayout (LayoutKind.Sequential)]
123         public struct LongAlignStruct {
124                 public int a;
125                 public long b;
126                 public long c;
127         }
128
129         [StructLayout(LayoutKind.Sequential)]
130         public class BlittableClass
131         {
132                 public int a = 1;
133                 public int b = 2;
134         }
135
136         [StructLayout (LayoutKind.Sequential)]
137         class SimpleObj
138         {
139                 public string str;
140                 public int i;
141         }
142
143         [StructLayout(LayoutKind.Sequential)]
144         struct AsAnyStruct
145         {
146                 public int i;
147                 public int j;
148                 public int k;
149                 public String s;
150
151                 public AsAnyStruct(int i, int j, int k, String s) {
152                         this.i = i;
153                         this.j = j;
154                         this.k = k;
155                         this.s = s;
156                 }
157         }
158
159         [StructLayout(LayoutKind.Sequential)]
160         class AsAnyClass
161         {
162                 public int i;
163                 public int j;
164                 public int k;
165                 public String s;
166
167                 public AsAnyClass(int i, int j, int k, String s) {
168                         this.i = i;
169                         this.j = j;
170                         this.k = k;
171                 }
172         }
173
174         [DllImport ("libnot-found", EntryPoint="not_found")]
175         public static extern int mono_library_not_found ();
176
177         [DllImport ("libtest", EntryPoint="not_found")]
178         public static extern int mono_entry_point_not_found ();
179
180         [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
181         public static extern int mono_test_marshal_char_2 (char a1);
182
183         [DllImport ("test", EntryPoint="mono_test_marshal_char")]
184         public static extern int mono_test_marshal_char_3 (char a1);
185
186         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
187         public static extern int mono_test_marshal_char (char a1);
188
189         [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
190         public static extern int mono_test_marshal_char_array (char[] a1);
191
192         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
193         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
194
195         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
196         public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
197
198         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
199         public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
200
201         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
202         public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
203
204         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
205         public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
206
207         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
208         public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
209
210         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
211         public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
212
213         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
214         public static extern int mono_test_marshal_array (int [] a1);
215
216         [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
217         public static extern int mono_test_marshal_empty_string_array (string [] a1);
218
219         [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
220         public static extern int mono_test_marshal_string_array (string [] a1);
221
222         [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
223         public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
224
225         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
226         public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);   
227
228         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
229         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
230
231         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
232         public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
233
234         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_byref_array_out_size_param")]
235         public static extern int mono_test_marshal_out_byref_array_out_size_param ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out int [] a1, out int n);
236
237         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
238         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
239         
240         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
241         public static extern int mono_test_marshal_struct (SimpleStruct ss);
242         
243         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
244         public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
245
246         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
247         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
248
249         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
250         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
251
252         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
253         public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
254
255         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
256         public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
257
258         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
259         public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
260
261         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
262         public static extern int mono_test_marshal_point (Point p);
263
264         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
265         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
266
267         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
268         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
269
270         [DllImport ("libtest", EntryPoint="mono_test_return_empty_struct")]
271         public static extern EmptyStruct mono_test_return_empty_struct (int a);
272
273         [DllImport ("libtest", EntryPoint="mono_test_return_empty_struct")]
274         public static extern EmptyStructCpp mono_test_return_empty_struct_cpp (int a);
275
276         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
277         public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
278
279         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
280         public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
281
282         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
283         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
284
285         [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
286         public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
287
288         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
289         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
290
291         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
292         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
293
294         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
295         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
296
297         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
298         public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
299
300         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
301         public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
302
303         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
304         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
305
306         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
307         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
308
309         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
310         public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
311
312         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_append")]
313         public static extern void mono_test_marshal_stringbuilder_append (StringBuilder sb, int len);
314
315         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
316         public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
317
318         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
319         public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
320
321         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
322         public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
323
324         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
325         public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
326
327         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
328         public static extern void mono_test_last_error (int err);
329
330         [DllImport ("libtest", EntryPoint="mono_test_asany")]
331         public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
332
333         [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
334         public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
335
336         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
337         static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj); 
338
339         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
340         static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj); 
341         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
342         static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj); 
343
344         [DllImport ("libtest")]
345         static extern int class_marshal_test0 (SimpleObj obj);
346
347         [DllImport ("libtest")]
348         static extern void class_marshal_test1 (out SimpleObj obj);
349
350         [DllImport ("libtest")]
351         static extern int class_marshal_test4 (SimpleObj obj);
352         
353         [DllImport ("libtest")]
354         static extern int string_marshal_test0 (string str);
355
356         [DllImport ("libtest")]
357         static extern void string_marshal_test1 (out string str);
358
359         [DllImport ("libtest")]
360         static extern int string_marshal_test2 (ref string str);
361
362         [DllImport ("libtest")]
363         static extern int string_marshal_test3 (string str);
364
365         public delegate int SimpleDelegate (int a);
366
367         public static int Main (string[] args) {
368                 return TestDriver.RunTests (typeof (Tests), args);
369         }
370
371         public static int test_0_marshal_char () {
372                 return mono_test_marshal_char ('a');
373         }
374
375         public static int test_0_marshal_char_array () {
376                 // a unicode char[] is implicitly marshalled as [Out]
377                 char[] buf = new char [32];
378                 mono_test_marshal_char_array (buf);
379                 string s = new string (buf);
380                 if (s.StartsWith ("abcdef"))
381                         return 0;
382                 else
383                         return 1;
384         }
385
386         public static int test_1225_marshal_array () {
387                 int [] a1 = new int [50];
388                 for (int i = 0; i < 50; i++)
389                         a1 [i] = i;
390
391                 return mono_test_marshal_array (a1);
392         }
393
394         public static int test_1225_marshal_inout_array () {
395                 int [] a1 = new int [50];
396                 for (int i = 0; i < 50; i++)
397                         a1 [i] = i;
398
399                 int res = mono_test_marshal_inout_array (a1);
400
401                 for (int i = 0; i < 50; i++)
402                         if (a1 [i] != 50 - i) {
403                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
404                                 return 2;
405                         }
406
407                 return res;
408         }
409
410         public static int test_0_marshal_out_array () {
411                 int [] a1 = new int [50];
412
413                 int res = mono_test_marshal_out_array (a1, 0);
414
415                 for (int i = 0; i < 50; i++)
416                         if (a1 [i] != i) {
417                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
418                                 return 2;
419                         }
420
421                 return 0;
422         }
423
424         public static int test_0_marshal_out_byref_array_out_size_param () {
425                 int [] a1 = null;
426                 int len;
427
428                 int res = mono_test_marshal_out_byref_array_out_size_param (out a1, out len);
429                 if (len != 4)
430                         return 1;
431                 for (int i = 0; i < len; i++)
432                         if (a1 [i] != i)
433                                 return 2;
434                 return 0;
435         }
436
437         public static int test_0_marshal_inout_nonblittable_array () {
438                 char [] a1 = new char [10];
439                 for (int i = 0; i < 10; i++)
440                         a1 [i] = "Hello, World" [i];
441
442                 int res = mono_test_marshal_inout_nonblittable_array (a1);
443
444                 for (int i = 0; i < 10; i++)
445                         if (a1 [i] != 'F')
446                                 return 2;
447
448                 return res;
449         }
450
451         public static int test_0_marshal_struct () {
452                 SimpleStruct ss = new  SimpleStruct ();
453                 ss.b = true;
454                 ss.d = "TEST";
455                 
456                 return mono_test_marshal_struct (ss);
457         }
458
459         public static int test_0_marshal_struct_gen () {
460                 SimpleStructGen<string> ss = new  SimpleStructGen<string> ();
461                 ss.b = true;
462                 ss.d = "TEST";
463                 
464                 return mono_test_marshal_struct_gen (ss);
465         }
466
467         public static int test_0_marshal_struct2 () {
468                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
469                 ss2.b = true;
470                 ss2.d = "TEST";
471                 ss2.e = 99;
472                 ss2.f = 1.5;
473                 ss2.g = 42;
474                 ss2.h = 123L;
475
476                 return mono_test_marshal_struct2 (ss2);
477         }
478
479         public static int test_0_marshal_struct3 () {
480                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
481                 ss2.b = true;
482                 ss2.d = "TEST";
483                 ss2.e = 99;
484                 ss2.f = 1.5;
485                 ss2.g = 42;
486                 ss2.h = 123L;
487
488                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
489         }
490
491         public static int test_0_marshal_empty_struct () {
492                 EmptyStruct es = new EmptyStruct ();
493
494                 if (mono_test_empty_struct (1, es, 2) != 0)
495                         return 1;
496
497                 mono_test_return_empty_struct (42);
498
499                 return 0;
500         }
501
502         public static int test_0_marshal_empty_struct_cpp () {
503                 EmptyStructCpp es = new EmptyStructCpp ();
504
505                 mono_test_return_empty_struct_cpp (42);
506                 
507                 return 0;
508         }
509
510         public static int test_0_marshal_lpstruct () {
511                 SimpleStruct ss = new  SimpleStruct ();
512                 ss.b = true;
513                 ss.d = "TEST";
514                 
515                 return mono_test_marshal_lpstruct (ss);
516         }
517
518         public static int test_0_marshal_lpstruct_blittable () {
519                 Point p = new Point ();
520                 p.x = 1.0;
521                 p.y = 2.0;
522                 
523                 return mono_test_marshal_lpstruct_blittable (p);
524         }
525
526         public static int test_0_marshal_struct_array () {
527                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
528
529                 SimpleStruct2 ss2 = new SimpleStruct2 ();
530                 ss2.b = true;
531                 ss2.d = "TEST";
532                 ss2.e = 99;
533                 ss2.f = 1.5;
534                 ss2.g = 42;
535                 ss2.h = 123L;
536
537                 ss_arr [0] = ss2;
538
539                 ss2.b = false;
540                 ss2.d = "TEST2";
541                 ss2.e = 100;
542                 ss2.f = 2.5;
543                 ss2.g = 43;
544                 ss2.h = 124L;
545
546                 ss_arr [1] = ss2;
547
548                 return mono_test_marshal_struct_array (ss_arr);
549         }
550
551         public static int test_105_marshal_long_align_struct_array () {
552                 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
553
554                 LongAlignStruct ss = new LongAlignStruct ();
555                 ss.a = 5;
556                 ss.b = 10;
557                 ss.c = 15;
558
559                 ss_arr [0] = ss;
560
561                 ss.a = 20;
562                 ss.b = 25;
563                 ss.c = 30;
564
565                 ss_arr [1] = ss;
566
567                 return mono_test_marshal_long_align_struct_array (ss_arr);
568         }
569
570         /* Test classes as arguments and return values */
571         public static int test_0_marshal_class () {
572                 SimpleClass ss = new  SimpleClass ();
573                 ss.b = true;
574                 ss.d = "TEST";
575                 ss.e = 99;
576                 ss.f = 1.5;
577                 ss.g = 42;
578                 ss.h = 123L;
579
580                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
581                 if (res == null)
582                         return 1;
583                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
584                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
585                                 res.g == ss.g && res.h == ss.h))
586                         return 2;
587
588                 /* Test null arguments and results */
589                 res = mono_test_marshal_class (10, 11, 12, null, 14);
590                 if (res != null)
591                         return 3;
592
593                 return 0;
594         }
595
596         public static int test_0_marshal_byref_class () {
597                 SimpleClass ss = new  SimpleClass ();
598                 ss.b = true;
599                 ss.d = "TEST";
600                 ss.e = 99;
601                 ss.f = 1.5;
602                 ss.g = 42;
603                 ss.h = 123L;
604
605                 int res = mono_test_marshal_byref_class (ref ss);
606                 if (ss.d != "TEST-RES")
607                         return 1;
608
609                 return 0;
610         }
611
612         public static int test_0_marshal_delegate () {
613                 SimpleDelegate d = new SimpleDelegate (delegate_test);
614
615                 return mono_test_marshal_delegate (d);
616         }
617
618         public static int test_34_marshal_instance_delegate () {
619                 Tests t = new Tests ();
620                 t.int_field = 32;
621                 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
622
623                 return mono_test_marshal_delegate (d);
624         }
625
626         /* Static delegates closed over their first argument */
627         public static int closed_delegate (Tests t, int a) {
628                 return t.int_field + a;
629         }
630
631         public static int test_34_marshal_closed_static_delegate () {
632                 Tests t = new Tests ();
633                 t.int_field = 32;
634                 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
635
636                 return mono_test_marshal_delegate (d);
637         }
638
639         public static int test_0_marshal_return_delegate () {
640                 SimpleDelegate d = new SimpleDelegate (delegate_test);
641
642                 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
643
644                 return d2 (2);
645         }
646
647         public static int test_0_marshal_delegate_struct () {
648                 DelegateStruct s = new DelegateStruct ();
649
650                 s.a = 2;
651                 s.del = new SimpleDelegate (delegate_test);
652                 s.del2 = new SimpleDelegate (delegate_test);
653                 s.del3 = null;
654
655                 DelegateStruct res = mono_test_marshal_delegate_struct (s);
656
657                 if (res.a != 0)
658                         return 1;
659                 if (res.del (2) != 0)
660                         return 2;
661                 if (res.del2 (2) != 0)
662                         return 3;
663                 if (res.del3 != null)
664                         return 4;
665
666                 return 0;
667         }
668
669         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
670         public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
671
672         public static int test_3_marshal_out_delegate () {
673                 SimpleDelegate d = null;
674
675                 mono_test_marshal_out_delegate (out d);
676
677                 return d (2);
678         }
679
680         public static int test_0_marshal_byref_struct () {
681                 SimpleStruct s = new SimpleStruct ();
682                 s.a = true;
683                 s.b = false;
684                 s.c = true;
685                 s.d = "ABC";
686                 s.d2 = "DEF";
687
688                 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
689                 if (res != 0)
690                         return 1;
691                 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
692                         return 2;
693                 return 0;
694         }
695
696         public static int test_0_marshal_byref_struct_in () {
697                 SimpleStruct s = new SimpleStruct ();
698                 s.a = true;
699                 s.b = false;
700                 s.c = true;
701                 s.d = "ABC";
702                 s.d2 = "DEF";
703
704                 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
705                 if (res != 0)
706                         return 1;
707                 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
708                         return 2;
709                 return 0;
710         }
711
712         public static int test_0_marshal_byref_struct_inout () {
713                 SimpleStruct s = new SimpleStruct ();
714                 s.a = true;
715                 s.b = false;
716                 s.c = true;
717                 s.d = "ABC";
718                 s.d2 = "DEF";
719
720                 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
721                 if (res != 0)
722                         return 1;
723                 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
724                         return 2;
725                 return 0;
726         }
727
728         public static int test_0_marshal_point () {
729                 Point pt = new Point();
730                 pt.x = 1.25;
731                 pt.y = 3.5;
732                 
733                 return mono_test_marshal_point(pt);
734         }
735
736         public static int test_0_marshal_mixed_point () {
737                 MixedPoint mpt = new MixedPoint();
738                 mpt.x = 5;
739                 mpt.y = 6.75;
740                 
741                 return mono_test_marshal_mixed_point(mpt);
742         }
743
744         public static int test_0_marshal_bool_byref () {
745                 bool b = true;
746                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
747                         return 1;
748                 b = false;
749                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
750                         return 12;
751                 if (b != true)
752                         return 13;
753
754                 return 0;
755         }
756
757         public static int test_0_marshal_bool_as_I1 () {
758
759                 int ret;
760                 bool bTrue, bFalse;
761                 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
762                         return ret;
763
764                 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
765                         return ret;
766
767                 if(!bTrue)
768                         return 10;
769
770                 if(bFalse)
771                         return 11;
772
773                 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
774                         return ret;
775
776                 if(bTrue)
777                         return 12;
778
779                 if(!bFalse)
780                         return 13;
781
782                 return 0;
783         }
784
785         public static int test_0_marshal_bool_as_U1 () {
786
787                 int ret;
788                 bool bTrue, bFalse;
789                 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
790                         return ret;
791
792                 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
793                         return ret;
794
795                 if(!bTrue)
796                         return 10;
797
798                 if(bFalse)
799                         return 11;
800
801                 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
802                         return ret;
803
804                 if(bTrue)
805                         return 12;
806
807                 if(!bFalse)
808                         return 13;
809
810                 return 0;
811         }
812
813         public static int test_0_return_vtype () {
814                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
815
816                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
817                         return 0;
818
819                 return 1;
820         }
821
822         public static int test_0_marshal_stringbuilder () {
823                 StringBuilder sb = new StringBuilder(255);
824                 sb.Append ("ABCD");
825                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
826                 String res = sb.ToString();
827
828                 if (res != "This is my message.  Isn't it nice?")
829                         return 1;  
830
831                 // Test StringBuilder with default capacity (16)
832                 StringBuilder sb2 = new StringBuilder();
833                 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
834                 if (sb2.ToString () != "This is my messa")
835                         return 3;
836
837                 return 0;
838         }
839
840         public static int test_0_marshal_stringbuilder_append () {
841                 const String in_sentinel = "MONO_";
842                 const String out_sentinel = "CSHARP_";
843                 const int iterations = 100;
844                 StringBuilder sb = new StringBuilder(255);
845                 StringBuilder check = new StringBuilder(255);
846
847                 for (int i = 0; i < iterations; i++) {
848                         sb.Append (in_sentinel[i % in_sentinel.Length]);
849                         check.Append (out_sentinel[i % out_sentinel.Length]);
850
851                         mono_test_marshal_stringbuilder_append (sb, sb.Length);
852
853                         String res = sb.ToString();
854                         String checkRev = check.ToString();
855                         if (res != checkRev)
856                                 return 1;
857                 }
858
859                 // Test StringBuilder with default capacity (16)
860                 StringBuilder sb2 = new StringBuilder();
861                 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
862                 if (sb2.ToString () != "This is my messa")
863                         return 3;
864
865                 return 0;
866         }
867
868         public static int test_0_marshal_stringbuilder_unicode () {
869                 StringBuilder sb = new StringBuilder(255);
870                 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
871                 String res = sb.ToString();
872
873                 if (res != "This is my message.  Isn't it nice?")
874                         return 1;  
875
876                 // Test StringBuilder with default capacity (16)
877                 StringBuilder sb2 = new StringBuilder();
878                 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
879                 if (sb2.ToString () != "This is my messa")
880                         return 2;
881                 
882                 return 0;
883         }
884
885         public static int test_0_marshal_stringbuilder_out () {
886                 StringBuilder sb;
887                 mono_test_marshal_stringbuilder_out (out sb);
888                 
889                 if (sb.ToString () != "This is my message.  Isn't it nice?")
890                         return 1;  
891                 return 0;
892         }
893
894         public static int test_0_marshal_stringbuilder_out_unicode () {
895                 StringBuilder sb;
896                 mono_test_marshal_stringbuilder_out_unicode (out sb);
897
898                 if (sb.ToString () != "This is my message.  Isn't it nice?")
899                         return 1;  
900                 return 0;
901         }
902
903         public static int test_0_marshal_stringbuilder_ref () {
904                 StringBuilder sb = new StringBuilder ();
905                 sb.Append ("ABC");
906                 int res = mono_test_marshal_stringbuilder_ref (ref sb);
907                 if (res != 0)
908                         return 1;
909                 
910                 if (sb.ToString () != "This is my message.  Isn't it nice?")
911                         return 2;  
912                 return 0;
913         }
914
915         public static int test_0_marshal_empty_string_array () {
916                 return mono_test_marshal_empty_string_array (null);
917         }
918
919         public static int test_0_marshal_string_array () {
920                 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
921         }
922
923         public static int test_0_marshal_unicode_string_array () {
924                 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
925         }
926
927         public static int test_0_marshal_stringbuilder_array () {
928                 StringBuilder sb1 = new StringBuilder ("ABC");
929                 StringBuilder sb2 = new StringBuilder ("DEF");
930
931                 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
932                 if (res != 0)
933                         return res;
934                 if (sb1.ToString () != "DEF")
935                         return 5;
936                 if (sb2.ToString () != "ABC")
937                         return 6;
938                 return 0;
939         }
940
941         public static int test_0_last_error () {
942                 mono_test_last_error (5);
943                 if (Marshal.GetLastWin32Error () == 5)
944                         return 0;
945                 else
946                         return 1;
947         }
948
949         public static int test_0_entry_point_not_found () {
950
951                 try {
952                         mono_entry_point_not_found ();
953                         return 1;
954                 }
955                 catch (EntryPointNotFoundException) {
956                 }
957
958                 return 0;
959         }
960
961         public static int test_0_library_not_found () {
962
963                 try {
964                         mono_library_not_found ();
965                         return 1;
966                 }
967                 catch (DllNotFoundException) {
968                 }
969
970                 return 0;
971         }
972
973         /* Check that the runtime trims .dll from the library name */
974         public static int test_0_trim_dll_from_name () {
975
976                 mono_test_marshal_char_2 ('A');
977
978                 return 0;
979         }
980
981         /* Check that the runtime adds lib to to the library name */
982         public static int test_0_add_lib_to_name () {
983
984                 mono_test_marshal_char_3 ('A');
985
986                 return 0;
987         }
988
989         class C {
990                 public int i;
991         }
992
993         public static int test_0_asany () {
994                 if (mono_test_asany (5, 1) != 0)
995                         return 1;
996
997                 if (mono_test_asany ("ABC", 2) != 0)
998                         return 2;
999
1000                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
1001                 ss2.b = true;
1002                 ss2.d = "TEST";
1003                 ss2.e = 99;
1004                 ss2.f = 1.5;
1005                 ss2.g = 42;
1006                 ss2.h = 123L;
1007
1008                 if (mono_test_asany (ss2, 3) != 0)
1009                         return 3;
1010
1011                 if (mono_test_asany_unicode ("ABC", 4) != 0)
1012                         return 4;
1013
1014                 try {
1015                         C c = new C ();
1016                         c.i = 5;
1017                         mono_test_asany (c, 0);
1018                         return 5;
1019                 }
1020                 catch (ArgumentException) {
1021                 }
1022
1023                 try {
1024                         mono_test_asany (new Object (), 0);
1025                         return 6;
1026                 }
1027                 catch (ArgumentException) {
1028                 }
1029
1030                 return 0;
1031         }
1032
1033         /* AsAny marshalling + [In, Out] */
1034
1035         public static int test_0_asany_in () {
1036                 // Struct
1037                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1038                 mono_test_asany_in (str);
1039
1040                 // Formatted Class
1041                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1042                 mono_test_asany_in (cls);
1043                 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
1044                         return 1;
1045
1046                 // Boxed Struct
1047                 object obj = new AsAnyStruct(1,2,3, "ABC");
1048                 mono_test_asany_in (obj);
1049                 str = (AsAnyStruct)obj;
1050                 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
1051                         return 2;
1052
1053                 return 0;
1054         }
1055
1056         public static int test_0_asany_out () {
1057                 // Struct
1058                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1059                 mono_test_asany_out (str);
1060
1061                 // Formatted Class
1062                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1063                 mono_test_asany_out (cls);
1064                 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1065                         return 1;
1066
1067                 // Boxed Struct
1068                 object obj = new AsAnyStruct(1,2,3, "ABC");
1069                 mono_test_asany_out (obj);
1070                 str = (AsAnyStruct)obj;
1071                 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1072                         return 2;
1073
1074                 return 0;
1075         }
1076
1077         public static int test_0_asany_inout () {
1078                 // Struct
1079                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1080                 mono_test_asany_inout (str);
1081
1082                 // Formatted Class
1083                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1084                 mono_test_asany_inout (cls);
1085                 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1086                         return 1;
1087
1088                 // Boxed Struct
1089                 object obj = new AsAnyStruct(1,2,3, "ABC");
1090                 mono_test_asany_inout (obj);
1091                 str = (AsAnyStruct)obj;
1092                 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1093                         return 2;
1094
1095                 return 0;
1096         }
1097
1098         /* Byref String Array */
1099
1100         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1101         public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1102
1103         public static int test_0_byref_string_array () {
1104
1105                 string[] arr = null;
1106
1107                 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1108                         return 1;
1109
1110                 arr = new string[] { "Alpha", "Beta", "Gamma" };
1111
1112                 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1113                         return 2;
1114
1115                 /* FIXME: Test returned array and out case */
1116
1117                 return 0;
1118         }
1119
1120         /*
1121          * AMD64 small structs-by-value tests.
1122          */
1123
1124         /* TEST 1: 16 byte long INTEGER struct */
1125
1126         [StructLayout(LayoutKind.Sequential)]
1127         public struct Amd64Struct1 {
1128                 public int i;
1129                 public int j;
1130                 public int k;
1131                 public int l;
1132         }
1133         
1134         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1135         public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1136         
1137         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1138         public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1_many_args (Amd64Struct1 s, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8);
1139
1140         public static int test_0_amd64_struct1 () {
1141                 Amd64Struct1 s = new Amd64Struct1 ();
1142                 s.i = 5;
1143                 s.j = -5;
1144                 s.k = 0xffffff;
1145                 s.l = 0xfffffff;
1146
1147                 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1148
1149                 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1150         }
1151
1152         public static int test_0_amd64_struct1_many_args () {
1153                 Amd64Struct1 s = new Amd64Struct1 ();
1154                 s.i = 5;
1155                 s.j = -5;
1156                 s.k = 0xffffff;
1157                 s.l = 0xfffffff;
1158
1159                 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1160
1161                 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1162         }
1163
1164         /* TEST 2: 8 byte long INTEGER struct */
1165
1166         [StructLayout(LayoutKind.Sequential)]
1167         public struct Amd64Struct2 {
1168                 public int i;
1169                 public int j;
1170         }
1171         
1172         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1173         public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1174
1175         public static int test_0_amd64_struct2 () {
1176                 Amd64Struct2 s = new Amd64Struct2 ();
1177                 s.i = 5;
1178                 s.j = -5;
1179
1180                 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1181
1182                 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1183         }
1184
1185         /* TEST 3: 4 byte long INTEGER struct */
1186
1187         [StructLayout(LayoutKind.Sequential)]
1188         public struct Amd64Struct3 {
1189                 public int i;
1190         }
1191         
1192         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1193         public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1194
1195         public static int test_0_amd64_struct3 () {
1196                 Amd64Struct3 s = new Amd64Struct3 ();
1197                 s.i = -5;
1198
1199                 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1200
1201                 return (s2.i == -4) ? 0 : 1;
1202         }
1203
1204         /* Test 4: 16 byte long FLOAT struct */
1205
1206         [StructLayout(LayoutKind.Sequential)]
1207         public struct Amd64Struct4 {
1208                 public double d1, d2;
1209         }
1210         
1211         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1212         public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1213
1214         public static int test_0_amd64_struct4 () {
1215                 Amd64Struct4 s = new Amd64Struct4 ();
1216                 s.d1 = 5.0;
1217                 s.d2 = -5.0;
1218
1219                 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1220
1221                 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1222         }
1223
1224         /*
1225          * IA64 struct tests
1226          */
1227
1228         /* Test 5: Float HFA */
1229
1230         [StructLayout(LayoutKind.Sequential)]
1231         public struct TestStruct5 {
1232                 public float d1, d2;
1233         }
1234         
1235         [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1236         public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1237
1238         public static int test_0_ia64_struct5 () {
1239                 TestStruct5 s = new TestStruct5 ();
1240                 s.d1 = 5.0f;
1241                 s.d2 = -5.0f;
1242
1243                 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1244
1245                 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1246         }
1247
1248         /* Test 6: Double HFA */
1249
1250         [StructLayout(LayoutKind.Sequential)]
1251         public struct TestStruct6 {
1252                 public double d1, d2;
1253         }
1254         
1255         [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1256         public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1257
1258         public static int test_0_ia64_struct6 () {
1259                 TestStruct6 s = new TestStruct6 ();
1260                 s.d1 = 6.0;
1261                 s.d2 = -6.0;
1262
1263                 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1264
1265                 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1266         }
1267         
1268         /* Blittable class */
1269         [DllImport("libtest")]
1270         private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1271
1272         public static int test_0_marshal_blittable_class () {
1273                 BlittableClass v1 = new BlittableClass ();
1274
1275                 /* Since it is blittable, it looks like it is passed as in/out */
1276                 BlittableClass v2 = TestBlittableClass (v1);
1277
1278                 if (v1.a != 2 || v1.b != 3)
1279                         return 1;
1280                 
1281                 if (v2.a != 2 || v2.b != 3)
1282                         return 2;
1283
1284                 // Test null
1285                 BlittableClass v3 = TestBlittableClass (null);
1286
1287                 if (v3.a != 42 || v3.b != 43)
1288                         return 3;
1289                 
1290                 return 0;
1291         }
1292
1293         /*
1294          * Generic structures
1295          */
1296
1297         [StructLayout(LayoutKind.Sequential)]
1298         public struct Amd64Struct1Gen<T> {
1299                 public T i;
1300                 public T j;
1301                 public T k;
1302                 public T l;
1303         }
1304         
1305         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1306         public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1307
1308         public static int test_0_amd64_struct1_gen () {
1309                 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1310                 s.i = 5;
1311                 s.j = -5;
1312                 s.k = 0xffffff;
1313                 s.l = 0xfffffff;
1314
1315                 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1316
1317                 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1318         }
1319
1320         /*
1321          * Other tests
1322          */
1323
1324         public static int test_0_marshal_byval_class () {
1325                 SimpleObj obj0 = new SimpleObj ();
1326                 obj0.str = "T1";
1327                 obj0.i = 4;
1328                 
1329                 if (class_marshal_test0 (obj0) != 0)
1330                         return 1;
1331
1332                 return 0;
1333         }
1334
1335         public static int test_0_marshal_byval_class_null () {
1336                 if (class_marshal_test4 (null) != 0)
1337                         return 1;
1338
1339                 return 0;
1340         }
1341
1342         public static int test_0_marshal_out_class () {
1343                 SimpleObj obj1;
1344
1345                 class_marshal_test1 (out obj1);
1346
1347                 if (obj1.str != "ABC")
1348                         return 1;
1349
1350                 if (obj1.i != 5)
1351                         return 2;
1352
1353                 return 0;
1354         }
1355
1356         public static int test_0_marshal_string () {
1357                 return string_marshal_test0 ("TEST0");
1358         }
1359
1360         public static int test_0_marshal_out_string () {
1361                 string res;
1362                 
1363                 string_marshal_test1 (out res);
1364
1365                 if (res != "TEST1")
1366                         return 1;
1367
1368                 return 0;
1369         }
1370
1371         public static int test_0_marshal_byref_string () {
1372                 string res = "TEST1";
1373
1374                 int r = string_marshal_test2 (ref res);
1375                 if (r != 0)
1376                         return 1;
1377                 if (res != "TEST2")
1378                         return 2;
1379                 return 0;
1380         }
1381
1382         public static int test_0_marshal_null_string () {
1383                 return string_marshal_test3 (null);
1384         }
1385
1386 #if FALSE
1387         [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_1", CallingConvention=CallingConvention.StdCall)]
1388         public static extern int mono_test_stdcall_mismatch_1 (int a, int b, int c);
1389
1390         /* Test mismatched called conventions, the native function is cdecl */
1391         public static int test_0_stdcall_mismatch_1 () {
1392                 mono_test_stdcall_mismatch_1 (0, 1, 2);
1393                 return 0;
1394         }
1395
1396         [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_2", CallingConvention=CallingConvention.Cdecl)]
1397         public static extern int mono_test_stdcall_mismatch_2 (int a, int b, int c);
1398
1399         /* Test mismatched called conventions, the native function is stdcall */
1400         public static int test_0_stdcall_mismatch_2 () {
1401                 mono_test_stdcall_mismatch_2 (0, 1, 2);
1402                 return 0;
1403         }
1404 #endif
1405
1406         [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1407         public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1408
1409         public static int test_0_stdcall_name_mangling () {
1410                 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1411         }
1412
1413         /* Float test */
1414
1415         [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1416         public static extern float mono_test_marshal_pass_return_float (float f);
1417
1418         public static int test_0_pass_return_float () {
1419                 float f = mono_test_marshal_pass_return_float (1.5f);
1420
1421                 return (f == 2.5f) ? 0 : 1;
1422         }
1423
1424         /*
1425          * Pointers to structures can not be passed
1426          */
1427
1428         /* This seems to be allowed by MS in some cases */
1429         /*
1430         public struct CharInfo {
1431                 public char Character;
1432                 public short Attributes;
1433         }
1434
1435         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1436         public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1437
1438         public static unsafe int test_0_marshal_ptr_to_struct () {
1439                 CharInfo [] buffer = new CharInfo [1];
1440                 fixed (CharInfo *ptr = &buffer [0]) {
1441                         try {
1442                                 mono_test_marshal_ptr_to_struct (ptr);
1443                                 return 1;
1444                         }
1445                         catch (MarshalDirectiveException) {
1446                                 return 0;
1447                         }
1448                 }
1449                 return 1;
1450         }
1451         */
1452
1453         /*
1454          * LPWStr marshalling
1455          */
1456
1457         [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1458         [return: MarshalAs(UnmanagedType.LPWStr)]
1459         private static extern string mono_test_marshal_lpwstr_marshal(
1460                 [MarshalAs(UnmanagedType.LPWStr)] string s,
1461                 int length );
1462
1463         [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1464         private static extern string mono_test_marshal_lpwstr_marshal2(
1465                 string s,
1466                 int length );
1467
1468         [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1469         private static extern void mono_test_marshal_lpwstr_out_marshal(
1470                 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1471
1472         [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1473         private static extern void mono_test_marshal_lpwstr_out_marshal2(
1474                 out string s);
1475
1476         public static int test_0_pass_return_lwpstr () {
1477                 string s;
1478                 
1479                 mono_test_marshal_lpwstr_out_marshal (out s);
1480
1481                 if (s != "ABC")
1482                         return 1;
1483
1484                 s = null;
1485                 mono_test_marshal_lpwstr_out_marshal2 (out s);
1486
1487                 if (s != "ABC")
1488                         return 2;
1489                 
1490                 return 0;               
1491         }
1492
1493         public static int test_0_out_lwpstr () {
1494                 string s = "ABC";
1495                 
1496                 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1497
1498                 if (res != "ABC")
1499                         return 1;
1500
1501                 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1502
1503                 if (res2 != "ABC")
1504                         return 2;
1505                 
1506                 return 0;               
1507         }
1508
1509         /*
1510          * Byref bool marshalling
1511          */
1512
1513         [DllImport("libtest")]
1514         extern static int marshal_test_ref_bool
1515         (
1516                 int i, 
1517                 [MarshalAs(UnmanagedType.I1)] ref bool b1, 
1518                 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2, 
1519                 ref bool b3
1520         );
1521
1522         public static int test_0_pass_byref_bool () {
1523                 for (int i = 0; i < 8; i++)
1524                 {
1525                         bool b1 = (i & 4) != 0;
1526                         bool b2 = (i & 2) != 0;
1527                         bool b3 = (i & 1) != 0;
1528                         bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1529                         if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1530                                 return 4 * i + 1;
1531                         if (b1 != !orig_b1)
1532                                 return 4 * i + 2;
1533                         if (b2 != !orig_b2)
1534                                 return 4 * i + 3;
1535                         if (b3 != !orig_b3)
1536                                 return 4 * i + 4;
1537                 }
1538
1539                 return 0;
1540         }
1541
1542         /*
1543          * Bool struct field marshalling
1544          */
1545
1546         struct BoolStruct
1547         {
1548                 public int i;
1549                 [MarshalAs(UnmanagedType.I1)] public bool b1;
1550                 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1551                 public bool b3;
1552         }
1553
1554         [DllImport("libtest")]
1555         extern static int marshal_test_bool_struct(ref BoolStruct s);
1556
1557         public static int test_0_pass_bool_in_struct () {
1558                 for (int i = 0; i < 8; i++)
1559                 {
1560                         BoolStruct s = new BoolStruct();
1561                         s.i = i;
1562                         s.b1 = (i & 4) != 0;
1563                         s.b2 = (i & 2) != 0;
1564                         s.b3 = (i & 1) != 0;
1565                         BoolStruct orig = s;
1566                         if (marshal_test_bool_struct(ref s) != 0)
1567                                 return 4 * i + 33;
1568                         if (s.b1 != !orig.b1)
1569                                 return 4 * i + 34;
1570                         if (s.b2 != !orig.b2)
1571                                 return 4 * i + 35;
1572                         if (s.b3 != !orig.b3)
1573                                 return 4 * i + 36;
1574                 }
1575
1576                 return 0;
1577         }
1578
1579         /*
1580          * Alignment of structs containing longs
1581          */
1582
1583         struct LongStruct2 {
1584                 public long l;
1585         }
1586
1587         struct LongStruct {
1588                 public int i;
1589                 public LongStruct2 l;
1590         }
1591
1592         [DllImport("libtest")]
1593         extern static int mono_test_marshal_long_struct (ref LongStruct s);
1594
1595         public static int test_47_pass_long_struct () {
1596                 LongStruct s = new LongStruct ();
1597                 s.i = 5;
1598                 s.l = new LongStruct2 ();
1599                 s.l.l = 42;
1600
1601                 return mono_test_marshal_long_struct (ref s);
1602         }
1603
1604         /*
1605          * Invoking pinvoke methods through delegates
1606          */
1607
1608         delegate int MyDelegate (string name);
1609
1610         [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1611         public static extern int puts_static (string name);
1612
1613         public static int test_0_invoke_pinvoke_through_delegate () {
1614                 puts_static ("A simple Test for PInvoke 1");
1615
1616                 MyDelegate d = new MyDelegate (puts_static);
1617                 d ("A simple Test for PInvoke 2");
1618
1619                 object [] args = {"A simple Test for PInvoke 3"};
1620                 d.DynamicInvoke (args);
1621
1622                 return 0;
1623         }
1624
1625         /*
1626          * Missing virtual pinvoke methods
1627          */
1628
1629         public class T {
1630
1631                 public virtual object MyClone ()
1632                 {
1633                         return null;
1634                 }
1635         }
1636
1637         public class T2 : T {
1638                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1639                 public override extern object MyClone ();
1640         }
1641
1642         public static int test_0_missing_virtual_pinvoke_method () {
1643                 T2 t = new T2 ();
1644
1645                 try {
1646                         t.MyClone ();
1647                 } catch (Exception ex) {
1648                         return 0;
1649                 }
1650                 
1651                 return 1;
1652         }
1653
1654         /*
1655          * Marshalling of type 'object'
1656          */
1657
1658         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1659         public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1660
1661         public static int test_0_marshal_object () {
1662                 try {
1663                         mono_test_marshal_object (0, 0, 0, null, 0);
1664                         return 1;
1665                 } catch (Exception) {
1666                         return 0;
1667                 }
1668         }
1669
1670         /*
1671          * Marshalling of DateTime to OLE DATE (double)
1672          */
1673         [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
1674         public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
1675
1676         public static int test_0_marshal_date_time () {
1677                 DateTime d = new DateTime (2009, 12, 6);
1678                 DateTime d2;
1679                 double d3 = mono_test_marshal_date_time (d, out d2);
1680                 if (d3 != 40153.0)
1681                         return 1;
1682                 if (d2 != d)
1683                         return 2;
1684                 return 0;
1685         }
1686
1687         /*
1688          * Calling pinvoke functions dynamically using calli
1689          */
1690         
1691         [DllImport("libtest")]
1692         private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
1693
1694         delegate void CalliDel (IntPtr a, int[] f);
1695
1696         public static int test_0_calli_dynamic () {
1697                 /* we need the cdecl version because the icall convention demands it under Windows */
1698                 IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1699
1700                 DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
1701
1702                 var il = dm.GetILGenerator ();
1703                 var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
1704
1705                 il.Emit (OpCodes.Ldarg, 1);
1706                 signature.AddArgument (typeof (byte[]));
1707
1708                 il.Emit (OpCodes.Ldarg_0);
1709
1710                 il.Emit (OpCodes.Calli, signature);
1711                 il.Emit (OpCodes.Ret);
1712
1713                 var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
1714
1715                 int[] arr = new int [1000];
1716                 for (int i = 0; i < 50; ++i)
1717                         arr [i] = (int)i;
1718                 f (func, arr);
1719                 if (arr.Length != 1000)
1720                         return 1;
1721                 for (int i = 0; i < 50; ++i)
1722                         if (arr [i] != 50 - i)
1723                                 return 2;
1724
1725                 return 0;
1726         }
1727
1728
1729         /*char array marshaling */
1730         [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
1731         public static extern int mono_test_marshal_ansi_char_array (char[] a1);
1732
1733         public static int test_0_marshal_ansi_char_array () {
1734                 char[] buf = new char [32];
1735                 buf [0] = 'q';
1736                 buf [1] = 'w';
1737                 buf [2] = 'e';
1738                 buf [3] = 'r';
1739
1740                 if (mono_test_marshal_ansi_char_array (buf) != 0)
1741                         return 1;
1742
1743                 string s = new string (buf);
1744                 if (s.StartsWith ("qwer"))
1745                         return 0;
1746                 else
1747                         return 2;
1748         }
1749
1750         /*char array marshaling */
1751         [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
1752         public static extern int mono_test_marshal_unicode_char_array (char[] a1);
1753
1754         public static int test_0_marshal_unicode_char_array () {
1755                 char[] buf = new char [32];
1756                 buf [0] = 'q';
1757                 buf [1] = 'w';
1758                 buf [2] = 'e';
1759                 buf [3] = 'r';
1760
1761                 if (mono_test_marshal_unicode_char_array (buf) != 0)
1762                         return 1;
1763
1764                 string s = new string (buf);
1765                 if (s.StartsWith ("abcdef"))
1766                         return 0;
1767                 else
1768                         return 2;
1769         }
1770
1771         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
1772         public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
1773
1774         public static int test_0_mono_test_marshal_lpstr () {
1775                 string str = "ABC";
1776
1777                 if (mono_test_marshal_lpstr (str) != 0)
1778                         return 1;
1779
1780                 return 0;
1781         }
1782
1783         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
1784         public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
1785
1786         public static int test_0_mono_test_marshal_lpwstr () {
1787                 string str = "ABC";
1788
1789                 if (mono_test_marshal_lpwstr (str) != 0)
1790                         return 1;
1791
1792                 return 0;
1793         }
1794
1795
1796         [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
1797         [return: MarshalAs(UnmanagedType.LPStr)]
1798         public static extern string mono_test_marshal_return_lpstr ();
1799
1800         public static int test_0_mono_test_marshal_return_lpstr () {
1801                 string str = mono_test_marshal_return_lpstr ();
1802                 if ("XYZ" == str)
1803                         return 0;
1804
1805                 return 1;
1806         }
1807
1808         [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
1809         [return: MarshalAs(UnmanagedType.LPWStr)]
1810         public static extern string mono_test_marshal_return_lpwstr ();
1811
1812         public static int test_0_mono_test_marshal_return_lpwstr () {
1813                 string str = mono_test_marshal_return_lpwstr ();
1814                 if ("XYZ" == str)
1815                         return 0;
1816
1817                 return 1;
1818         }
1819
1820         [DllImport ("libtest", EntryPoint="mono_test_has_thiscall")]
1821         public static extern int mono_test_has_thiscall ();
1822
1823         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1824         public static extern int mono_test_native_thiscall (int a);
1825
1826         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1827         public static extern int mono_test_native_thiscall (int a, int b);
1828
1829         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1830         public static extern int mono_test_native_thiscall (int a, int b, int c);
1831
1832         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1833         public static extern int mono_test_native_thiscall (TinyStruct a);
1834
1835         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1836         public static extern int mono_test_native_thiscall (TinyStruct a, int b);
1837
1838         [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1839         public static extern int mono_test_native_thiscall (TinyStruct a, int b, int c);
1840
1841         public static int test_0_native_thiscall ()
1842         {
1843                 if (mono_test_has_thiscall () == 0)
1844                         return 0;
1845
1846                 if (mono_test_native_thiscall (1968329802) != 1968329802)
1847                         return 1;
1848
1849                 if (mono_test_native_thiscall (268894549, 1212675791) != 1481570339)
1850                         return 2;
1851
1852                 if (mono_test_native_thiscall (1288082683, -421187449, -1733670329) != -866775098)
1853                         return 3;
1854
1855                 if (mono_test_native_thiscall (new TinyStruct(1968329802)) != 1968329802)
1856                         return 4;
1857
1858                 if (mono_test_native_thiscall (new TinyStruct(268894549), 1212675791) != 1481570339)
1859                         return 5;
1860
1861                 if (mono_test_native_thiscall (new TinyStruct(1288082683), -421187449, -1733670329) != -866775098)
1862                         return 6;
1863
1864                 return 0;
1865         }
1866
1867         [DllImport ("libtest", EntryPoint = "mono_test_marshal_return_single_double_struct")]
1868         public static extern SingleDoubleStruct mono_test_marshal_return_single_double_struct ();
1869
1870         public static int test_0_x86_single_double_struct_ret () {
1871                 double d = mono_test_marshal_return_single_double_struct ().d;
1872                 if (d != 3.0)
1873                         return 1;
1874                 else
1875                         return 0;
1876         }
1877 }
1878