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 ArrayDelegate3 (int i,
751 [In, MarshalAs(UnmanagedType.LPArray,
752 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
754 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
755 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
757 public static int array_delegate3 (int i, string j, string[] arr) {
758 return (arr == null) ? 0 : 1;
761 public static int test_0_marshal_array_delegate_bad_paramindex () {
763 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
766 catch (MarshalDirectiveException) {
771 public delegate int ArrayDelegate4 (int i,
773 [In, MarshalAs(UnmanagedType.LPArray,
774 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
776 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
777 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
779 public static int array_delegate4 (int i, string j, string[] arr) {
780 return (arr == null) ? 0 : 1;
783 public static int test_0_marshal_array_delegate_bad_paramtype () {
785 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
788 catch (MarshalDirectiveException) {
793 public delegate int ArrayDelegate4_2 (int i,
797 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
798 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
800 public static int array_delegate4_2 (int i, string j, string[] arr) {
801 return (arr == null) ? 0 : 1;
804 public static int test_0_marshal_array_delegate_no_marshal_directive () {
806 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
809 catch (MarshalDirectiveException) {
814 public delegate int ArrayDelegate4_3 (int i,
818 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
819 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
821 public int array_delegate4_3 (int i, string j, string[] arr) {
822 return (arr == null) ? 0 : 1;
825 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
827 Tests t = new Tests ();
828 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
831 catch (MarshalDirectiveException) {
836 public delegate int ArrayDelegate5 (int i,
838 [In, MarshalAs(UnmanagedType.LPArray,
839 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
841 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
842 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
844 public static int array_delegate5 (int i, string j, string[] arr) {
847 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
852 public static int test_0_marshal_array_delegate_unicode_string () {
853 string[] arr = new string [] { "ABC", "DEF" };
854 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
857 public delegate int ArrayDelegate6 (int i,
859 [In, MarshalAs(UnmanagedType.LPArray,
860 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
862 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
863 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
865 public static int array_delegate6 (int i, string j, string[] arr) {
868 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
873 public static int test_0_marshal_array_delegate_sizeconst () {
874 string[] arr = new string [] { "ABC", "DEF" };
875 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
878 public delegate int ArrayDelegate7 (int i,
880 [In, MarshalAs(UnmanagedType.LPArray,
881 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
883 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
884 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
886 public static int array_delegate7 (int i, string j, string[] arr) {
889 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
894 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
895 string[] arr = new string [] { "ABC", "DEF" };
896 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
899 public delegate int ArrayDelegate8 (int i, string j,
900 [In, MarshalAs(UnmanagedType.LPArray,
904 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
905 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
907 public static int array_delegate8 (int i, string j, int[] arr) {
910 if ((arr [0] != 42) || (arr [1] != 43))
915 public static int test_0_marshal_array_delegate_blittable () {
916 int[] arr = new int [] { 42, 43 };
917 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
920 /* Array with size param of type long */
922 public delegate int ArrayDelegate8_2 (long i,
924 [In, MarshalAs(UnmanagedType.LPArray,
925 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
927 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
928 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
930 public static int array_delegate8_2 (long i, string j, string[] arr) {
933 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
938 public static int test_0_marshal_array_delegate_long_param () {
939 string[] arr = new string [] { "ABC", "DEF" };
940 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
945 * [Out] blittable arrays
948 public delegate int ArrayDelegate9 (int i, string j,
949 [Out, MarshalAs(UnmanagedType.LPArray,
953 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
954 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
956 public static int array_delegate9 (int i, string j, int[] arr) {
966 public static int test_0_marshal_out_array_delegate () {
967 int[] arr = new int [] { 42, 43 };
968 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
972 * [Out] string arrays
975 public delegate int ArrayDelegate10 (int i,
977 [Out, MarshalAs(UnmanagedType.LPArray,
978 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
980 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
981 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
983 public static int array_delegate10 (int i, string j, string[] arr) {
993 public static int test_0_marshal_out_string_array_delegate () {
994 string[] arr = new string [] { "", "" };
995 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
1002 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
1004 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
1005 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
1007 public static int delegate_test_byval_class_inout (SimpleClass ss) {
1008 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
1019 public static int test_0_marshal_inout_byval_class_delegate () {
1020 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
1024 * Returning unicode strings
1026 [return: MarshalAs(UnmanagedType.LPWStr)]
1027 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
1029 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
1030 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
1032 public static String return_unicode_string_delegate (string message) {
1036 public static int test_0_marshal_return_unicode_string_delegate () {
1037 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
1041 * Returning string arrays
1043 public delegate string[] ReturnArrayDelegate (int i);
1045 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
1046 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
1048 public static String[] return_array_delegate (int i) {
1049 String[] arr = new String [2];
1057 public static String[] return_array_delegate_null (int i) {
1061 public static int test_0_marshal_return_string_array_delegate () {
1062 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1065 public static int test_3_marshal_return_string_array_delegate_null () {
1066 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1070 * Byref string marshalling
1072 public delegate int ByrefStringDelegate (ref string s);
1074 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1075 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1077 public static int byref_string_delegate (ref string s) {
1086 public static int test_0_marshal_byref_string_delegate () {
1087 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1094 public delegate int SimpleDelegate (int i);
1096 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1097 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1099 public static int test_43_thread_attach () {
1100 int res = mono_test_marshal_thread_attach (delegate (int i) {
1101 if (!Thread.CurrentThread.IsBackground)
1109 * Appdomain save/restore
1111 static Func<int> callback;
1113 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1114 public static extern int mono_test_marshal_set_callback (Func<int> a);
1116 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1117 public static extern int mono_test_marshal_call_callback ();
1119 public static int test_0_appdomain_switch () {
1120 // FIXME: The appdomain unload hangs
1122 AppDomain ad = AppDomain.CreateDomain ("foo");
1123 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1124 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1126 int domain_id = AppDomain.CurrentDomain.Id;
1127 int new_id = mono_test_marshal_call_callback ();
1129 if (new_id == domain_id)
1131 if (AppDomain.CurrentDomain.Id != domain_id)
1133 AppDomain.Unload (ad);
1137 static int domain_callback () {
1138 return AppDomain.CurrentDomain.Id;
1141 class CallbackClass : MarshalByRefObject {
1142 public int SetCallback () {
1143 mono_test_marshal_set_callback (domain_callback);
1149 public VirtualDelegate get_del () {
1150 return delegate_test;
1153 public virtual int delegate_test (int i) {
1158 class Derived : Base {
1159 public override int delegate_test (int i) {
1164 public static int test_43_virtual () {
1165 Base b = new Derived ();
1167 return mono_test_marshal_virtual_delegate (b.get_del ());
1170 public static int test_0_icall_delegate () {
1171 var m = typeof (Marshal).GetMethod ("PtrToStringAnsi", new Type[] { typeof (IntPtr) });
1173 return mono_test_marshal_icall_delegate ((IcallDelegate)Delegate.CreateDelegate (typeof (IcallDelegate), m));
1174 Console.WriteLine (m);