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 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
196 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
198 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
200 public delegate int SimpleDelegate5 (ref SimpleClass ss);
202 public delegate int SimpleDelegate7 (out SimpleClass ss);
204 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
206 public delegate int return_int_delegate (int i);
208 public delegate int SimpleDelegate9 (return_int_delegate del);
210 public delegate int SimpleDelegate11 (StringBuilder s1);
212 public delegate int PrimitiveByrefDelegate (ref int i);
214 public delegate return_int_delegate ReturnDelegateDelegate ();
216 public delegate int DelegateByrefDelegate (ref return_int_delegate del);
218 public static int Main () {
219 return TestDriver.RunTests (typeof (Tests));
222 /* Test structures as arguments and return values of delegates */
223 public static int test_0_marshal_struct_delegate () {
224 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
226 return mono_test_marshal_delegate2 (d);
229 /* Test structures as byref arguments of delegates */
230 public static int test_0_marshal_byref_struct_delegate () {
231 SimpleStruct ss = new SimpleStruct ();
232 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
237 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
240 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
246 /* Test structures as out arguments of delegates */
247 public static int test_0_marshal_out_struct_delegate () {
248 SimpleStruct ss = new SimpleStruct ();
249 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
251 return mono_test_marshal_out_struct (1, out ss, 2, d);
254 /* Test structures as in arguments of delegates */
255 public static int test_0_marshal_in_struct_delegate () {
256 SimpleStruct ss = new SimpleStruct () { a = true, b = false, c = true, d = "TEST2" };
257 InStructDelegate d = new InStructDelegate (delegate_test_struct_in);
259 return mono_test_marshal_in_struct (1, ref ss, 2, d);
262 /* Test classes as arguments and return values of delegates */
263 public static int test_0_marshal_class_delegate () {
264 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
266 return mono_test_marshal_delegate4 (d);
269 /* Test classes as byref arguments of delegates */
270 public static int test_0_marshal_byref_class_delegate () {
271 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
273 return mono_test_marshal_delegate5 (d);
276 /* Test classes as out arguments of delegates */
277 public static int test_0_marshal_out_class_delegate () {
278 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
280 return mono_test_marshal_delegate7 (d);
283 /* Test string marshalling with delegates */
284 public static int test_0_marshal_string_delegate () {
285 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
287 return mono_test_marshal_delegate8 (d, "ABC");
290 /* Test string builder marshalling with delegates */
291 public static int test_0_marshal_string_builder_delegate () {
292 SimpleDelegate11 d = new SimpleDelegate11 (delegate_test_string_builder_marshalling);
294 if (mono_test_marshal_delegate11 (d, null) != 2)
297 return mono_test_marshal_delegate11 (d, "ABC");
300 /* Test that the delegate wrapper correctly catches null byref arguments */
301 public static int test_0_marshal_byref_class_delegate_null () {
302 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
305 mono_test_marshal_delegate6 (d);
308 catch (ArgumentNullException ex) {
313 static int return_self (int i) {
317 static int call_int_delegate (return_int_delegate d) {
321 public static int test_55_marshal_delegate_delegate () {
322 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
324 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
327 public static int test_0_marshal_primitive_byref_delegate () {
328 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
330 return mono_test_marshal_primitive_byref_delegate (d);
333 public static return_int_delegate return_delegate () {
334 return new return_int_delegate (return_self);
337 public static int test_55_marshal_return_delegate_delegate () {
338 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
341 public static int return_plus_1 (int i) {
345 public static int ref_delegate_delegate (ref return_int_delegate del) {
350 public static int test_55_marshal_delegate_ref_delegate () {
351 var del = new DelegateByrefDelegate (ref_delegate_delegate);
352 return mono_test_marshal_delegate_ref_delegate (del);
355 /* Passing and returning strings */
357 public delegate String ReturnStringDelegate (String s);
359 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
360 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
362 public static String managed_return_string (String s) {
369 public static int test_0_marshal_return_string_delegate () {
370 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
371 String s = mono_test_marshal_return_string_delegate (d);
373 return (s == "12345") ? 0 : 1;
376 /* Passing and returning enums */
378 public enum FooEnum {
384 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
386 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
387 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
389 public static FooEnum managed_return_enum (FooEnum e) {
390 return (FooEnum)((int)e + 1);
393 public static int test_0_marshal_return_enum_delegate () {
394 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
395 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
397 return e == FooEnum.Foo3 ? 0 : 1;
400 /* Passing and returning blittable structs */
402 [StructLayout (LayoutKind.Sequential)]
403 public struct BlittableStruct {
408 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
420 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
422 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
423 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
425 public static int test_0_marshal_blittable_struct_delegate () {
426 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
430 * Passing and returning small structs
433 /* TEST 1: 4 byte long INTEGER struct */
435 [StructLayout (LayoutKind.Sequential)]
436 public struct SmallStruct1 {
440 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
448 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
450 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
451 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
453 public static int test_0_marshal_small_struct_delegate1 () {
454 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
457 /* TEST 2: 2+2 byte long INTEGER struct */
459 [StructLayout (LayoutKind.Sequential)]
460 public struct SmallStruct2 {
464 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
467 res.i = (short)-ss.i;
468 res.j = (short)-ss.j;
473 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
475 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
476 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
478 public static int test_0_marshal_small_struct_delegate2 () {
479 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
482 /* TEST 3: 2+1 byte long INTEGER struct */
484 [StructLayout (LayoutKind.Sequential)]
485 public struct SmallStruct3 {
490 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
493 res.i = (short)-ss.i;
499 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
501 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
502 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
504 public static int test_0_marshal_small_struct_delegate3 () {
505 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
508 /* TEST 4: 2 byte long INTEGER struct */
510 [StructLayout (LayoutKind.Sequential)]
511 public struct SmallStruct4 {
515 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
518 res.i = (short)-ss.i;
523 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
525 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
526 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
528 public static int test_0_marshal_small_struct_delegate4 () {
529 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
532 /* TEST 5: 8 byte long INTEGER struct */
534 [StructLayout (LayoutKind.Sequential)]
535 public struct SmallStruct5 {
539 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
547 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
549 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
550 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
552 public static int test_0_marshal_small_struct_delegate5 () {
553 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
556 /* TEST 6: 4+4 byte long INTEGER struct */
558 [StructLayout (LayoutKind.Sequential)]
559 public struct SmallStruct6 {
563 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
572 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
574 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
575 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
577 public static int test_0_marshal_small_struct_delegate6 () {
578 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
581 /* TEST 7: 4+2 byte long INTEGER struct */
583 [StructLayout (LayoutKind.Sequential)]
584 public struct SmallStruct7 {
589 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
593 res.j = (short)-ss.j;
598 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
600 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
601 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
603 public static int test_0_marshal_small_struct_delegate7 () {
604 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
607 /* TEST 8: 4 byte long FLOAT struct */
609 [StructLayout (LayoutKind.Sequential)]
610 public struct SmallStruct8 {
614 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
622 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
624 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
625 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
627 public static int test_0_marshal_small_struct_delegate8 () {
628 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
631 /* TEST 9: 8 byte long FLOAT struct */
633 [StructLayout (LayoutKind.Sequential)]
634 public struct SmallStruct9 {
638 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
646 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
648 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
649 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
651 public static int test_0_marshal_small_struct_delegate9 () {
652 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
655 /* TEST 10: 4+4 byte long FLOAT struct */
657 [StructLayout (LayoutKind.Sequential)]
658 public struct SmallStruct10 {
663 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
672 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
674 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
675 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
677 public static int test_0_marshal_small_struct_delegate10 () {
678 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
681 /* TEST 11: 4+4 byte long MIXED struct */
683 [StructLayout (LayoutKind.Sequential)]
684 public struct SmallStruct11 {
689 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
698 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
700 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
701 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
703 public static int test_0_marshal_small_struct_delegate11 () {
704 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
710 public delegate int ArrayDelegate1 (int i,
712 [In, MarshalAs(UnmanagedType.LPArray,
713 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
715 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
716 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
718 public static int array_delegate1 (int i, string j, string[] arr) {
721 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
726 public static int test_0_marshal_array_delegate_string () {
727 string[] arr = new string [] { "ABC", "DEF" };
728 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
731 public static int array_delegate2 (int i, string j, string[] arr) {
732 return (arr == null) ? 0 : 1;
735 public static int test_0_marshal_array_delegate_null () {
736 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
739 public delegate int ArrayDelegate3 (int i,
741 [In, MarshalAs(UnmanagedType.LPArray,
742 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
744 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
745 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
747 public static int array_delegate3 (int i, string j, string[] arr) {
748 return (arr == null) ? 0 : 1;
751 public static int test_0_marshal_array_delegate_bad_paramindex () {
753 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
756 catch (MarshalDirectiveException) {
761 public delegate int ArrayDelegate4 (int i,
763 [In, MarshalAs(UnmanagedType.LPArray,
764 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
766 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
767 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
769 public static int array_delegate4 (int i, string j, string[] arr) {
770 return (arr == null) ? 0 : 1;
773 public static int test_0_marshal_array_delegate_bad_paramtype () {
775 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
778 catch (MarshalDirectiveException) {
783 public delegate int ArrayDelegate4_2 (int i,
787 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
788 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr, int len, ArrayDelegate4_2 d);
790 public static int array_delegate4_2 (int i, string j, string[] arr) {
791 return (arr == null) ? 0 : 1;
794 public static int test_0_marshal_array_delegate_no_marshal_directive () {
796 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2));
799 catch (MarshalDirectiveException) {
804 public delegate int ArrayDelegate4_3 (int i,
808 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
809 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr, int len, ArrayDelegate4_3 d);
811 public int array_delegate4_3 (int i, string j, string[] arr) {
812 return (arr == null) ? 0 : 1;
815 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
817 Tests t = new Tests ();
818 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t.array_delegate4_3));
821 catch (MarshalDirectiveException) {
826 public delegate int ArrayDelegate5 (int i,
828 [In, MarshalAs(UnmanagedType.LPArray,
829 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
831 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
832 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
834 public static int array_delegate5 (int i, string j, string[] arr) {
837 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
842 public static int test_0_marshal_array_delegate_unicode_string () {
843 string[] arr = new string [] { "ABC", "DEF" };
844 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
847 public delegate int ArrayDelegate6 (int i,
849 [In, MarshalAs(UnmanagedType.LPArray,
850 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
852 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
853 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
855 public static int array_delegate6 (int i, string j, string[] arr) {
858 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
863 public static int test_0_marshal_array_delegate_sizeconst () {
864 string[] arr = new string [] { "ABC", "DEF" };
865 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
868 public delegate int ArrayDelegate7 (int i,
870 [In, MarshalAs(UnmanagedType.LPArray,
871 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
873 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
874 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
876 public static int array_delegate7 (int i, string j, string[] arr) {
879 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
884 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
885 string[] arr = new string [] { "ABC", "DEF" };
886 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
889 public delegate int ArrayDelegate8 (int i, string j,
890 [In, MarshalAs(UnmanagedType.LPArray,
894 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
895 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
897 public static int array_delegate8 (int i, string j, int[] arr) {
900 if ((arr [0] != 42) || (arr [1] != 43))
905 public static int test_0_marshal_array_delegate_blittable () {
906 int[] arr = new int [] { 42, 43 };
907 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
910 /* Array with size param of type long */
912 public delegate int ArrayDelegate8_2 (long i,
914 [In, MarshalAs(UnmanagedType.LPArray,
915 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
917 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate_long")]
918 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr, long len, ArrayDelegate8_2 d);
920 public static int array_delegate8_2 (long i, string j, string[] arr) {
923 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
928 public static int test_0_marshal_array_delegate_long_param () {
929 string[] arr = new string [] { "ABC", "DEF" };
930 return mono_test_marshal_array_delegate8_2 (arr, arr.Length, new ArrayDelegate8_2 (array_delegate8_2));
935 * [Out] blittable arrays
938 public delegate int ArrayDelegate9 (int i, string j,
939 [Out, MarshalAs(UnmanagedType.LPArray,
943 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
944 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
946 public static int array_delegate9 (int i, string j, int[] arr) {
956 public static int test_0_marshal_out_array_delegate () {
957 int[] arr = new int [] { 42, 43 };
958 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
962 * [Out] string arrays
965 public delegate int ArrayDelegate10 (int i,
967 [Out, MarshalAs(UnmanagedType.LPArray,
968 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
970 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
971 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
973 public static int array_delegate10 (int i, string j, string[] arr) {
983 public static int test_0_marshal_out_string_array_delegate () {
984 string[] arr = new string [] { "", "" };
985 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
992 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
994 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
995 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
997 public static int delegate_test_byval_class_inout (SimpleClass ss) {
998 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
1009 public static int test_0_marshal_inout_byval_class_delegate () {
1010 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
1014 * Returning unicode strings
1016 [return: MarshalAs(UnmanagedType.LPWStr)]
1017 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
1019 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
1020 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
1022 public static String return_unicode_string_delegate (string message) {
1026 public static int test_0_marshal_return_unicode_string_delegate () {
1027 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
1031 * Returning string arrays
1033 public delegate string[] ReturnArrayDelegate (int i);
1035 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
1036 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
1038 public static String[] return_array_delegate (int i) {
1039 String[] arr = new String [2];
1047 public static String[] return_array_delegate_null (int i) {
1051 public static int test_0_marshal_return_string_array_delegate () {
1052 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
1055 public static int test_3_marshal_return_string_array_delegate_null () {
1056 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));
1060 * Byref string marshalling
1062 public delegate int ByrefStringDelegate (ref string s);
1064 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_delegate")]
1065 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d);
1067 public static int byref_string_delegate (ref string s) {
1076 public static int test_0_marshal_byref_string_delegate () {
1077 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate));
1084 public delegate int SimpleDelegate (int i);
1086 [DllImport ("libtest", EntryPoint="mono_test_marshal_thread_attach")]
1087 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d);
1089 public static int test_43_thread_attach () {
1090 int res = mono_test_marshal_thread_attach (delegate (int i) {
1091 if (!Thread.CurrentThread.IsBackground)
1099 * Appdomain save/restore
1101 static Func<int> callback;
1103 [DllImport ("libtest", EntryPoint="mono_test_marshal_set_callback")]
1104 public static extern int mono_test_marshal_set_callback (Func<int> a);
1106 [DllImport ("libtest", EntryPoint="mono_test_marshal_call_callback")]
1107 public static extern int mono_test_marshal_call_callback ();
1109 public static int test_0_appdomain_switch () {
1110 // FIXME: The appdomain unload hangs
1112 AppDomain ad = AppDomain.CreateDomain ("foo");
1113 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1114 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1116 int domain_id = AppDomain.CurrentDomain.Id;
1117 int new_id = mono_test_marshal_call_callback ();
1119 if (new_id == domain_id)
1121 if (AppDomain.CurrentDomain.Id != domain_id)
1123 AppDomain.Unload (ad);
1127 static int domain_callback () {
1128 return AppDomain.CurrentDomain.Id;
1131 class CallbackClass : MarshalByRefObject {
1132 public int SetCallback () {
1133 mono_test_marshal_set_callback (domain_callback);