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