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 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
193 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
195 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
197 public delegate int SimpleDelegate5 (ref SimpleClass ss);
199 public delegate int SimpleDelegate7 (out SimpleClass ss);
201 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
203 public delegate int return_int_delegate (int i);
205 public delegate int SimpleDelegate9 (return_int_delegate del);
207 public delegate int SimpleDelegate11 (StringBuilder s1);
209 public delegate int PrimitiveByrefDelegate (ref int i);
211 public delegate return_int_delegate ReturnDelegateDelegate ();
213 public static int Main () {
214 return TestDriver.RunTests (typeof (Tests));
217 /* Test structures as arguments and return values of delegates */
218 public static int test_0_marshal_struct_delegate () {
219 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
221 return mono_test_marshal_delegate2 (d);
224 /* Test structures as byref arguments of delegates */
225 public static int test_0_marshal_byref_struct_delegate () {
226 SimpleStruct ss = new SimpleStruct ();
227 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
232 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
235 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
241 /* Test structures as out arguments of delegates */
242 public static int test_0_marshal_out_struct_delegate () {
243 SimpleStruct ss = new SimpleStruct ();
244 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
246 return mono_test_marshal_out_struct (1, out ss, 2, d);
249 /* Test structures as in arguments of delegates */
250 public static int test_0_marshal_in_struct_delegate () {
251 SimpleStruct ss = new SimpleStruct () { a = true, b = false, c = true, d = "TEST2" };
252 InStructDelegate d = new InStructDelegate (delegate_test_struct_in);
254 return mono_test_marshal_in_struct (1, ref ss, 2, d);
257 /* Test classes as arguments and return values of delegates */
258 public static int test_0_marshal_class_delegate () {
259 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
261 return mono_test_marshal_delegate4 (d);
264 /* Test classes as byref arguments of delegates */
265 public static int test_0_marshal_byref_class_delegate () {
266 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
268 return mono_test_marshal_delegate5 (d);
271 /* Test classes as out arguments of delegates */
272 public static int test_0_marshal_out_class_delegate () {
273 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
275 return mono_test_marshal_delegate7 (d);
278 /* Test string marshalling with delegates */
279 public static int test_0_marshal_string_delegate () {
280 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
282 return mono_test_marshal_delegate8 (d, "ABC");
285 /* Test string builder marshalling with delegates */
286 public static int test_0_marshal_string_builder_delegate () {
287 SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
289 if (mono_test_marshal_delegate11 (d, null) != 2)
292 return mono_test_marshal_delegate11 (d, "ABC");
295 /* Test that the delegate wrapper correctly catches null byref arguments */
296 public static int test_0_marshal_byref_class_delegate_null () {
297 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
300 mono_test_marshal_delegate6 (d);
303 catch (ArgumentNullException ex) {
308 static int return_self (int i) {
312 static int call_int_delegate (return_int_delegate d) {
316 public static int test_55_marshal_delegate_delegate () {
317 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
319 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
322 public static int test_0_marshal_primitive_byref_delegate () {
323 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
325 return mono_test_marshal_primitive_byref_delegate (d);
328 public static return_int_delegate return_delegate () {
329 return new return_int_delegate (return_self);
332 public static int test_55_marshal_return_delegate_delegate () {
333 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
336 /* Passing and returning strings */
338 public delegate String ReturnStringDelegate (String s);
340 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
341 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
343 public static String managed_return_string (String s) {
350 public static int test_0_marshal_return_string_delegate () {
351 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
352 String s = mono_test_marshal_return_string_delegate (d);
354 return (s == "12345") ? 0 : 1;
357 /* Passing and returning enums */
359 public enum FooEnum {
365 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
367 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
368 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
370 public static FooEnum managed_return_enum (FooEnum e) {
371 return (FooEnum)((int)e + 1);
374 public static int test_0_marshal_return_enum_delegate () {
375 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
376 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
378 return e == FooEnum.Foo3 ? 0 : 1;
381 /* Passing and returning blittable structs */
383 [StructLayout (LayoutKind.Sequential)]
384 public struct BlittableStruct {
389 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
401 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
403 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
404 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
406 public static int test_0_marshal_blittable_struct_delegate () {
407 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
411 * Passing and returning small structs
414 /* TEST 1: 4 byte long INTEGER struct */
416 [StructLayout (LayoutKind.Sequential)]
417 public struct SmallStruct1 {
421 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
429 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
431 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
432 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
434 public static int test_0_marshal_small_struct_delegate1 () {
435 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
438 /* TEST 2: 2+2 byte long INTEGER struct */
440 [StructLayout (LayoutKind.Sequential)]
441 public struct SmallStruct2 {
445 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
448 res.i = (short)-ss.i;
449 res.j = (short)-ss.j;
454 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
456 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
457 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
459 public static int test_0_marshal_small_struct_delegate2 () {
460 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
463 /* TEST 3: 2+1 byte long INTEGER struct */
465 [StructLayout (LayoutKind.Sequential)]
466 public struct SmallStruct3 {
471 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
474 res.i = (short)-ss.i;
480 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
482 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
483 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
485 public static int test_0_marshal_small_struct_delegate3 () {
486 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
489 /* TEST 4: 2 byte long INTEGER struct */
491 [StructLayout (LayoutKind.Sequential)]
492 public struct SmallStruct4 {
496 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
499 res.i = (short)-ss.i;
504 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
506 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
507 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
509 public static int test_0_marshal_small_struct_delegate4 () {
510 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
513 /* TEST 5: 8 byte long INTEGER struct */
515 [StructLayout (LayoutKind.Sequential)]
516 public struct SmallStruct5 {
520 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
528 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
530 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
531 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
533 public static int test_0_marshal_small_struct_delegate5 () {
534 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
537 /* TEST 6: 4+4 byte long INTEGER struct */
539 [StructLayout (LayoutKind.Sequential)]
540 public struct SmallStruct6 {
544 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
553 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
555 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
556 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
558 public static int test_0_marshal_small_struct_delegate6 () {
559 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
562 /* TEST 7: 4+2 byte long INTEGER struct */
564 [StructLayout (LayoutKind.Sequential)]
565 public struct SmallStruct7 {
570 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
574 res.j = (short)-ss.j;
579 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
581 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
582 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
584 public static int test_0_marshal_small_struct_delegate7 () {
585 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
588 /* TEST 8: 4 byte long FLOAT struct */
590 [StructLayout (LayoutKind.Sequential)]
591 public struct SmallStruct8 {
595 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
603 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
605 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
606 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
608 public static int test_0_marshal_small_struct_delegate8 () {
609 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
612 /* TEST 9: 8 byte long FLOAT struct */
614 [StructLayout (LayoutKind.Sequential)]
615 public struct SmallStruct9 {
619 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
627 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
629 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
630 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
632 public static int test_0_marshal_small_struct_delegate9 () {
633 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
636 /* TEST 10: 4+4 byte long FLOAT struct */
638 [StructLayout (LayoutKind.Sequential)]
639 public struct SmallStruct10 {
644 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
653 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
655 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
656 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
658 public static int test_0_marshal_small_struct_delegate10 () {
659 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
662 /* TEST 11: 4+4 byte long MIXED struct */
664 [StructLayout (LayoutKind.Sequential)]
665 public struct SmallStruct11 {
670 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
679 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
681 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
682 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
684 public static int test_0_marshal_small_struct_delegate11 () {
685 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
691 public delegate int ArrayDelegate1 (int i,
693 [In, MarshalAs(UnmanagedType.LPArray,
694 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
696 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
697 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
699 public static int array_delegate1 (int i, string j, string[] arr) {
702 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
707 public static int test_0_marshal_array_delegate_string () {
708 string[] arr = new string [] { "ABC", "DEF" };
709 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
712 public static int array_delegate2 (int i, string j, string[] arr) {
713 return (arr == null) ? 0 : 1;
716 public static int test_0_marshal_array_delegate_null () {
717 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
720 public delegate int ArrayDelegate3 (int i,
722 [In, MarshalAs(UnmanagedType.LPArray,
723 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
725 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
726 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
728 public static int array_delegate3 (int i, string j, string[] arr) {
729 return (arr == null) ? 0 : 1;
732 public static int test_0_marshal_array_delegate_bad_paramindex () {
734 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
737 catch (MarshalDirectiveException) {
742 public delegate int ArrayDelegate4 (int i,
744 [In, MarshalAs(UnmanagedType.LPArray,
745 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
747 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
748 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
750 public static int array_delegate4 (int i, string j, string[] arr) {
751 return (arr == null) ? 0 : 1;
754 public static int test_0_marshal_array_delegate_bad_paramtype () {
756 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
759 catch (MarshalDirectiveException) {
764 public delegate int ArrayDelegate4_2 (int i,
768 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
769 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
771 public static int array_delegate4_2 (int i, string j, string[] arr) {
772 return (arr == null) ? 0 : 1;
775 public static int test_0_marshal_array_delegate_no_marshal_directive () {
777 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
780 catch (MarshalDirectiveException) {
785 public delegate int ArrayDelegate4_3 (int i,
789 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
790 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
792 public int array_delegate4_3 (int i, string j, string[] arr) {
793 return (arr == null) ? 0 : 1;
796 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
798 Tests t = new Tests ();
799 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
802 catch (MarshalDirectiveException) {
807 public delegate int ArrayDelegate5 (int i,
809 [In, MarshalAs(UnmanagedType.LPArray,
810 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
812 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
813 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
815 public static int array_delegate5 (int i, string j, string[] arr) {
818 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
823 public static int test_0_marshal_array_delegate_unicode_string () {
824 string[] arr = new string [] { "ABC", "DEF" };
825 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
828 public delegate int ArrayDelegate6 (int i,
830 [In, MarshalAs(UnmanagedType.LPArray,
831 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
833 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
834 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
836 public static int array_delegate6 (int i, string j, string[] arr) {
839 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
844 public static int test_0_marshal_array_delegate_sizeconst () {
845 string[] arr = new string [] { "ABC", "DEF" };
846 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
849 public delegate int ArrayDelegate7 (int i,
851 [In, MarshalAs(UnmanagedType.LPArray,
852 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
854 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
855 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
857 public static int array_delegate7 (int i, string j, string[] arr) {
860 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
865 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
866 string[] arr = new string [] { "ABC", "DEF" };
867 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
870 public delegate int ArrayDelegate8 (int i, string j,
871 [In, MarshalAs(UnmanagedType.LPArray,
875 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
876 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
878 public static int array_delegate8 (int i, string j, int[] arr) {
881 if ((arr [0] != 42) || (arr [1] != 43))
886 public static int test_0_marshal_array_delegate_blittable () {
887 int[] arr = new int [] { 42, 43 };
888 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
891 /* Array with size param of type long */
893 public delegate int ArrayDelegate8_2 (long i,
895 [In, MarshalAs(UnmanagedType.LPArray,
896 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
898 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
899 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
901 public static int array_delegate8_2 (long i, string j, string[] arr) {
904 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
909 public static int test_0_marshal_array_delegate_long_param () {
910 string[] arr = new string [] { "ABC", "DEF" };
911 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
916 * [Out] blittable arrays
919 public delegate int ArrayDelegate9 (int i, string j,
920 [Out, MarshalAs(UnmanagedType.LPArray,
924 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
925 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
927 public static int array_delegate9 (int i, string j, int[] arr) {
937 public static int test_0_marshal_out_array_delegate () {
938 int[] arr = new int [] { 42, 43 };
939 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
943 * [Out] string arrays
946 public delegate int ArrayDelegate10 (int i,
948 [Out, MarshalAs(UnmanagedType.LPArray,
949 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
951 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
952 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
954 public static int array_delegate10 (int i, string j, string[] arr) {
964 public static int test_0_marshal_out_string_array_delegate () {
965 string[] arr = new string [] { "", "" };
966 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
973 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
975 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
976 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
978 public static int delegate_test_byval_class_inout (SimpleClass ss) {
979 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
990 public static int test_0_marshal_inout_byval_class_delegate () {
991 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
995 * Returning unicode strings
997 [return: MarshalAs(UnmanagedType.LPWStr)]
998 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
1000 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
1001 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
1003 public static String return_unicode_string_delegate (string message) {
1007 public static int test_0_marshal_return_unicode_string_delegate () {
1008 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
1012 * Returning string arrays
1014 public delegate string[] ReturnArrayDelegate (int i);
1016 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
1017 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
1019 public static String[] return_array_delegate (int i) {
1020 String[] arr = new String [2];
1028 public static String[] return_array_delegate_null (int i) {
1032 public static int test_0_marshal_return_string_array_delegate () {
1033 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1036 public static int test_3_marshal_return_string_array_delegate_null () {
1037 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1041 * Byref string marshalling
1043 public delegate int ByrefStringDelegate (ref string s);
1045 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1046 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1048 public static int byref_string_delegate (ref string s) {
1057 public static int test_0_marshal_byref_string_delegate () {
1058 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1065 public delegate int SimpleDelegate (int i);
1067 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1068 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1070 public static int test_43_thread_attach () {
1071 int res = mono_test_marshal_thread_attach (delegate (int i) {
1072 if (!Thread.CurrentThread.IsBackground)
1080 * Appdomain save/restore
1082 static Func<int> callback;
1084 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1085 public static extern int mono_test_marshal_set_callback (Func<int> a);
1087 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1088 public static extern int mono_test_marshal_call_callback ();
1090 public static int test_0_appdomain_switch () {
1091 // FIXME: The appdomain unload hangs
1093 AppDomain ad = AppDomain.CreateDomain ("foo");
1094 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1095 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1097 int domain_id = AppDomain.CurrentDomain.Id;
1098 int new_id = mono_test_marshal_call_callback ();
1100 if (new_id == domain_id)
1102 if (AppDomain.CurrentDomain.Id != domain_id)
1104 AppDomain.Unload (ad);
1108 static int domain_callback () {
1109 return AppDomain.CurrentDomain.Id;
1112 class CallbackClass : MarshalByRefObject {
1113 public int SetCallback () {
1114 mono_test_marshal_set_callback (domain_callback);