4 // Tests for native->managed marshalling
9 using System.Runtime.InteropServices;
10 using System.Threading;
14 [StructLayout (LayoutKind.Sequential)]
15 public struct SimpleStruct {
20 [MarshalAs(UnmanagedType.LPWStr)]
24 [StructLayout (LayoutKind.Sequential)]
25 public class SimpleClass {
32 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
39 res.d = ss.d + "-RES";
40 res.d2 = ss.d2 + "-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") {
58 public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
69 public static int delegate_test_struct_in (int a, [In] ref SimpleStruct ss, int b)
71 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
82 public static SimpleClass delegate_test_class (SimpleClass ss)
87 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
95 public static int delegate_test_class_byref (ref SimpleClass ss)
100 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
112 public static int delegate_test_class_out (out SimpleClass ss)
114 ss = new SimpleClass ();
123 public static int delegate_test_primitive_byref (ref int i)
132 public static int delegate_test_string_marshalling (string s)
134 return s == "ABC" ? 0 : 1;
137 public static int delegate_test_string_builder_marshalling (StringBuilder s)
142 return s.ToString () == "ABC" ? 0 : 1;
145 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
146 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
148 public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
150 public delegate int InStructDelegate (int a, [In] ref SimpleStruct ss, int b);
152 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
153 public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
155 [DllImport ("libtest", EntryPoint="mono_test_marshal_in_struct")]
156 public static extern int mono_test_marshal_in_struct (int a, ref SimpleStruct ss, int b, InStructDelegate d);
158 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
159 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
161 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
162 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
164 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
165 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
167 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
168 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
170 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
171 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
173 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
174 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
176 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
177 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
179 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
180 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
182 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8")]
183 public static extern int mono_test_marshal_delegate11 (SimpleDelegate11 d, string s);
185 [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
186 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
188 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
189 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
191 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_ref_delegate")]
192 public static extern int mono_test_marshal_delegate_ref_delegate (DelegateByrefDelegate del);
194 [DllImport ("libtest", EntryPoint="mono_test_marshal_virtual_delegate")]
195 public static extern int mono_test_marshal_virtual_delegate (VirtualDelegate del);
197 [DllImport ("libtest", EntryPoint="mono_test_marshal_icall_delegate")]
198 public static extern int mono_test_marshal_icall_delegate (IcallDelegate del);
200 public delegate string IcallDelegate (IntPtr p);
202 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
204 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
206 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
208 public delegate int SimpleDelegate5 (ref SimpleClass ss);
210 public delegate int SimpleDelegate7 (out SimpleClass ss);
212 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
214 public delegate int return_int_delegate (int i);
216 public delegate int SimpleDelegate9 (return_int_delegate del);
218 public delegate int SimpleDelegate11 (StringBuilder s1);
220 public delegate int PrimitiveByrefDelegate (ref int i);
222 public delegate return_int_delegate ReturnDelegateDelegate ();
224 public delegate int DelegateByrefDelegate (ref return_int_delegate del);
226 public delegate int VirtualDelegate (int i);
228 public static int Main () {
229 return TestDriver.RunTests (typeof (Tests));
232 /* Test structures as arguments and return values of delegates */
233 public static int test_0_marshal_struct_delegate () {
234 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
236 return mono_test_marshal_delegate2 (d);
239 /* Test structures as byref arguments of delegates */
240 public static int test_0_marshal_byref_struct_delegate () {
241 SimpleStruct ss = new SimpleStruct ();
242 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
247 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
250 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
256 /* Test structures as out arguments of delegates */
257 public static int test_0_marshal_out_struct_delegate () {
258 SimpleStruct ss = new SimpleStruct ();
259 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
261 return mono_test_marshal_out_struct (1, out ss, 2, d);
264 /* Test structures as in arguments of delegates */
265 public static int test_0_marshal_in_struct_delegate () {
266 SimpleStruct ss = new SimpleStruct () { a = true, b = false, c = true, d = "TEST2" };
267 InStructDelegate d = new InStructDelegate (delegate_test_struct_in);
269 return mono_test_marshal_in_struct (1, ref ss, 2, d);
272 /* Test classes as arguments and return values of delegates */
273 public static int test_0_marshal_class_delegate () {
274 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
276 return mono_test_marshal_delegate4 (d);
279 /* Test classes as byref arguments of delegates */
280 public static int test_0_marshal_byref_class_delegate () {
281 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
283 return mono_test_marshal_delegate5 (d);
286 /* Test classes as out arguments of delegates */
287 public static int test_0_marshal_out_class_delegate () {
288 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
290 return mono_test_marshal_delegate7 (d);
293 /* Test string marshalling with delegates */
294 public static int test_0_marshal_string_delegate () {
295 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
297 return mono_test_marshal_delegate8 (d, "ABC");
300 /* Test string builder marshalling with delegates */
301 public static int test_0_marshal_string_builder_delegate () {
302 SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
304 if (mono_test_marshal_delegate11 (d, null) != 2)
307 return mono_test_marshal_delegate11 (d, "ABC");
310 /* Test that the delegate wrapper correctly catches null byref arguments */
311 public static int test_0_marshal_byref_class_delegate_null () {
312 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
315 mono_test_marshal_delegate6 (d);
318 catch (ArgumentNullException ex) {
323 static int return_self (int i) {
327 static int call_int_delegate (return_int_delegate d) {
331 public static int test_55_marshal_delegate_delegate () {
332 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
334 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
337 public static int test_0_marshal_primitive_byref_delegate () {
338 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
340 return mono_test_marshal_primitive_byref_delegate (d);
343 public static return_int_delegate return_delegate () {
344 return new return_int_delegate (return_self);
347 public static int test_55_marshal_return_delegate_delegate () {
348 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
351 public static int return_plus_1 (int i) {
355 public static int ref_delegate_delegate (ref return_int_delegate del) {
360 public static int test_55_marshal_delegate_ref_delegate () {
361 var del = new DelegateByrefDelegate (ref_delegate_delegate);
362 return mono_test_marshal_delegate_ref_delegate (del);
365 /* Passing and returning strings */
367 public delegate String ReturnStringDelegate (String s);
369 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
370 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
372 public static String managed_return_string (String s) {
379 public static int test_0_marshal_return_string_delegate () {
380 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
381 String s = mono_test_marshal_return_string_delegate (d);
383 return (s == "12345") ? 0 : 1;
386 /* Passing and returning enums */
388 public enum FooEnum {
394 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
396 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
397 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
399 public static FooEnum managed_return_enum (FooEnum e) {
400 return (FooEnum)((int)e + 1);
403 public static int test_0_marshal_return_enum_delegate () {
404 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
405 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
407 return e == FooEnum.Foo3 ? 0 : 1;
410 /* Passing and returning blittable structs */
412 [StructLayout (LayoutKind.Sequential)]
413 public struct BlittableStruct {
418 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
430 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
432 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
433 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
435 public static int test_0_marshal_blittable_struct_delegate () {
436 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
440 * Passing and returning small structs
443 /* TEST 1: 4 byte long INTEGER struct */
445 [StructLayout (LayoutKind.Sequential)]
446 public struct SmallStruct1 {
450 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
458 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
460 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
461 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
463 public static int test_0_marshal_small_struct_delegate1 () {
464 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
467 /* TEST 2: 2+2 byte long INTEGER struct */
469 [StructLayout (LayoutKind.Sequential)]
470 public struct SmallStruct2 {
474 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
477 res.i = (short)-ss.i;
478 res.j = (short)-ss.j;
483 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
485 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
486 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
488 public static int test_0_marshal_small_struct_delegate2 () {
489 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
492 /* TEST 3: 2+1 byte long INTEGER struct */
494 [StructLayout (LayoutKind.Sequential)]
495 public struct SmallStruct3 {
500 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
503 res.i = (short)-ss.i;
509 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
511 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
512 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
514 public static int test_0_marshal_small_struct_delegate3 () {
515 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
518 /* TEST 4: 2 byte long INTEGER struct */
520 [StructLayout (LayoutKind.Sequential)]
521 public struct SmallStruct4 {
525 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
528 res.i = (short)-ss.i;
533 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
535 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
536 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
538 public static int test_0_marshal_small_struct_delegate4 () {
539 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
542 /* TEST 5: 8 byte long INTEGER struct */
544 [StructLayout (LayoutKind.Sequential)]
545 public struct SmallStruct5 {
549 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
557 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
559 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
560 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
562 public static int test_0_marshal_small_struct_delegate5 () {
563 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
566 /* TEST 6: 4+4 byte long INTEGER struct */
568 [StructLayout (LayoutKind.Sequential)]
569 public struct SmallStruct6 {
573 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
582 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
584 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
585 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
587 public static int test_0_marshal_small_struct_delegate6 () {
588 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
591 /* TEST 7: 4+2 byte long INTEGER struct */
593 [StructLayout (LayoutKind.Sequential)]
594 public struct SmallStruct7 {
599 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
603 res.j = (short)-ss.j;
608 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
610 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
611 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
613 public static int test_0_marshal_small_struct_delegate7 () {
614 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
617 /* TEST 8: 4 byte long FLOAT struct */
619 [StructLayout (LayoutKind.Sequential)]
620 public struct SmallStruct8 {
624 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
632 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
634 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
635 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
637 public static int test_0_marshal_small_struct_delegate8 () {
638 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
641 /* TEST 9: 8 byte long FLOAT struct */
643 [StructLayout (LayoutKind.Sequential)]
644 public struct SmallStruct9 {
648 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
656 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
658 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
659 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
661 public static int test_0_marshal_small_struct_delegate9 () {
662 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
665 /* TEST 10: 4+4 byte long FLOAT struct */
667 [StructLayout (LayoutKind.Sequential)]
668 public struct SmallStruct10 {
673 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
682 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
684 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
685 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
687 public static int test_0_marshal_small_struct_delegate10 () {
688 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
691 /* TEST 11: 4+4 byte long MIXED struct */
693 [StructLayout (LayoutKind.Sequential)]
694 public struct SmallStruct11 {
699 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
708 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
710 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
711 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
713 public static int test_0_marshal_small_struct_delegate11 () {
714 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
720 public delegate int ArrayDelegate1 (int i,
722 [In, MarshalAs(UnmanagedType.LPArray,
723 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
725 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
726 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
728 public static int array_delegate1 (int i, string j, string[] arr) {
731 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
736 public static int test_0_marshal_array_delegate_string () {
737 string[] arr = new string [] { "ABC", "DEF" };
738 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
741 public static int array_delegate2 (int i, string j, string[] arr) {
742 return (arr == null) ? 0 : 1;
745 public static int test_0_marshal_array_delegate_null () {
746 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
749 public delegate int ArrayDelegateBlittable (int i, string j,
750 [In, MarshalAs(UnmanagedType.LPArray,
751 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] int[] arr);
753 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
754 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegateBlittable d);
756 public static int array_delegate_null_blittable (int i, string j, int[] arr) {
757 return (arr == null) ? 0 : 1;
760 public static int test_0_marshal_array_delegate_null_blittable () {
761 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegateBlittable (array_delegate_null_blittable));
764 public delegate int ArrayDelegate3 (int i,
766 [In, MarshalAs(UnmanagedType.LPArray,
767 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
769 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
770 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
772 public static int array_delegate3 (int i, string j, string[] arr) {
773 return (arr == null) ? 0 : 1;
776 public static int test_0_marshal_array_delegate_bad_paramindex () {
778 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
781 catch (MarshalDirectiveException) {
786 public delegate int ArrayDelegate4 (int i,
788 [In, MarshalAs(UnmanagedType.LPArray,
789 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
791 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
792 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
794 public static int array_delegate4 (int i, string j, string[] arr) {
795 return (arr == null) ? 0 : 1;
798 public static int test_0_marshal_array_delegate_bad_paramtype () {
800 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
803 catch (MarshalDirectiveException) {
808 public delegate int ArrayDelegate4_2 (int i,
812 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
813 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
815 public static int array_delegate4_2 (int i, string j, string[] arr) {
816 return (arr == null) ? 0 : 1;
819 public static int test_0_marshal_array_delegate_no_marshal_directive () {
821 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
824 catch (MarshalDirectiveException) {
829 public delegate int ArrayDelegate4_3 (int i,
833 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
834 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
836 public int array_delegate4_3 (int i, string j, string[] arr) {
837 return (arr == null) ? 0 : 1;
840 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
842 Tests t = new Tests ();
843 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
846 catch (MarshalDirectiveException) {
851 public delegate int ArrayDelegate5 (int i,
853 [In, MarshalAs(UnmanagedType.LPArray,
854 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
856 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
857 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
859 public static int array_delegate5 (int i, string j, string[] arr) {
862 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
867 public static int test_0_marshal_array_delegate_unicode_string () {
868 string[] arr = new string [] { "ABC", "DEF" };
869 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
872 public delegate int ArrayDelegate6 (int i,
874 [In, MarshalAs(UnmanagedType.LPArray,
875 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
877 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
878 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
880 public static int array_delegate6 (int i, string j, string[] arr) {
883 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
888 public static int test_0_marshal_array_delegate_sizeconst () {
889 string[] arr = new string [] { "ABC", "DEF" };
890 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
893 public delegate int ArrayDelegate7 (int i,
895 [In, MarshalAs(UnmanagedType.LPArray,
896 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
898 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
899 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
901 public static int array_delegate7 (int i, string j, string[] arr) {
904 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
909 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
910 string[] arr = new string [] { "ABC", "DEF" };
911 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
914 public delegate int ArrayDelegate8 (int i, string j,
915 [In, MarshalAs(UnmanagedType.LPArray,
919 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
920 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
922 public static int array_delegate8 (int i, string j, int[] arr) {
925 if ((arr [0] != 42) || (arr [1] != 43))
930 public static int test_0_marshal_array_delegate_blittable () {
931 int[] arr = new int [] { 42, 43 };
932 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
935 /* Array with size param of type long */
937 public delegate int ArrayDelegate8_2 (long i,
939 [In, MarshalAs(UnmanagedType.LPArray,
940 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
942 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
943 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
945 public static int array_delegate8_2 (long i, string j, string[] arr) {
948 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
953 public static int test_0_marshal_array_delegate_long_param () {
954 string[] arr = new string [] { "ABC", "DEF" };
955 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
960 * [Out] blittable arrays
963 public delegate int ArrayDelegate9 (int i, string j,
964 [Out, MarshalAs(UnmanagedType.LPArray,
968 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
969 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
971 public static int array_delegate9 (int i, string j, int[] arr) {
981 public static int test_0_marshal_out_array_delegate () {
982 int[] arr = new int [] { 42, 43 };
983 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
987 * [Out] string arrays
990 public delegate int ArrayDelegate10 (int i,
992 [Out, MarshalAs(UnmanagedType.LPArray,
993 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
995 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
996 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
998 public static int array_delegate10 (int i, string j, string[] arr) {
1008 public static int test_0_marshal_out_string_array_delegate () {
1009 string[] arr = new string [] { "", "" };
1010 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
1017 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
1019 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
1020 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
1022 public static int delegate_test_byval_class_inout (SimpleClass ss) {
1023 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
1034 public static int test_0_marshal_inout_byval_class_delegate () {
1035 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
1039 * Returning unicode strings
1041 [return: MarshalAs(UnmanagedType.LPWStr)]
1042 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
1044 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
1045 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
1047 public static String return_unicode_string_delegate (string message) {
1051 public static int test_0_marshal_return_unicode_string_delegate () {
1052 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
1056 * Returning string arrays
1058 public delegate string[] ReturnArrayDelegate (int i);
1060 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
1061 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
1063 public static String[] return_array_delegate (int i) {
1064 String[] arr = new String [2];
1072 public static String[] return_array_delegate_null (int i) {
1076 public static int test_0_marshal_return_string_array_delegate () {
1077 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1080 public static int test_3_marshal_return_string_array_delegate_null () {
1081 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1085 * Byref string marshalling
1087 public delegate int ByrefStringDelegate (ref string s);
1089 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1090 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1092 public static int byref_string_delegate (ref string s) {
1101 public static int test_0_marshal_byref_string_delegate () {
1102 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1109 public delegate int SimpleDelegate (int i);
1111 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1112 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1114 public static int test_43_thread_attach () {
1115 int res = mono_test_marshal_thread_attach (delegate (int i) {
1116 if (!Thread.CurrentThread.IsBackground)
1124 volatile bool stop = false;
1125 public void Stop () {
1129 public void Work () {
1131 for (int i = 0; i < 100; i++) {
1132 var a = new double[80000];
1140 public static int test_43_thread_attach_detach_contested () {
1141 // Test plan: we want to create a race between the GC
1142 // and native threads detaching. When a native thread
1143 // calls a managed delegate, it's attached to the
1144 // runtime by the wrapper. It is detached when the
1145 // thread is destroyed and the TLS key destructor for
1146 // MonoThreadInfo runs. That destructor wants to take
1147 // the GC lock. So we create a lot of native threads
1148 // while at the same time running a worker that
1149 // allocates garbage and invokes the collector.
1150 var w = new Worker ();
1151 Thread t = new Thread(new ThreadStart (w.Work));
1154 for (int count = 0; count < 500; count++) {
1155 int res = mono_test_marshal_thread_attach (delegate (int i) {
1160 Thread.Sleep (1000);
1167 * Appdomain save/restore
1169 static Func<int> callback;
1171 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1172 public static extern int mono_test_marshal_set_callback (Func<int> a);
1174 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1175 public static extern int mono_test_marshal_call_callback ();
1177 public static int test_0_appdomain_switch () {
1178 // FIXME: The appdomain unload hangs
1180 AppDomain ad = AppDomain.CreateDomain ("foo");
1181 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1182 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1184 int domain_id = AppDomain.CurrentDomain.Id;
1185 int new_id = mono_test_marshal_call_callback ();
1187 if (new_id == domain_id)
1189 if (AppDomain.CurrentDomain.Id != domain_id)
1191 AppDomain.Unload (ad);
1195 static int domain_callback () {
1196 return AppDomain.CurrentDomain.Id;
1199 class CallbackClass : MarshalByRefObject {
1200 public int SetCallback () {
1201 mono_test_marshal_set_callback (domain_callback);
1207 public VirtualDelegate get_del () {
1208 return delegate_test;
1211 public virtual int delegate_test (int i) {
1216 class Derived : Base {
1217 public override int delegate_test (int i) {
1222 public static int test_43_virtual () {
1223 Base b = new Derived ();
1225 return mono_test_marshal_virtual_delegate (b.get_del ());
1228 public static int test_0_icall_delegate () {
1229 var m = typeof (Marshal).GetMethod ("PtrToStringAnsi", new Type[] { typeof (IntPtr) });
1231 return mono_test_marshal_icall_delegate ((IcallDelegate)Delegate.CreateDelegate (typeof (IcallDelegate), m));