2008-10-24 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / tests / pinvoke2.cs
1 using System;
2 using System.Text;
3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
5
6 public class Tests {
7
8         public int int_field;
9
10         public static int delegate_test (int a)
11         {
12                 if (a == 2)
13                         return 0;
14
15                 return 1;
16         }
17
18         public int delegate_test_instance (int a)
19         {
20                 return int_field + a;
21         }
22         
23         [StructLayout (LayoutKind.Sequential)]
24         public struct SimpleStruct {
25                 public bool a;
26                 public bool b;
27                 public bool c;
28                 public string d;
29                 [MarshalAs(UnmanagedType.LPWStr)]
30                 public string d2;
31         }
32
33         [StructLayout (LayoutKind.Sequential)]
34         public struct SimpleStruct2 {
35                 public bool a;
36                 public bool b;
37                 public bool c;
38                 public string d;
39                 public byte e;
40                 public double f;
41                 public byte g;
42                 public long h;
43         }
44
45         [StructLayout (LayoutKind.Sequential, Size=0)]
46         public struct EmptyStruct {
47         }
48
49         [StructLayout (LayoutKind.Sequential)]
50         public struct DelegateStruct {
51                 public int a;
52                 public SimpleDelegate del;
53                 [MarshalAs(UnmanagedType.FunctionPtr)] 
54                 public SimpleDelegate del2;
55         }
56
57         /* sparcv9 has complex conventions when passing structs with doubles in them 
58            by value, some simple tests for them */
59         [StructLayout (LayoutKind.Sequential)]
60         public struct Point {
61                 public double x;
62                 public double y;
63         }
64
65         [StructLayout (LayoutKind.Sequential)]
66         public struct MixedPoint {
67                 public int x;
68                 public double y;
69         }
70
71         [StructLayout (LayoutKind.Sequential)]
72         public class SimpleClass {
73                 public bool a;
74                 public bool b;
75                 public bool c;
76                 public string d;
77                 public byte e;
78                 public double f;
79                 public byte g;
80                 public long h;
81         }
82
83         [StructLayout (LayoutKind.Sequential)]
84         public class EmptyClass {
85         }
86
87         [StructLayout (LayoutKind.Sequential)]
88         public struct LongAlignStruct {
89                 public int a;
90                 public long b;
91                 public long c;
92         }
93
94         [StructLayout(LayoutKind.Sequential)]
95         public class BlittableClass
96         {
97                 public int a = 1;
98                 public int b = 2;
99         }
100
101         [StructLayout (LayoutKind.Sequential)]
102         class SimpleObj
103         {
104                 public string str;
105                 public int i;
106         }
107
108         [StructLayout(LayoutKind.Sequential)]
109         struct AsAnyStruct
110         {
111                 public int i;
112                 public int j;
113                 public int k;
114                 public String s;
115
116                 public AsAnyStruct(int i, int j, int k, String s) {
117                         this.i = i;
118                         this.j = j;
119                         this.k = k;
120                         this.s = s;
121                 }
122         }
123
124         [StructLayout(LayoutKind.Sequential)]
125         class AsAnyClass
126         {
127                 public int i;
128                 public int j;
129                 public int k;
130                 public String s;
131
132                 public AsAnyClass(int i, int j, int k, String s) {
133                         this.i = i;
134                         this.j = j;
135                         this.k = k;
136                 }
137         }
138
139         [DllImport ("libnot-found", EntryPoint="not_found")]
140         public static extern int mono_library_not_found ();
141
142         [DllImport ("libtest", EntryPoint="not_found")]
143         public static extern int mono_entry_point_not_found ();
144
145         [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
146         public static extern int mono_test_marshal_char_2 (char a1);
147
148         [DllImport ("test", EntryPoint="mono_test_marshal_char")]
149         public static extern int mono_test_marshal_char_3 (char a1);
150
151         [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
152         public static extern int mono_test_marshal_char (char a1);
153
154         [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
155         public static extern int mono_test_marshal_char_array (char[] a1);
156
157         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
158         public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
159
160         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
161         public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
162
163         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
164         public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
165
166         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
167         public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
168
169         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
170         public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
171
172         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
173         public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
174
175         [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
176         public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
177
178         [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
179         public static extern int mono_test_marshal_array (int [] a1);
180
181         [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
182         public static extern int mono_test_marshal_empty_string_array (string [] a1);
183
184         [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
185         public static extern int mono_test_marshal_string_array (string [] a1);
186
187         [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
188         public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
189
190         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
191         public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);   
192
193         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
194         public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
195
196         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
197         public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
198
199         [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
200         public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
201         
202         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
203         public static extern int mono_test_marshal_struct (SimpleStruct ss);
204
205         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
206         public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
207
208         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
209         public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
210
211         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
212         public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
213
214         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
215         public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
216
217         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
218         public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
219
220         [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
221         public static extern int mono_test_marshal_point (Point p);
222
223         [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
224         public static extern int mono_test_marshal_mixed_point (MixedPoint p);
225
226         [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
227         public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
228
229         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
230         public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
231
232         [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
233         public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
234
235         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
236         public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
237
238         [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
239         public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
240
241         [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
242         public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
243
244         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
245         public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
246
247         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
248         public static extern int mono_test_marshal_delegate (SimpleDelegate d);
249
250         [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
251         public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
252
253         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
254         public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
255
256         [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
257         public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
258
259         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
260         public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
261
262         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
263         public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
264
265         [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
266         public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
267
268         [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
269         public static extern void mono_test_last_error (int err);
270
271         [DllImport ("libtest", EntryPoint="mono_test_asany")]
272         public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
273
274         [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
275         public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
276
277         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
278         static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj); 
279
280         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
281         static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj); 
282         [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
283         static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj); 
284
285         [DllImport ("libtest")]
286         static extern int class_marshal_test0 (SimpleObj obj);
287
288         [DllImport ("libtest")]
289         static extern void class_marshal_test1 (out SimpleObj obj);
290
291         [DllImport ("libtest")]
292         static extern int class_marshal_test4 (SimpleObj obj);
293         
294         [DllImport ("libtest")]
295         static extern int string_marshal_test0 (string str);
296
297         [DllImport ("libtest")]
298         static extern void string_marshal_test1 (out string str);
299
300         [DllImport ("libtest")]
301         static extern int string_marshal_test2 (ref string str);
302
303         [DllImport ("libtest")]
304         static extern int string_marshal_test3 (string str);
305
306         public delegate int SimpleDelegate (int a);
307
308         public static int Main (string[] args) {
309                 return TestDriver.RunTests (typeof (Tests), args);
310         }
311
312         public static int test_0_marshal_char () {
313                 return mono_test_marshal_char ('a');
314         }
315
316         public static int test_0_marshal_char_array () {
317                 // a unicode char[] is implicitly marshalled as [Out]
318                 char[] buf = new char [32];
319                 mono_test_marshal_char_array (buf);
320                 string s = new string (buf);
321                 if (s.StartsWith ("abcdef"))
322                         return 0;
323                 else
324                         return 1;
325         }
326
327         public static int test_1225_marshal_array () {
328                 int [] a1 = new int [50];
329                 for (int i = 0; i < 50; i++)
330                         a1 [i] = i;
331
332                 return mono_test_marshal_array (a1);
333         }
334
335         public static int test_1225_marshal_inout_array () {
336                 int [] a1 = new int [50];
337                 for (int i = 0; i < 50; i++)
338                         a1 [i] = i;
339
340                 int res = mono_test_marshal_inout_array (a1);
341
342                 for (int i = 0; i < 50; i++)
343                         if (a1 [i] != 50 - i) {
344                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
345                                 return 2;
346                         }
347
348                 return res;
349         }
350
351         public static int test_0_marshal_out_array () {
352                 int [] a1 = new int [50];
353
354                 int res = mono_test_marshal_out_array (a1, 0);
355
356                 for (int i = 0; i < 50; i++)
357                         if (a1 [i] != i) {
358                                 Console.WriteLine ("X: " + i + " " + a1 [i]);
359                                 return 2;
360                         }
361
362                 return 0;
363         }
364
365         public static int test_0_marshal_inout_nonblittable_array () {
366                 char [] a1 = new char [10];
367                 for (int i = 0; i < 10; i++)
368                         a1 [i] = "Hello, World" [i];
369
370                 int res = mono_test_marshal_inout_nonblittable_array (a1);
371
372                 for (int i = 0; i < 10; i++)
373                         if (a1 [i] != 'F')
374                                 return 2;
375
376                 return res;
377         }
378
379         public static int test_0_marshal_struct () {
380                 SimpleStruct ss = new  SimpleStruct ();
381                 ss.b = true;
382                 ss.d = "TEST";
383                 
384                 return mono_test_marshal_struct (ss);
385         }
386
387         public static int test_0_marshal_struct2 () {
388                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
389                 ss2.b = true;
390                 ss2.d = "TEST";
391                 ss2.e = 99;
392                 ss2.f = 1.5;
393                 ss2.g = 42;
394                 ss2.h = 123L;
395
396                 return mono_test_marshal_struct2 (ss2);
397         }
398
399         public static int test_0_marshal_struct3 () {
400                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
401                 ss2.b = true;
402                 ss2.d = "TEST";
403                 ss2.e = 99;
404                 ss2.f = 1.5;
405                 ss2.g = 42;
406                 ss2.h = 123L;
407
408                 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
409         }
410
411         public static int test_0_marshal_empty_struct () {
412                 EmptyStruct es = new EmptyStruct ();
413
414                 if (mono_test_empty_struct (1, es, 2) != 0)
415                         return 1;
416                 
417                 return 0;
418         }
419
420         public static int test_0_marshal_lpstruct () {
421                 SimpleStruct ss = new  SimpleStruct ();
422                 ss.b = true;
423                 ss.d = "TEST";
424                 
425                 return mono_test_marshal_lpstruct (ss);
426         }
427
428         public static int test_0_marshal_lpstruct_blittable () {
429                 Point p = new Point ();
430                 p.x = 1.0;
431                 p.y = 2.0;
432                 
433                 return mono_test_marshal_lpstruct_blittable (p);
434         }
435
436         public static int test_0_marshal_struct_array () {
437                 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
438
439                 SimpleStruct2 ss2 = new SimpleStruct2 ();
440                 ss2.b = true;
441                 ss2.d = "TEST";
442                 ss2.e = 99;
443                 ss2.f = 1.5;
444                 ss2.g = 42;
445                 ss2.h = 123L;
446
447                 ss_arr [0] = ss2;
448
449                 ss2.b = false;
450                 ss2.d = "TEST2";
451                 ss2.e = 100;
452                 ss2.f = 2.5;
453                 ss2.g = 43;
454                 ss2.h = 124L;
455
456                 ss_arr [1] = ss2;
457
458                 return mono_test_marshal_struct_array (ss_arr);
459         }
460
461         public static int test_105_marshal_long_align_struct_array () {
462                 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
463
464                 LongAlignStruct ss = new LongAlignStruct ();
465                 ss.a = 5;
466                 ss.b = 10;
467                 ss.c = 15;
468
469                 ss_arr [0] = ss;
470
471                 ss.a = 20;
472                 ss.b = 25;
473                 ss.c = 30;
474
475                 ss_arr [1] = ss;
476
477                 return mono_test_marshal_long_align_struct_array (ss_arr);
478         }
479
480         /* Test classes as arguments and return values */
481         public static int test_0_marshal_class () {
482                 SimpleClass ss = new  SimpleClass ();
483                 ss.b = true;
484                 ss.d = "TEST";
485                 ss.e = 99;
486                 ss.f = 1.5;
487                 ss.g = 42;
488                 ss.h = 123L;
489
490                 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
491                 if (res == null)
492                         return 1;
493                 if  (! (res.a == ss.a && res.b == ss.b && res.c == ss.c && 
494                                 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
495                                 res.g == ss.g && res.h == ss.h))
496                         return 2;
497
498                 /* Test null arguments and results */
499                 res = mono_test_marshal_class (10, 11, 12, null, 14);
500                 if (res != null)
501                         return 3;
502
503                 return 0;
504         }
505
506         public static int test_0_marshal_byref_class () {
507                 SimpleClass ss = new  SimpleClass ();
508                 ss.b = true;
509                 ss.d = "TEST";
510                 ss.e = 99;
511                 ss.f = 1.5;
512                 ss.g = 42;
513                 ss.h = 123L;
514
515                 int res = mono_test_marshal_byref_class (ref ss);
516                 if (ss.d != "TEST-RES")
517                         return 1;
518
519                 return 0;
520         }
521
522         public static int test_0_marshal_delegate () {
523                 SimpleDelegate d = new SimpleDelegate (delegate_test);
524
525                 return mono_test_marshal_delegate (d);
526         }
527
528         public static int test_34_marshal_instance_delegate () {
529                 Tests t = new Tests ();
530                 t.int_field = 32;
531                 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
532
533                 return mono_test_marshal_delegate (d);
534         }
535
536         public static int test_0_marshal_return_delegate () {
537                 SimpleDelegate d = new SimpleDelegate (delegate_test);
538
539                 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
540
541                 return d2 (2);
542         }
543
544         public static int test_0_marshal_delegate_struct () {
545                 DelegateStruct s = new DelegateStruct ();
546
547                 s.a = 2;
548                 s.del = new SimpleDelegate (delegate_test);
549                 s.del2 = new SimpleDelegate (delegate_test);
550
551                 DelegateStruct res = mono_test_marshal_delegate_struct (s);
552
553                 if (res.a != 0)
554                         return 1;
555                 if (res.del (2) != 0)
556                         return 2;
557                 if (res.del2 (2) != 0)
558                         return 3;
559
560                 return 0;
561         }
562
563         [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
564         public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
565
566         public static int test_3_marshal_out_delegate () {
567                 SimpleDelegate d = null;
568
569                 mono_test_marshal_out_delegate (out d);
570
571                 return d (2);
572         }
573
574         public static int test_0_marshal_byref_struct () {
575                 SimpleStruct s = new SimpleStruct ();
576                 s.a = true;
577                 s.b = false;
578                 s.c = true;
579                 s.d = "ABC";
580                 s.d2 = "DEF";
581
582                 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
583                 if (res != 0)
584                         return 1;
585                 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
586                         return 2;
587                 return 0;
588         }
589
590         public static int test_0_marshal_byref_struct_in () {
591                 SimpleStruct s = new SimpleStruct ();
592                 s.a = true;
593                 s.b = false;
594                 s.c = true;
595                 s.d = "ABC";
596                 s.d2 = "DEF";
597
598                 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
599                 if (res != 0)
600                         return 1;
601                 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
602                         return 2;
603                 return 0;
604         }
605
606         public static int test_0_marshal_byref_struct_inout () {
607                 SimpleStruct s = new SimpleStruct ();
608                 s.a = true;
609                 s.b = false;
610                 s.c = true;
611                 s.d = "ABC";
612                 s.d2 = "DEF";
613
614                 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
615                 if (res != 0)
616                         return 1;
617                 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
618                         return 2;
619                 return 0;
620         }
621
622         public static int test_0_marshal_point () {
623                 Point pt = new Point();
624                 pt.x = 1.25;
625                 pt.y = 3.5;
626                 
627                 return mono_test_marshal_point(pt);
628         }
629
630         public static int test_0_marshal_mixed_point () {
631                 MixedPoint mpt = new MixedPoint();
632                 mpt.x = 5;
633                 mpt.y = 6.75;
634                 
635                 return mono_test_marshal_mixed_point(mpt);
636         }
637
638         public static int test_0_marshal_bool_byref () {
639                 bool b = true;
640                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
641                         return 1;
642                 b = false;
643                 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
644                         return 12;
645                 if (b != true)
646                         return 13;
647
648                 return 0;
649         }
650
651         public static int test_0_marshal_bool_as_I1 () {
652
653                 int ret;
654                 bool bTrue, bFalse;
655                 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
656                         return ret;
657
658                 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
659                         return ret;
660
661                 if(!bTrue)
662                         return 10;
663
664                 if(bFalse)
665                         return 11;
666
667                 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
668                         return ret;
669
670                 if(bTrue)
671                         return 12;
672
673                 if(!bFalse)
674                         return 13;
675
676                 return 0;
677         }
678
679         public static int test_0_marshal_bool_as_U1 () {
680
681                 int ret;
682                 bool bTrue, bFalse;
683                 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
684                         return ret;
685
686                 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
687                         return ret;
688
689                 if(!bTrue)
690                         return 10;
691
692                 if(bFalse)
693                         return 11;
694
695                 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
696                         return ret;
697
698                 if(bTrue)
699                         return 12;
700
701                 if(!bFalse)
702                         return 13;
703
704                 return 0;
705         }
706
707         public static int test_0_return_vtype () {
708                 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
709
710                 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
711                         return 0;
712
713                 return 1;
714         }
715
716         public static int test_0_marshal_stringbuilder () {
717                 StringBuilder sb = new StringBuilder(255);
718                 sb.Append ("ABCD");
719                 mono_test_marshal_stringbuilder (sb, sb.Capacity);
720                 String res = sb.ToString();
721
722                 if (res != "This is my message.  Isn't it nice?")
723                         return 1;  
724
725                 // Test StringBuilder with default capacity (16)
726                 StringBuilder sb2 = new StringBuilder();
727                 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
728                 if (sb2.ToString () != "This is my messa")
729                         return 2;
730
731                 return 0;
732         }
733
734         public static int test_0_marshal_stringbuilder_unicode () {
735                 StringBuilder sb = new StringBuilder(255);
736                 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
737                 String res = sb.ToString();
738
739                 if (res != "This is my message.  Isn't it nice?")
740                         return 1;  
741
742                 // Test StringBuilder with default capacity (16)
743                 StringBuilder sb2 = new StringBuilder();
744                 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
745                 if (sb2.ToString () != "This is my messa")
746                         return 2;
747                 
748                 return 0;
749         }
750
751         public static int test_0_marshal_empty_string_array () {
752                 return mono_test_marshal_empty_string_array (null);
753         }
754
755         public static int test_0_marshal_string_array () {
756                 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
757         }
758
759         public static int test_0_marshal_unicode_string_array () {
760                 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
761         }
762
763         public static int test_0_marshal_stringbuilder_array () {
764                 StringBuilder sb1 = new StringBuilder ("ABC");
765                 StringBuilder sb2 = new StringBuilder ("DEF");
766
767                 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
768                 if (res != 0)
769                         return res;
770                 if (sb1.ToString () != "DEF")
771                         return 5;
772                 if (sb2.ToString () != "ABC")
773                         return 6;
774                 return 0;
775         }
776
777         public static int test_0_last_error () {
778                 mono_test_last_error (5);
779                 if (Marshal.GetLastWin32Error () == 5)
780                         return 0;
781                 else
782                         return 1;
783         }
784
785         public static int test_0_entry_point_not_found () {
786
787                 try {
788                         mono_entry_point_not_found ();
789                         return 1;
790                 }
791                 catch (EntryPointNotFoundException) {
792                 }
793
794                 return 0;
795         }
796
797         public static int test_0_library_not_found () {
798
799                 try {
800                         mono_library_not_found ();
801                         return 1;
802                 }
803                 catch (DllNotFoundException) {
804                 }
805
806                 return 0;
807         }
808
809         /* Check that the runtime trims .dll from the library name */
810         public static int test_0_trim_dll_from_name () {
811
812                 mono_test_marshal_char_2 ('A');
813
814                 return 0;
815         }
816
817         /* Check that the runtime adds lib to to the library name */
818         public static int test_0_add_lib_to_name () {
819
820                 mono_test_marshal_char_3 ('A');
821
822                 return 0;
823         }
824
825         class C {
826                 public int i;
827         }
828
829         public static int test_0_asany () {
830                 if (mono_test_asany (5, 1) != 0)
831                         return 1;
832
833                 if (mono_test_asany ("ABC", 2) != 0)
834                         return 2;
835
836                 SimpleStruct2 ss2 = new  SimpleStruct2 ();
837                 ss2.b = true;
838                 ss2.d = "TEST";
839                 ss2.e = 99;
840                 ss2.f = 1.5;
841                 ss2.g = 42;
842                 ss2.h = 123L;
843
844                 if (mono_test_asany (ss2, 3) != 0)
845                         return 3;
846
847                 if (mono_test_asany_unicode ("ABC", 4) != 0)
848                         return 4;
849
850                 try {
851                         C c = new C ();
852                         c.i = 5;
853                         mono_test_asany (c, 0);
854                         return 5;
855                 }
856                 catch (ArgumentException) {
857                 }
858
859                 try {
860                         mono_test_asany (new Object (), 0);
861                         return 6;
862                 }
863                 catch (ArgumentException) {
864                 }
865
866                 return 0;
867         }
868
869         /* AsAny marshalling + [In, Out] */
870
871         public static int test_0_asany_in () {
872                 // Struct
873                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
874                 mono_test_asany_in (str);
875
876                 // Formatted Class
877                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
878                 mono_test_asany_in (cls);
879                 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
880                         return 1;
881
882                 // Boxed Struct
883                 object obj = new AsAnyStruct(1,2,3, "ABC");
884                 mono_test_asany_in (obj);
885                 str = (AsAnyStruct)obj;
886                 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
887                         return 2;
888
889                 return 0;
890         }
891
892         public static int test_0_asany_out () {
893                 // Struct
894                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
895                 mono_test_asany_out (str);
896
897                 // Formatted Class
898                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
899                 mono_test_asany_out (cls);
900                 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
901                         return 1;
902
903                 // Boxed Struct
904                 object obj = new AsAnyStruct(1,2,3, "ABC");
905                 mono_test_asany_out (obj);
906                 str = (AsAnyStruct)obj;
907                 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
908                         return 2;
909
910                 return 0;
911         }
912
913         public static int test_0_asany_inout () {
914                 // Struct
915                 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
916                 mono_test_asany_inout (str);
917
918                 // Formatted Class
919                 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
920                 mono_test_asany_inout (cls);
921                 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
922                         return 1;
923
924                 // Boxed Struct
925                 object obj = new AsAnyStruct(1,2,3, "ABC");
926                 mono_test_asany_inout (obj);
927                 str = (AsAnyStruct)obj;
928                 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
929                         return 2;
930
931                 return 0;
932         }
933
934         /* Byref String Array */
935
936         [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
937         public static extern int mono_test_marshal_byref_string_array (ref string[] data);
938
939         public static int test_0_byref_string_array () {
940
941                 string[] arr = null;
942
943                 if (mono_test_marshal_byref_string_array (ref arr) != 0)
944                         return 1;
945
946                 arr = new string[] { "Alpha", "Beta", "Gamma" };
947
948                 if (mono_test_marshal_byref_string_array (ref arr) != 1)
949                         return 2;
950
951                 /* FIXME: Test returned array and out case */
952
953                 return 0;
954         }
955
956         /*
957          * AMD64 small structs-by-value tests.
958          */
959
960         /* TEST 1: 16 byte long INTEGER struct */
961
962         [StructLayout(LayoutKind.Sequential)]
963         public struct Amd64Struct1 {
964                 public int i;
965                 public int j;
966                 public int k;
967                 public int l;
968         }
969         
970         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
971         public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
972         
973         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
974         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);
975
976         public static int test_0_amd64_struct1 () {
977                 Amd64Struct1 s = new Amd64Struct1 ();
978                 s.i = 5;
979                 s.j = -5;
980                 s.k = 0xffffff;
981                 s.l = 0xfffffff;
982
983                 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
984
985                 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
986         }
987
988         public static int test_0_amd64_struct1_many_args () {
989                 Amd64Struct1 s = new Amd64Struct1 ();
990                 s.i = 5;
991                 s.j = -5;
992                 s.k = 0xffffff;
993                 s.l = 0xfffffff;
994
995                 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
996
997                 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
998         }
999
1000         /* TEST 2: 8 byte long INTEGER struct */
1001
1002         [StructLayout(LayoutKind.Sequential)]
1003         public struct Amd64Struct2 {
1004                 public int i;
1005                 public int j;
1006         }
1007         
1008         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1009         public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1010
1011         public static int test_0_amd64_struct2 () {
1012                 Amd64Struct2 s = new Amd64Struct2 ();
1013                 s.i = 5;
1014                 s.j = -5;
1015
1016                 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1017
1018                 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1019         }
1020
1021         /* TEST 3: 4 byte long INTEGER struct */
1022
1023         [StructLayout(LayoutKind.Sequential)]
1024         public struct Amd64Struct3 {
1025                 public int i;
1026         }
1027         
1028         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1029         public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1030
1031         public static int test_0_amd64_struct3 () {
1032                 Amd64Struct3 s = new Amd64Struct3 ();
1033                 s.i = -5;
1034
1035                 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1036
1037                 return (s2.i == -4) ? 0 : 1;
1038         }
1039
1040         /* Test 4: 16 byte long FLOAT struct */
1041
1042         [StructLayout(LayoutKind.Sequential)]
1043         public struct Amd64Struct4 {
1044                 public double d1, d2;
1045         }
1046         
1047         [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1048         public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1049
1050         public static int test_0_amd64_struct4 () {
1051                 Amd64Struct4 s = new Amd64Struct4 ();
1052                 s.d1 = 5.0;
1053                 s.d2 = -5.0;
1054
1055                 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1056
1057                 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1058         }
1059
1060         /*
1061          * IA64 struct tests
1062          */
1063
1064         /* Test 5: Float HFA */
1065
1066         [StructLayout(LayoutKind.Sequential)]
1067         public struct TestStruct5 {
1068                 public float d1, d2;
1069         }
1070         
1071         [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1072         public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1073
1074         public static int test_0_ia64_struct5 () {
1075                 TestStruct5 s = new TestStruct5 ();
1076                 s.d1 = 5.0f;
1077                 s.d2 = -5.0f;
1078
1079                 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1080
1081                 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1082         }
1083
1084         /* Test 6: Double HFA */
1085
1086         [StructLayout(LayoutKind.Sequential)]
1087         public struct TestStruct6 {
1088                 public double d1, d2;
1089         }
1090         
1091         [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1092         public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1093
1094         public static int test_0_ia64_struct6 () {
1095                 TestStruct6 s = new TestStruct6 ();
1096                 s.d1 = 6.0;
1097                 s.d2 = -6.0;
1098
1099                 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1100
1101                 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1102         }
1103         
1104         /* Blittable class */
1105         [DllImport("libtest")]
1106         private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1107
1108         public static int test_0_marshal_blittable_class () {
1109                 BlittableClass v1 = new BlittableClass ();
1110
1111                 /* Since it is blittable, it looks like it is passed as in/out */
1112                 BlittableClass v2 = TestBlittableClass (v1);
1113
1114                 if (v1.a != 2 || v1.b != 3)
1115                         return 1;
1116                 
1117                 if (v2.a != 2 || v2.b != 3)
1118                         return 2;
1119
1120                 // Test null
1121                 BlittableClass v3 = TestBlittableClass (null);
1122
1123                 if (v3.a != 42 || v3.b != 43)
1124                         return 3;
1125                 
1126                 return 0;
1127         }
1128
1129         public static int test_0_marshal_byval_class () {
1130                 SimpleObj obj0 = new SimpleObj ();
1131                 obj0.str = "T1";
1132                 obj0.i = 4;
1133                 
1134                 if (class_marshal_test0 (obj0) != 0)
1135                         return 1;
1136
1137                 return 0;
1138         }
1139
1140         public static int test_0_marshal_byval_class_null () {
1141                 if (class_marshal_test4 (null) != 0)
1142                         return 1;
1143
1144                 return 0;
1145         }
1146
1147         public static int test_0_marshal_out_class () {
1148                 SimpleObj obj1;
1149
1150                 class_marshal_test1 (out obj1);
1151
1152                 if (obj1.str != "ABC")
1153                         return 1;
1154
1155                 if (obj1.i != 5)
1156                         return 2;
1157
1158                 return 0;
1159         }
1160
1161         public static int test_0_marshal_string () {
1162                 return string_marshal_test0 ("TEST0");
1163         }
1164
1165         public static int test_0_marshal_out_string () {
1166                 string res;
1167                 
1168                 string_marshal_test1 (out res);
1169
1170                 if (res != "TEST1")
1171                         return 1;
1172
1173                 return 0;
1174         }
1175
1176         public static int test_0_marshal_byref_string () {
1177                 string res = "TEST1";
1178
1179                 return string_marshal_test2 (ref res);
1180         }
1181
1182         public static int test_0_marshal_null_string () {
1183                 return string_marshal_test3 (null);
1184         }
1185
1186         [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1187         public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1188
1189         public static int test_0_stdcall_name_mangling () {
1190                 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1191         }
1192
1193         /* Float test */
1194
1195         [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1196         public static extern float mono_test_marshal_pass_return_float (float f);
1197
1198         public static int test_0_pass_return_float () {
1199                 float f = mono_test_marshal_pass_return_float (1.5f);
1200
1201                 return (f == 2.5f) ? 0 : 1;
1202         }
1203
1204         /*
1205          * Pointers to structures can not be passed
1206          */
1207
1208         public struct CharInfo {
1209                 public char Character;
1210                 public short Attributes;
1211         }
1212
1213         [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1214         public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1215
1216         public static unsafe int test_0_marshal_ptr_to_struct () {
1217                 CharInfo [] buffer = new CharInfo [1];
1218                 fixed (CharInfo *ptr = &buffer [0]) {
1219                         try {
1220                                 mono_test_marshal_ptr_to_struct (ptr);
1221                                 return 1;
1222                         }
1223                         catch (MarshalDirectiveException) {
1224                                 return 0;
1225                         }
1226                 }
1227                 return 1;
1228         }
1229
1230         /*
1231          * LPWStr marshalling
1232          */
1233
1234         [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1235         [return: MarshalAs(UnmanagedType.LPWStr)]
1236         private static extern string mono_test_marshal_lpwstr_marshal(
1237                 [MarshalAs(UnmanagedType.LPWStr)] string s,
1238                 int length );
1239
1240         [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1241         private static extern string mono_test_marshal_lpwstr_marshal2(
1242                 string s,
1243                 int length );
1244
1245         [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1246         private static extern void mono_test_marshal_lpwstr_out_marshal(
1247                 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1248
1249         [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1250         private static extern void mono_test_marshal_lpwstr_out_marshal2(
1251                 out string s);
1252
1253         public static int test_0_pass_return_lwpstr () {
1254                 string s;
1255                 
1256                 mono_test_marshal_lpwstr_out_marshal (out s);
1257
1258                 if (s != "ABC")
1259                         return 1;
1260
1261                 s = null;
1262                 mono_test_marshal_lpwstr_out_marshal2 (out s);
1263
1264                 if (s != "ABC")
1265                         return 2;
1266                 
1267                 return 0;               
1268         }
1269
1270         public static int test_0_out_lwpstr () {
1271                 string s = "ABC";
1272                 
1273                 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1274
1275                 if (res != "ABC")
1276                         return 1;
1277
1278                 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1279
1280                 if (res2 != "ABC")
1281                         return 2;
1282                 
1283                 return 0;               
1284         }
1285
1286         /*
1287          * Byref bool marshalling
1288          */
1289
1290         [DllImport("libtest")]
1291         extern static int marshal_test_ref_bool
1292         (
1293                 int i, 
1294                 [MarshalAs(UnmanagedType.I1)] ref bool b1, 
1295                 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2, 
1296                 ref bool b3
1297         );
1298
1299         public static int test_0_pass_byref_bool () {
1300                 for (int i = 0; i < 8; i++)
1301                 {
1302                         bool b1 = (i & 4) != 0;
1303                         bool b2 = (i & 2) != 0;
1304                         bool b3 = (i & 1) != 0;
1305                         bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1306                         if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1307                                 return 4 * i + 1;
1308                         if (b1 != !orig_b1)
1309                                 return 4 * i + 2;
1310                         if (b2 != !orig_b2)
1311                                 return 4 * i + 3;
1312                         if (b3 != !orig_b3)
1313                                 return 4 * i + 4;
1314                 }
1315
1316                 return 0;
1317         }
1318
1319         /*
1320          * Bool struct field marshalling
1321          */
1322
1323         struct BoolStruct
1324         {
1325                 public int i;
1326                 [MarshalAs(UnmanagedType.I1)] public bool b1;
1327                 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1328                 public bool b3;
1329         }
1330
1331         [DllImport("libtest")]
1332         extern static int marshal_test_bool_struct(ref BoolStruct s);
1333
1334         public static int test_0_pass_bool_in_struct () {
1335                 for (int i = 0; i < 8; i++)
1336                 {
1337                         BoolStruct s = new BoolStruct();
1338                         s.i = i;
1339                         s.b1 = (i & 4) != 0;
1340                         s.b2 = (i & 2) != 0;
1341                         s.b3 = (i & 1) != 0;
1342                         BoolStruct orig = s;
1343                         if (marshal_test_bool_struct(ref s) != 0)
1344                                 return 4 * i + 33;
1345                         if (s.b1 != !orig.b1)
1346                                 return 4 * i + 34;
1347                         if (s.b2 != !orig.b2)
1348                                 return 4 * i + 35;
1349                         if (s.b3 != !orig.b3)
1350                                 return 4 * i + 36;
1351                 }
1352
1353                 return 0;
1354         }
1355
1356         /*
1357          * Invoking pinvoke methods through delegates
1358          */
1359
1360         delegate int MyDelegate (string name);
1361
1362         [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1363         public static extern int puts_static (string name);
1364
1365         public static int test_0_invoke_pinvoke_through_delegate () {
1366                 puts_static ("A simple Test for PInvoke 1");
1367
1368                 MyDelegate d = new MyDelegate (puts_static);
1369                 d ("A simple Test for PInvoke 2");
1370
1371                 object [] args = {"A simple Test for PInvoke 3"};
1372                 d.DynamicInvoke (args);
1373
1374                 return 0;
1375         }
1376
1377         /*
1378          * Missing virtual pinvoke methods
1379          */
1380
1381         public class T {
1382
1383                 public virtual object MyClone ()
1384                 {
1385                         return null;
1386                 }
1387         }
1388
1389         public class T2 : T {
1390                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1391                 public override extern object MyClone ();
1392         }
1393
1394         public static int test_0_missing_virtual_pinvoke_method () {
1395                 T2 t = new T2 ();
1396
1397                 try {
1398                         t.MyClone ();
1399                 } catch (Exception ex) {
1400                         return 0;
1401                 }
1402                 
1403                 return 1;
1404         }
1405 }
1406