Merge changes, that were only made in corlib, back into Mono.Security
[mono-project.git] / mono / tests / pinvoke3.cs
blobc5e7fa6ad6704ef86cd73389f52c926f271c7bd3
1 //
2 // pinvoke3.cs:
3 //
4 // Tests for native->managed marshalling
5 //
7 using System;
8 using System.Text;
9 using System.Runtime.InteropServices;
10 using System.Threading;
12 public class Tests {
14 [StructLayout (LayoutKind.Sequential)]
15 public struct SimpleStruct {
16 public bool a;
17 public bool b;
18 public bool c;
19 public string d;
20 [MarshalAs(UnmanagedType.LPWStr)]
21 public string d2;
24 [StructLayout (LayoutKind.Sequential)]
25 public class SimpleClass {
26 public bool a;
27 public bool b;
28 public bool c;
29 public string d;
32 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
34 SimpleStruct res;
36 res.a = !ss.a;
37 res.b = !ss.b;
38 res.c = !ss.c;
39 res.d = ss.d + "-RES";
40 res.d2 = ss.d2 + "-RES";
42 return res;
45 public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
47 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
48 ss.a = true;
49 ss.b = true;
50 ss.c = true;
51 ss.d = "TEST3";
52 return 0;
55 return 1;
58 public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
60 ss.a = true;
61 ss.b = true;
62 ss.c = true;
63 ss.d = "TEST3";
64 ss.d2 = "TEST4";
66 return 0;
69 public static SimpleClass delegate_test_class (SimpleClass ss)
71 if (ss == null)
72 return null;
74 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
75 return null;
77 SimpleClass res = ss;
79 return res;
82 public static int delegate_test_class_byref (ref SimpleClass ss)
84 if (ss == null)
85 return -1;
87 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
88 ss.a = true;
89 ss.b = false;
90 ss.c = true;
91 ss.d = "RES";
93 return 0;
96 return 1;
99 public static int delegate_test_class_out (out SimpleClass ss)
101 ss = new SimpleClass ();
102 ss.a = true;
103 ss.b = false;
104 ss.c = true;
105 ss.d = "RES";
107 return 0;
110 public static int delegate_test_primitive_byref (ref int i)
112 if (i != 1)
113 return 1;
115 i = 2;
116 return 0;
119 public static int delegate_test_string_marshalling (string s)
121 return s == "ABC" ? 0 : 1;
124 public static int delegate_test_string_builder_marshalling (StringBuilder s)
126 if (s == null)
127 return 2;
128 else
129 return s.ToString () == "ABC" ? 0 : 1;
132 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
133 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
135 public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
137 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
138 public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
140 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
141 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
143 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
144 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
146 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
147 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
149 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
150 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
152 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
153 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
155 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
156 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
158 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
159 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
161 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
162 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
164 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8")]
165 public static extern int mono_test_marshal_delegate11 (SimpleDelegate11 d, string s);
167 [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
168 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
170 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
171 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
173 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
175 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
177 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
179 public delegate int SimpleDelegate5 (ref SimpleClass ss);
181 public delegate int SimpleDelegate7 (out SimpleClass ss);
183 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
185 public delegate int return_int_delegate (int i);
187 public delegate int SimpleDelegate9 (return_int_delegate del);
189 public delegate int SimpleDelegate11 (StringBuilder s1);
191 public delegate int PrimitiveByrefDelegate (ref int i);
193 public delegate return_int_delegate ReturnDelegateDelegate ();
195 public static int Main () {
196 return TestDriver.RunTests (typeof (Tests));
199 /* Test structures as arguments and return values of delegates */
200 public static int test_0_marshal_struct_delegate () {
201 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
203 return mono_test_marshal_delegate2 (d);
206 /* Test structures as byref arguments of delegates */
207 public static int test_0_marshal_byref_struct_delegate () {
208 SimpleStruct ss = new SimpleStruct ();
209 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
211 ss.b = true;
212 ss.d = "TEST1";
214 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
215 return 1;
217 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
218 return 2;
220 return 0;
223 /* Test structures as out arguments of delegates */
224 public static int test_0_marshal_out_struct_delegate () {
225 SimpleStruct ss = new SimpleStruct ();
226 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
228 return mono_test_marshal_out_struct (1, out ss, 2, d);
231 /* Test classes as arguments and return values of delegates */
232 public static int test_0_marshal_class_delegate () {
233 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
235 return mono_test_marshal_delegate4 (d);
238 /* Test classes as byref arguments of delegates */
239 public static int test_0_marshal_byref_class_delegate () {
240 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
242 return mono_test_marshal_delegate5 (d);
245 /* Test classes as out arguments of delegates */
246 public static int test_0_marshal_out_class_delegate () {
247 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
249 return mono_test_marshal_delegate7 (d);
252 /* Test string marshalling with delegates */
253 public static int test_0_marshal_string_delegate () {
254 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
256 return mono_test_marshal_delegate8 (d, "ABC");
259 /* Test string builder marshalling with delegates */
260 public static int test_0_marshal_string_builder_delegate () {
261 SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
263 if (mono_test_marshal_delegate11 (d, null) != 2)
264 return 2;
266 return mono_test_marshal_delegate11 (d, "ABC");
269 /* Test that the delegate wrapper correctly catches null byref arguments */
270 public static int test_0_marshal_byref_class_delegate_null () {
271 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
273 try {
274 mono_test_marshal_delegate6 (d);
275 return 1;
277 catch (ArgumentNullException ex) {
278 return 0;
282 static int return_self (int i) {
283 return i;
286 static int call_int_delegate (return_int_delegate d) {
287 return d (55);
290 public static int test_55_marshal_delegate_delegate () {
291 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
293 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
296 public static int test_0_marshal_primitive_byref_delegate () {
297 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
299 return mono_test_marshal_primitive_byref_delegate (d);
302 public static return_int_delegate return_delegate () {
303 return new return_int_delegate (return_self);
306 public static int test_55_marshal_return_delegate_delegate () {
307 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
310 /* Passing and returning strings */
312 public delegate String ReturnStringDelegate (String s);
314 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
315 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
317 public static String managed_return_string (String s) {
318 if (s != "TEST")
319 return "";
320 else
321 return "12345";
324 public static int test_0_marshal_return_string_delegate () {
325 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
326 String s = mono_test_marshal_return_string_delegate (d);
328 return (s == "12345") ? 0 : 1;
331 /* Passing and returning enums */
333 public enum FooEnum {
334 Foo1,
335 Foo2,
336 Foo3
339 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
341 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
342 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
344 public static FooEnum managed_return_enum (FooEnum e) {
345 return (FooEnum)((int)e + 1);
348 public static int test_0_marshal_return_enum_delegate () {
349 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
350 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
352 return e == FooEnum.Foo3 ? 0 : 1;
355 /* Passing and returning blittable structs */
357 [StructLayout (LayoutKind.Sequential)]
358 public struct BlittableStruct {
359 public int a, b, c;
360 public long d;
363 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
365 BlittableStruct res;
367 res.a = -ss.a;
368 res.b = -ss.b;
369 res.c = -ss.c;
370 res.d = -ss.d;
372 return res;
375 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
377 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
378 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
380 public static int test_0_marshal_blittable_struct_delegate () {
381 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
385 * Passing and returning small structs
388 /* TEST 1: 4 byte long INTEGER struct */
390 [StructLayout (LayoutKind.Sequential)]
391 public struct SmallStruct1 {
392 public int i;
395 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
396 SmallStruct1 res;
398 res.i = -ss.i;
400 return res;
403 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
405 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
406 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
408 public static int test_0_marshal_small_struct_delegate1 () {
409 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
412 /* TEST 2: 2+2 byte long INTEGER struct */
414 [StructLayout (LayoutKind.Sequential)]
415 public struct SmallStruct2 {
416 public short i, j;
419 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
420 SmallStruct2 res;
422 res.i = (short)-ss.i;
423 res.j = (short)-ss.j;
425 return res;
428 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
430 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
431 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
433 public static int test_0_marshal_small_struct_delegate2 () {
434 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
437 /* TEST 3: 2+1 byte long INTEGER struct */
439 [StructLayout (LayoutKind.Sequential)]
440 public struct SmallStruct3 {
441 public short i;
442 public byte j;
445 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
446 SmallStruct3 res;
448 res.i = (short)-ss.i;
449 res.j = (byte)-ss.j;
451 return res;
454 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
456 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
457 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
459 public static int test_0_marshal_small_struct_delegate3 () {
460 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
463 /* TEST 4: 2 byte long INTEGER struct */
465 [StructLayout (LayoutKind.Sequential)]
466 public struct SmallStruct4 {
467 public short i;
470 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
471 SmallStruct4 res;
473 res.i = (short)-ss.i;
475 return res;
478 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
480 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
481 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
483 public static int test_0_marshal_small_struct_delegate4 () {
484 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
487 /* TEST 5: 8 byte long INTEGER struct */
489 [StructLayout (LayoutKind.Sequential)]
490 public struct SmallStruct5 {
491 public long l;
494 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
495 SmallStruct5 res;
497 res.l = -ss.l;
499 return res;
502 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
504 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
505 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
507 public static int test_0_marshal_small_struct_delegate5 () {
508 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
511 /* TEST 6: 4+4 byte long INTEGER struct */
513 [StructLayout (LayoutKind.Sequential)]
514 public struct SmallStruct6 {
515 public int i, j;
518 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
519 SmallStruct6 res;
521 res.i = -ss.i;
522 res.j = -ss.j;
524 return res;
527 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
529 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
530 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
532 public static int test_0_marshal_small_struct_delegate6 () {
533 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
536 /* TEST 7: 4+2 byte long INTEGER struct */
538 [StructLayout (LayoutKind.Sequential)]
539 public struct SmallStruct7 {
540 public int i;
541 public short j;
544 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
545 SmallStruct7 res;
547 res.i = -ss.i;
548 res.j = (short)-ss.j;
550 return res;
553 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
555 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
556 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
558 public static int test_0_marshal_small_struct_delegate7 () {
559 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
562 /* TEST 8: 4 byte long FLOAT struct */
564 [StructLayout (LayoutKind.Sequential)]
565 public struct SmallStruct8 {
566 public float i;
569 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
570 SmallStruct8 res;
572 res.i = -ss.i;
574 return res;
577 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
579 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
580 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
582 public static int test_0_marshal_small_struct_delegate8 () {
583 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
586 /* TEST 9: 8 byte long FLOAT struct */
588 [StructLayout (LayoutKind.Sequential)]
589 public struct SmallStruct9 {
590 public double i;
593 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
594 SmallStruct9 res;
596 res.i = -ss.i;
598 return res;
601 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
603 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
604 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
606 public static int test_0_marshal_small_struct_delegate9 () {
607 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
610 /* TEST 10: 4+4 byte long FLOAT struct */
612 [StructLayout (LayoutKind.Sequential)]
613 public struct SmallStruct10 {
614 public float i;
615 public float j;
618 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
619 SmallStruct10 res;
621 res.i = -ss.i;
622 res.j = -ss.j;
624 return res;
627 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
629 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
630 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
632 public static int test_0_marshal_small_struct_delegate10 () {
633 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
636 /* TEST 11: 4+4 byte long MIXED struct */
638 [StructLayout (LayoutKind.Sequential)]
639 public struct SmallStruct11 {
640 public float i;
641 public int j;
644 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
645 SmallStruct11 res;
647 res.i = -ss.i;
648 res.j = -ss.j;
650 return res;
653 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
655 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
656 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
658 public static int test_0_marshal_small_struct_delegate11 () {
659 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
663 * Passing arrays
665 public delegate int ArrayDelegate1 (int i,
666 string j,
667 [In, MarshalAs(UnmanagedType.LPArray,
668 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
670 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
671 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
673 public static int array_delegate1 (int i, string j, string[] arr) {
674 if (arr.Length != 2)
675 return 1;
676 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
677 return 2;
678 return 0;
681 public static int test_0_marshal_array_delegate_string () {
682 string[] arr = new string [] { "ABC", "DEF" };
683 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
686 public static int array_delegate2 (int i, string j, string[] arr) {
687 return (arr == null) ? 0 : 1;
690 public static int test_0_marshal_array_delegate_null () {
691 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
694 public delegate int ArrayDelegate3 (int i,
695 string j,
696 [In, MarshalAs(UnmanagedType.LPArray,
697 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
699 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
700 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
702 public static int array_delegate3 (int i, string j, string[] arr) {
703 return (arr == null) ? 0 : 1;
706 public static int test_0_marshal_array_delegate_bad_paramindex () {
707 try {
708 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
709 return 1;
711 catch (MarshalDirectiveException) {
712 return 0;
716 public delegate int ArrayDelegate4 (int i,
717 string j,
718 [In, MarshalAs(UnmanagedType.LPArray,
719 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
721 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
722 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
724 public static int array_delegate4 (int i, string j, string[] arr) {
725 return (arr == null) ? 0 : 1;
728 public static int test_0_marshal_array_delegate_bad_paramtype () {
729 try {
730 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
731 return 1;
733 catch (MarshalDirectiveException) {
734 return 0;
738 public delegate int ArrayDelegate4_2 (int i,
739 string j,
740 string[] arr);
742 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
743 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
745 public static int array_delegate4_2 (int i, string j, string[] arr) {
746 return (arr == null) ? 0 : 1;
749 public static int test_0_marshal_array_delegate_no_marshal_directive () {
750 try {
751 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
752 return 1;
754 catch (MarshalDirectiveException) {
755 return 0;
759 public delegate int ArrayDelegate4_3 (int i,
760 string j,
761 string[] arr);
763 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
764 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
766 public int array_delegate4_3 (int i, string j, string[] arr) {
767 return (arr == null) ? 0 : 1;
770 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
771 try {
772 Tests t = new Tests ();
773 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
774 return 1;
776 catch (MarshalDirectiveException) {
777 return 0;
781 public delegate int ArrayDelegate5 (int i,
782 string j,
783 [In, MarshalAs(UnmanagedType.LPArray,
784 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
786 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
787 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
789 public static int array_delegate5 (int i, string j, string[] arr) {
790 if (arr.Length != 2)
791 return 1;
792 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
793 return 2;
794 return 0;
797 public static int test_0_marshal_array_delegate_unicode_string () {
798 string[] arr = new string [] { "ABC", "DEF" };
799 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
802 public delegate int ArrayDelegate6 (int i,
803 string j,
804 [In, MarshalAs(UnmanagedType.LPArray,
805 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
807 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
808 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
810 public static int array_delegate6 (int i, string j, string[] arr) {
811 if (arr.Length != 2)
812 return 1;
813 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
814 return 2;
815 return 0;
818 public static int test_0_marshal_array_delegate_sizeconst () {
819 string[] arr = new string [] { "ABC", "DEF" };
820 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
823 public delegate int ArrayDelegate7 (int i,
824 string j,
825 [In, MarshalAs(UnmanagedType.LPArray,
826 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
828 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
829 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
831 public static int array_delegate7 (int i, string j, string[] arr) {
832 if (arr.Length != 2)
833 return 1;
834 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
835 return 2;
836 return 0;
839 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
840 string[] arr = new string [] { "ABC", "DEF" };
841 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
844 public delegate int ArrayDelegate8 (int i, string j,
845 [In, MarshalAs(UnmanagedType.LPArray,
846 SizeParamIndex=0)]
847 int[] arr);
849 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
850 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
852 public static int array_delegate8 (int i, string j, int[] arr) {
853 if (arr.Length != 2)
854 return 1;
855 if ((arr [0] != 42) || (arr [1] != 43))
856 return 2;
857 return 0;
860 public static int test_0_marshal_array_delegate_blittable () {
861 int[] arr = new int [] { 42, 43 };
862 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
865 /* Array with size param of type long */
867 public delegate int ArrayDelegate8_2 (long i,
868 string j,
869 [In, MarshalAs(UnmanagedType.LPArray,
870 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
872 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
873 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
875 public static int array_delegate8_2 (long i, string j, string[] arr) {
876 if (arr.Length != 2)
877 return 1;
878 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
879 return 2;
880 return 0;
883 public static int test_0_marshal_array_delegate_long_param () {
884 string[] arr = new string [] { "ABC", "DEF" };
885 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
890 * [Out] blittable arrays
893 public delegate int ArrayDelegate9 (int i, string j,
894 [Out, MarshalAs(UnmanagedType.LPArray,
895 SizeParamIndex=0)]
896 int[] arr);
898 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
899 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
901 public static int array_delegate9 (int i, string j, int[] arr) {
902 if (arr.Length != 2)
903 return 1;
905 arr [0] = 1;
906 arr [1] = 2;
908 return 0;
911 public static int test_0_marshal_out_array_delegate () {
912 int[] arr = new int [] { 42, 43 };
913 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
917 * [Out] string arrays
920 public delegate int ArrayDelegate10 (int i,
921 string j,
922 [Out, MarshalAs(UnmanagedType.LPArray,
923 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
925 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
926 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
928 public static int array_delegate10 (int i, string j, string[] arr) {
929 if (arr.Length != 2)
930 return 1;
932 arr [0] = "ABC";
933 arr [1] = "DEF";
935 return 0;
938 public static int test_0_marshal_out_string_array_delegate () {
939 string[] arr = new string [] { "", "" };
940 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
944 * [In, Out] classes
947 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
949 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
950 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
952 public static int delegate_test_byval_class_inout (SimpleClass ss) {
953 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
954 return 1;
956 ss.a = true;
957 ss.b = false;
958 ss.c = true;
959 ss.d = "RES";
961 return 0;
964 public static int test_0_marshal_inout_byval_class_delegate () {
965 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
969 * Returning unicode strings
971 [return: MarshalAs(UnmanagedType.LPWStr)]
972 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
974 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
975 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
977 public static String return_unicode_string_delegate (string message) {
978 return message;
981 public static int test_0_marshal_return_unicode_string_delegate () {
982 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
986 * Returning string arrays
988 public delegate string[] ReturnArrayDelegate (int i);
990 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
991 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
993 public static String[] return_array_delegate (int i) {
994 String[] arr = new String [2];
996 arr [0] = "ABC";
997 arr [1] = "DEF";
999 return arr;
1002 public static String[] return_array_delegate_null (int i) {
1003 return null;
1006 public static int test_0_marshal_return_string_array_delegate () {
1007 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1010 public static int test_3_marshal_return_string_array_delegate_null () {
1011 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1015 * Byref string marshalling
1017 public delegate int ByrefStringDelegate (ref string s);
1019 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1020 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1022 public static int byref_string_delegate (ref string s) {
1023 if (s != "ABC")
1024 return 1;
1026 s = "DEF";
1028 return 0;
1031 public static int test_0_marshal_byref_string_delegate () {
1032 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1036 * Thread attach
1039 public delegate int SimpleDelegate (int i);
1041 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1042 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1044 public static int test_43_thread_attach () {
1045 int res = mono_test_marshal_thread_attach (delegate (int i) {
1046 if (!Thread.CurrentThread.IsBackground)
1047 return 0;
1048 return i + 1;
1050 return res;
1054 * Appdomain save/restore
1056 static Func<int> callback;
1058 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1059 public static extern int mono_test_marshal_set_callback (Func<int> a);
1061 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1062 public static extern int mono_test_marshal_call_callback ();
1064 public static int test_0_appdomain_swich () {
1065 callback = delegate () { return 42; };
1066 mono_test_marshal_set_callback (callback);
1068 // FIXME: The appdomain unload hangs
1069 return 0;
1071 AppDomain ad = AppDomain.CreateDomain ("foo");
1072 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1073 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1074 int res = c.OtherDomainTest ();
1075 AppDomain.Unload (ad);
1076 return res;
1080 class CallbackClass : MarshalByRefObject {
1081 public int OtherDomainTest () {
1082 int appDomainId = AppDomain.CurrentDomain.Id;
1083 int res = mono_test_marshal_call_callback ();
1084 if (res != 42)
1085 return 2;
1086 return appDomainId == AppDomain.CurrentDomain.Id ? 0 : 1;