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 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
199 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
201 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
203 public delegate int SimpleDelegate5 (ref SimpleClass ss);
205 public delegate int SimpleDelegate7 (out SimpleClass ss);
207 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
209 public delegate int return_int_delegate (int i);
211 public delegate int SimpleDelegate9 (return_int_delegate del);
213 public delegate int SimpleDelegate11 (StringBuilder s1);
215 public delegate int PrimitiveByrefDelegate (ref int i);
217 public delegate return_int_delegate ReturnDelegateDelegate ();
219 public delegate int DelegateByrefDelegate (ref return_int_delegate del);
221 public delegate int VirtualDelegate (int i);
223 public static int Main () {
224 return TestDriver.RunTests (typeof (Tests));
227 /* Test structures as arguments and return values of delegates */
228 public static int test_0_marshal_struct_delegate () {
229 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
231 return mono_test_marshal_delegate2 (d);
234 /* Test structures as byref arguments of delegates */
235 public static int test_0_marshal_byref_struct_delegate () {
236 SimpleStruct ss = new SimpleStruct ();
237 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
242 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
245 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
251 /* Test structures as out arguments of delegates */
252 public static int test_0_marshal_out_struct_delegate () {
253 SimpleStruct ss = new SimpleStruct ();
254 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
256 return mono_test_marshal_out_struct (1, out ss, 2, d);
259 /* Test structures as in arguments of delegates */
260 public static int test_0_marshal_in_struct_delegate () {
261 SimpleStruct ss = new SimpleStruct () { a = true, b = false, c = true, d = "TEST2" };
262 InStructDelegate d = new InStructDelegate (delegate_test_struct_in);
264 return mono_test_marshal_in_struct (1, ref ss, 2, d);
267 /* Test classes as arguments and return values of delegates */
268 public static int test_0_marshal_class_delegate () {
269 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
271 return mono_test_marshal_delegate4 (d);
274 /* Test classes as byref arguments of delegates */
275 public static int test_0_marshal_byref_class_delegate () {
276 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
278 return mono_test_marshal_delegate5 (d);
281 /* Test classes as out arguments of delegates */
282 public static int test_0_marshal_out_class_delegate () {
283 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
285 return mono_test_marshal_delegate7 (d);
288 /* Test string marshalling with delegates */
289 public static int test_0_marshal_string_delegate () {
290 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
292 return mono_test_marshal_delegate8 (d, "ABC");
295 /* Test string builder marshalling with delegates */
296 public static int test_0_marshal_string_builder_delegate () {
297 SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
299 if (mono_test_marshal_delegate11 (d, null) != 2)
302 return mono_test_marshal_delegate11 (d, "ABC");
305 /* Test that the delegate wrapper correctly catches null byref arguments */
306 public static int test_0_marshal_byref_class_delegate_null () {
307 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
310 mono_test_marshal_delegate6 (d);
313 catch (ArgumentNullException ex) {
318 static int return_self (int i) {
322 static int call_int_delegate (return_int_delegate d) {
326 public static int test_55_marshal_delegate_delegate () {
327 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
329 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
332 public static int test_0_marshal_primitive_byref_delegate () {
333 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
335 return mono_test_marshal_primitive_byref_delegate (d);
338 public static return_int_delegate return_delegate () {
339 return new return_int_delegate (return_self);
342 public static int test_55_marshal_return_delegate_delegate () {
343 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
346 public static int return_plus_1 (int i) {
350 public static int ref_delegate_delegate (ref return_int_delegate del) {
355 public static int test_55_marshal_delegate_ref_delegate () {
356 var del = new DelegateByrefDelegate (ref_delegate_delegate);
357 return mono_test_marshal_delegate_ref_delegate (del);
360 /* Passing and returning strings */
362 public delegate String ReturnStringDelegate (String s);
364 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
365 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
367 public static String managed_return_string (String s) {
374 public static int test_0_marshal_return_string_delegate () {
375 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
376 String s = mono_test_marshal_return_string_delegate (d);
378 return (s == "12345") ? 0 : 1;
381 /* Passing and returning enums */
383 public enum FooEnum {
389 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
391 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
392 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
394 public static FooEnum managed_return_enum (FooEnum e) {
395 return (FooEnum)((int)e + 1);
398 public static int test_0_marshal_return_enum_delegate () {
399 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
400 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
402 return e == FooEnum.Foo3 ? 0 : 1;
405 /* Passing and returning blittable structs */
407 [StructLayout (LayoutKind.Sequential)]
408 public struct BlittableStruct {
413 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
425 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
427 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
428 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
430 public static int test_0_marshal_blittable_struct_delegate () {
431 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
435 * Passing and returning small structs
438 /* TEST 1: 4 byte long INTEGER struct */
440 [StructLayout (LayoutKind.Sequential)]
441 public struct SmallStruct1 {
445 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
453 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
455 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
456 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
458 public static int test_0_marshal_small_struct_delegate1 () {
459 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
462 /* TEST 2: 2+2 byte long INTEGER struct */
464 [StructLayout (LayoutKind.Sequential)]
465 public struct SmallStruct2 {
469 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
472 res.i = (short)-ss.i;
473 res.j = (short)-ss.j;
478 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
480 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
481 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
483 public static int test_0_marshal_small_struct_delegate2 () {
484 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
487 /* TEST 3: 2+1 byte long INTEGER struct */
489 [StructLayout (LayoutKind.Sequential)]
490 public struct SmallStruct3 {
495 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
498 res.i = (short)-ss.i;
504 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
506 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
507 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
509 public static int test_0_marshal_small_struct_delegate3 () {
510 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
513 /* TEST 4: 2 byte long INTEGER struct */
515 [StructLayout (LayoutKind.Sequential)]
516 public struct SmallStruct4 {
520 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
523 res.i = (short)-ss.i;
528 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
530 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
531 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
533 public static int test_0_marshal_small_struct_delegate4 () {
534 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
537 /* TEST 5: 8 byte long INTEGER struct */
539 [StructLayout (LayoutKind.Sequential)]
540 public struct SmallStruct5 {
544 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
552 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
554 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
555 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
557 public static int test_0_marshal_small_struct_delegate5 () {
558 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
561 /* TEST 6: 4+4 byte long INTEGER struct */
563 [StructLayout (LayoutKind.Sequential)]
564 public struct SmallStruct6 {
568 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
577 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
579 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
580 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
582 public static int test_0_marshal_small_struct_delegate6 () {
583 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
586 /* TEST 7: 4+2 byte long INTEGER struct */
588 [StructLayout (LayoutKind.Sequential)]
589 public struct SmallStruct7 {
594 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
598 res.j = (short)-ss.j;
603 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
605 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
606 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
608 public static int test_0_marshal_small_struct_delegate7 () {
609 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
612 /* TEST 8: 4 byte long FLOAT struct */
614 [StructLayout (LayoutKind.Sequential)]
615 public struct SmallStruct8 {
619 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
627 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
629 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
630 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
632 public static int test_0_marshal_small_struct_delegate8 () {
633 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
636 /* TEST 9: 8 byte long FLOAT struct */
638 [StructLayout (LayoutKind.Sequential)]
639 public struct SmallStruct9 {
643 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
651 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
653 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
654 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
656 public static int test_0_marshal_small_struct_delegate9 () {
657 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
660 /* TEST 10: 4+4 byte long FLOAT struct */
662 [StructLayout (LayoutKind.Sequential)]
663 public struct SmallStruct10 {
668 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
677 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
679 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
680 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
682 public static int test_0_marshal_small_struct_delegate10 () {
683 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
686 /* TEST 11: 4+4 byte long MIXED struct */
688 [StructLayout (LayoutKind.Sequential)]
689 public struct SmallStruct11 {
694 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
703 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
705 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
706 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
708 public static int test_0_marshal_small_struct_delegate11 () {
709 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
715 public delegate int ArrayDelegate1 (int i,
717 [In, MarshalAs(UnmanagedType.LPArray,
718 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
720 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
721 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
723 public static int array_delegate1 (int i, string j, string[] arr) {
726 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
731 public static int test_0_marshal_array_delegate_string () {
732 string[] arr = new string [] { "ABC", "DEF" };
733 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
736 public static int array_delegate2 (int i, string j, string[] arr) {
737 return (arr == null) ? 0 : 1;
740 public static int test_0_marshal_array_delegate_null () {
741 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
744 public delegate int ArrayDelegate3 (int i,
746 [In, MarshalAs(UnmanagedType.LPArray,
747 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
749 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
750 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
752 public static int array_delegate3 (int i, string j, string[] arr) {
753 return (arr == null) ? 0 : 1;
756 public static int test_0_marshal_array_delegate_bad_paramindex () {
758 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
761 catch (MarshalDirectiveException) {
766 public delegate int ArrayDelegate4 (int i,
768 [In, MarshalAs(UnmanagedType.LPArray,
769 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
771 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
772 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
774 public static int array_delegate4 (int i, string j, string[] arr) {
775 return (arr == null) ? 0 : 1;
778 public static int test_0_marshal_array_delegate_bad_paramtype () {
780 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
783 catch (MarshalDirectiveException) {
788 public delegate int ArrayDelegate4_2 (int i,
792 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
793 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
795 public static int array_delegate4_2 (int i, string j, string[] arr) {
796 return (arr == null) ? 0 : 1;
799 public static int test_0_marshal_array_delegate_no_marshal_directive () {
801 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
804 catch (MarshalDirectiveException) {
809 public delegate int ArrayDelegate4_3 (int i,
813 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
814 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
816 public int array_delegate4_3 (int i, string j, string[] arr) {
817 return (arr == null) ? 0 : 1;
820 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
822 Tests t = new Tests ();
823 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
826 catch (MarshalDirectiveException) {
831 public delegate int ArrayDelegate5 (int i,
833 [In, MarshalAs(UnmanagedType.LPArray,
834 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
836 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
837 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
839 public static int array_delegate5 (int i, string j, string[] arr) {
842 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
847 public static int test_0_marshal_array_delegate_unicode_string () {
848 string[] arr = new string [] { "ABC", "DEF" };
849 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
852 public delegate int ArrayDelegate6 (int i,
854 [In, MarshalAs(UnmanagedType.LPArray,
855 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
857 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
858 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
860 public static int array_delegate6 (int i, string j, string[] arr) {
863 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
868 public static int test_0_marshal_array_delegate_sizeconst () {
869 string[] arr = new string [] { "ABC", "DEF" };
870 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
873 public delegate int ArrayDelegate7 (int i,
875 [In, MarshalAs(UnmanagedType.LPArray,
876 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
878 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
879 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
881 public static int array_delegate7 (int i, string j, string[] arr) {
884 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
889 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
890 string[] arr = new string [] { "ABC", "DEF" };
891 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
894 public delegate int ArrayDelegate8 (int i, string j,
895 [In, MarshalAs(UnmanagedType.LPArray,
899 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
900 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
902 public static int array_delegate8 (int i, string j, int[] arr) {
905 if ((arr [0] != 42) || (arr [1] != 43))
910 public static int test_0_marshal_array_delegate_blittable () {
911 int[] arr = new int [] { 42, 43 };
912 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
915 /* Array with size param of type long */
917 public delegate int ArrayDelegate8_2 (long i,
919 [In, MarshalAs(UnmanagedType.LPArray,
920 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
922 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
923 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
925 public static int array_delegate8_2 (long i, string j, string[] arr) {
928 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
933 public static int test_0_marshal_array_delegate_long_param () {
934 string[] arr = new string [] { "ABC", "DEF" };
935 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
940 * [Out] blittable arrays
943 public delegate int ArrayDelegate9 (int i, string j,
944 [Out, MarshalAs(UnmanagedType.LPArray,
948 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
949 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
951 public static int array_delegate9 (int i, string j, int[] arr) {
961 public static int test_0_marshal_out_array_delegate () {
962 int[] arr = new int [] { 42, 43 };
963 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
967 * [Out] string arrays
970 public delegate int ArrayDelegate10 (int i,
972 [Out, MarshalAs(UnmanagedType.LPArray,
973 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
975 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
976 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
978 public static int array_delegate10 (int i, string j, string[] arr) {
988 public static int test_0_marshal_out_string_array_delegate () {
989 string[] arr = new string [] { "", "" };
990 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
997 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
999 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
1000 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
1002 public static int delegate_test_byval_class_inout (SimpleClass ss) {
1003 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
1014 public static int test_0_marshal_inout_byval_class_delegate () {
1015 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
1019 * Returning unicode strings
1021 [return: MarshalAs(UnmanagedType.LPWStr)]
1022 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
1024 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
1025 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
1027 public static String return_unicode_string_delegate (string message) {
1031 public static int test_0_marshal_return_unicode_string_delegate () {
1032 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
1036 * Returning string arrays
1038 public delegate string[] ReturnArrayDelegate (int i);
1040 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
1041 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
1043 public static String[] return_array_delegate (int i) {
1044 String[] arr = new String [2];
1052 public static String[] return_array_delegate_null (int i) {
1056 public static int test_0_marshal_return_string_array_delegate () {
1057 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1060 public static int test_3_marshal_return_string_array_delegate_null () {
1061 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1065 * Byref string marshalling
1067 public delegate int ByrefStringDelegate (ref string s);
1069 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1070 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1072 public static int byref_string_delegate (ref string s) {
1081 public static int test_0_marshal_byref_string_delegate () {
1082 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1089 public delegate int SimpleDelegate (int i);
1091 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1092 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1094 public static int test_43_thread_attach () {
1095 int res = mono_test_marshal_thread_attach (delegate (int i) {
1096 if (!Thread.CurrentThread.IsBackground)
1104 * Appdomain save/restore
1106 static Func<int> callback;
1108 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1109 public static extern int mono_test_marshal_set_callback (Func<int> a);
1111 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1112 public static extern int mono_test_marshal_call_callback ();
1114 public static int test_0_appdomain_switch () {
1115 // FIXME: The appdomain unload hangs
1117 AppDomain ad = AppDomain.CreateDomain ("foo");
1118 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1119 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1121 int domain_id = AppDomain.CurrentDomain.Id;
1122 int new_id = mono_test_marshal_call_callback ();
1124 if (new_id == domain_id)
1126 if (AppDomain.CurrentDomain.Id != domain_id)
1128 AppDomain.Unload (ad);
1132 static int domain_callback () {
1133 return AppDomain.CurrentDomain.Id;
1136 class CallbackClass : MarshalByRefObject {
1137 public int SetCallback () {
1138 mono_test_marshal_set_callback (domain_callback);
1144 public VirtualDelegate get_del () {
1145 return delegate_test;
1148 public virtual int delegate_test (int i) {
1153 class Derived : Base {
1154 public override int delegate_test (int i) {
1159 public static int test_43_virtual () {
1160 Base b = new Derived ();
1162 return mono_test_marshal_virtual_delegate (b.get_del ());