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 SimpleClass
delegate_test_class (SimpleClass ss
)
74 if (! (!ss
.a
&& ss
.b
&& !ss
.c
&& ss
.d
== "TEST"))
82 public static int delegate_test_class_byref (ref SimpleClass ss
)
87 if (!ss
.a
&& ss
.b
&& !ss
.c
&& ss
.d
== "TEST") {
99 public static int delegate_test_class_out (out SimpleClass ss
)
101 ss
= new SimpleClass ();
110 public static int delegate_test_primitive_byref (ref int i
)
119 public static int delegate_test_string_marshalling (string s
)
121 return s
== "ABC" ? 0 : 1;
124 public static int delegate_test_string_builder_marshalling (StringBuilder s
)
129 return s
.ToString () == "ABC" ? 0 : 1;
132 [DllImport ("libtest", EntryPoint
="mono_test_ref_vtype")]
133 public static extern int mono_test_ref_vtype (int a
, ref SimpleStruct ss
, int b
, TestDelegate d
);
135 public delegate int OutStructDelegate (int a
, out SimpleStruct ss
, int b
);
137 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_struct")]
138 public static extern int mono_test_marshal_out_struct (int a
, out SimpleStruct ss
, int b
, OutStructDelegate d
);
140 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate2")]
141 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d
);
143 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate4")]
144 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d
);
146 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate5")]
147 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d
);
149 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate6")]
150 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d
);
152 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate7")]
153 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d
);
155 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate8", CharSet
=CharSet
.Unicode
)]
156 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d
, string s
);
158 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate9")]
159 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d
, return_int_delegate d2
);
161 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate10")]
162 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d
);
164 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate8")]
165 public static extern int mono_test_marshal_delegate11 (SimpleDelegate11 d
, string s
);
167 [DllImport ("libtest", EntryPoint
="mono_test_marshal_primitive_byref_delegate")]
168 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d
);
170 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_delegate_delegate")]
171 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d
);
173 public delegate int TestDelegate (int a
, ref SimpleStruct ss
, int b
);
175 public delegate SimpleStruct
SimpleDelegate2 (SimpleStruct ss
);
177 public delegate SimpleClass
SimpleDelegate4 (SimpleClass ss
);
179 public delegate int SimpleDelegate5 (ref SimpleClass ss
);
181 public delegate int SimpleDelegate7 (out SimpleClass ss
);
183 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType
.LPWStr
)] string s1
);
185 public delegate int return_int_delegate (int i
);
187 public delegate int SimpleDelegate9 (return_int_delegate del
);
189 public delegate int SimpleDelegate11 (StringBuilder s1
);
191 public delegate int PrimitiveByrefDelegate (ref int i
);
193 public delegate return_int_delegate
ReturnDelegateDelegate ();
195 public static int Main () {
196 return TestDriver
.RunTests (typeof (Tests
));
199 /* Test structures as arguments and return values of delegates */
200 public static int test_0_marshal_struct_delegate () {
201 SimpleDelegate2 d
= new SimpleDelegate2 (delegate_test_struct
);
203 return mono_test_marshal_delegate2 (d
);
206 /* Test structures as byref arguments of delegates */
207 public static int test_0_marshal_byref_struct_delegate () {
208 SimpleStruct ss
= new SimpleStruct ();
209 TestDelegate d
= new TestDelegate (delegate_test_struct_byref
);
214 if (mono_test_ref_vtype (1, ref ss
, 2, d
) != 0)
217 if (! (ss
.a
&& ss
.b
&& ss
.c
&& ss
.d
== "TEST3"))
223 /* Test structures as out arguments of delegates */
224 public static int test_0_marshal_out_struct_delegate () {
225 SimpleStruct ss
= new SimpleStruct ();
226 OutStructDelegate d
= new OutStructDelegate (delegate_test_struct_out
);
228 return mono_test_marshal_out_struct (1, out ss
, 2, d
);
231 /* Test classes as arguments and return values of delegates */
232 public static int test_0_marshal_class_delegate () {
233 SimpleDelegate4 d
= new SimpleDelegate4 (delegate_test_class
);
235 return mono_test_marshal_delegate4 (d
);
238 /* Test classes as byref arguments of delegates */
239 public static int test_0_marshal_byref_class_delegate () {
240 SimpleDelegate5 d
= new SimpleDelegate5 (delegate_test_class_byref
);
242 return mono_test_marshal_delegate5 (d
);
245 /* Test classes as out arguments of delegates */
246 public static int test_0_marshal_out_class_delegate () {
247 SimpleDelegate7 d
= new SimpleDelegate7 (delegate_test_class_out
);
249 return mono_test_marshal_delegate7 (d
);
252 /* Test string marshalling with delegates */
253 public static int test_0_marshal_string_delegate () {
254 SimpleDelegate8 d
= new SimpleDelegate8 (delegate_test_string_marshalling
);
256 return mono_test_marshal_delegate8 (d
, "ABC");
259 /* Test string builder marshalling with delegates */
260 public static int test_0_marshal_string_builder_delegate () {
261 SimpleDelegate11 d
= new SimpleDelegate11 (delegate_test_string_builder_marshalling
);
263 if (mono_test_marshal_delegate11 (d
, null) != 2)
266 return mono_test_marshal_delegate11 (d
, "ABC");
269 /* Test that the delegate wrapper correctly catches null byref arguments */
270 public static int test_0_marshal_byref_class_delegate_null () {
271 SimpleDelegate5 d
= new SimpleDelegate5 (delegate_test_class_byref
);
274 mono_test_marshal_delegate6 (d
);
277 catch (ArgumentNullException ex
) {
282 static int return_self (int i
) {
286 static int call_int_delegate (return_int_delegate d
) {
290 public static int test_55_marshal_delegate_delegate () {
291 SimpleDelegate9 d
= new SimpleDelegate9 (call_int_delegate
);
293 return mono_test_marshal_delegate9 (d
, new return_int_delegate (return_self
));
296 public static int test_0_marshal_primitive_byref_delegate () {
297 PrimitiveByrefDelegate d
= new PrimitiveByrefDelegate (delegate_test_primitive_byref
);
299 return mono_test_marshal_primitive_byref_delegate (d
);
302 public static return_int_delegate
return_delegate () {
303 return new return_int_delegate (return_self
);
306 public static int test_55_marshal_return_delegate_delegate () {
307 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate
));
310 /* Passing and returning strings */
312 public delegate String
ReturnStringDelegate (String s
);
314 [DllImport ("libtest", EntryPoint
="mono_test_return_string")]
315 public static extern String
mono_test_marshal_return_string_delegate (ReturnStringDelegate d
);
317 public static String
managed_return_string (String s
) {
324 public static int test_0_marshal_return_string_delegate () {
325 ReturnStringDelegate d
= new ReturnStringDelegate (managed_return_string
);
326 String s
= mono_test_marshal_return_string_delegate (d
);
328 return (s
== "12345") ? 0 : 1;
331 /* Passing and returning enums */
333 public enum FooEnum
{
339 public delegate FooEnum
ReturnEnumDelegate (FooEnum e
);
341 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_enum_delegate")]
342 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d
);
344 public static FooEnum
managed_return_enum (FooEnum e
) {
345 return (FooEnum
)((int)e
+ 1);
348 public static int test_0_marshal_return_enum_delegate () {
349 ReturnEnumDelegate d
= new ReturnEnumDelegate (managed_return_enum
);
350 FooEnum e
= (FooEnum
)mono_test_marshal_return_enum_delegate (d
);
352 return e
== FooEnum
.Foo3
? 0 : 1;
355 /* Passing and returning blittable structs */
357 [StructLayout (LayoutKind
.Sequential
)]
358 public struct BlittableStruct
{
363 public static BlittableStruct
delegate_test_blittable_struct (BlittableStruct ss
)
375 public delegate BlittableStruct
SimpleDelegate10 (BlittableStruct ss
);
377 [DllImport ("libtest", EntryPoint
="mono_test_marshal_blittable_struct_delegate")]
378 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d
);
380 public static int test_0_marshal_blittable_struct_delegate () {
381 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct
));
385 * Passing and returning small structs
388 /* TEST 1: 4 byte long INTEGER struct */
390 [StructLayout (LayoutKind
.Sequential
)]
391 public struct SmallStruct1
{
395 public static SmallStruct1
delegate_test_struct (SmallStruct1 ss
) {
403 public delegate SmallStruct1
SmallStructDelegate1 (SmallStruct1 ss
);
405 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate1")]
406 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d
);
408 public static int test_0_marshal_small_struct_delegate1 () {
409 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct
));
412 /* TEST 2: 2+2 byte long INTEGER struct */
414 [StructLayout (LayoutKind
.Sequential
)]
415 public struct SmallStruct2
{
419 public static SmallStruct2
delegate_test_struct (SmallStruct2 ss
) {
422 res
.i
= (short)-ss
.i
;
423 res
.j
= (short)-ss
.j
;
428 public delegate SmallStruct2
SmallStructDelegate2 (SmallStruct2 ss
);
430 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate2")]
431 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d
);
433 public static int test_0_marshal_small_struct_delegate2 () {
434 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct
));
437 /* TEST 3: 2+1 byte long INTEGER struct */
439 [StructLayout (LayoutKind
.Sequential
)]
440 public struct SmallStruct3
{
445 public static SmallStruct3
delegate_test_struct (SmallStruct3 ss
) {
448 res
.i
= (short)-ss
.i
;
454 public delegate SmallStruct3
SmallStructDelegate3 (SmallStruct3 ss
);
456 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate3")]
457 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d
);
459 public static int test_0_marshal_small_struct_delegate3 () {
460 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct
));
463 /* TEST 4: 2 byte long INTEGER struct */
465 [StructLayout (LayoutKind
.Sequential
)]
466 public struct SmallStruct4
{
470 public static SmallStruct4
delegate_test_struct (SmallStruct4 ss
) {
473 res
.i
= (short)-ss
.i
;
478 public delegate SmallStruct4
SmallStructDelegate4 (SmallStruct4 ss
);
480 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate4")]
481 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d
);
483 public static int test_0_marshal_small_struct_delegate4 () {
484 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct
));
487 /* TEST 5: 8 byte long INTEGER struct */
489 [StructLayout (LayoutKind
.Sequential
)]
490 public struct SmallStruct5
{
494 public static SmallStruct5
delegate_test_struct (SmallStruct5 ss
) {
502 public delegate SmallStruct5
SmallStructDelegate5 (SmallStruct5 ss
);
504 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate5")]
505 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d
);
507 public static int test_0_marshal_small_struct_delegate5 () {
508 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct
));
511 /* TEST 6: 4+4 byte long INTEGER struct */
513 [StructLayout (LayoutKind
.Sequential
)]
514 public struct SmallStruct6
{
518 public static SmallStruct6
delegate_test_struct (SmallStruct6 ss
) {
527 public delegate SmallStruct6
SmallStructDelegate6 (SmallStruct6 ss
);
529 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate6")]
530 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d
);
532 public static int test_0_marshal_small_struct_delegate6 () {
533 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct
));
536 /* TEST 7: 4+2 byte long INTEGER struct */
538 [StructLayout (LayoutKind
.Sequential
)]
539 public struct SmallStruct7
{
544 public static SmallStruct7
delegate_test_struct (SmallStruct7 ss
) {
548 res
.j
= (short)-ss
.j
;
553 public delegate SmallStruct7
SmallStructDelegate7 (SmallStruct7 ss
);
555 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate7")]
556 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d
);
558 public static int test_0_marshal_small_struct_delegate7 () {
559 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct
));
562 /* TEST 8: 4 byte long FLOAT struct */
564 [StructLayout (LayoutKind
.Sequential
)]
565 public struct SmallStruct8
{
569 public static SmallStruct8
delegate_test_struct (SmallStruct8 ss
) {
577 public delegate SmallStruct8
SmallStructDelegate8 (SmallStruct8 ss
);
579 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate8")]
580 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d
);
582 public static int test_0_marshal_small_struct_delegate8 () {
583 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct
));
586 /* TEST 9: 8 byte long FLOAT struct */
588 [StructLayout (LayoutKind
.Sequential
)]
589 public struct SmallStruct9
{
593 public static SmallStruct9
delegate_test_struct (SmallStruct9 ss
) {
601 public delegate SmallStruct9
SmallStructDelegate9 (SmallStruct9 ss
);
603 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate9")]
604 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d
);
606 public static int test_0_marshal_small_struct_delegate9 () {
607 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct
));
610 /* TEST 10: 4+4 byte long FLOAT struct */
612 [StructLayout (LayoutKind
.Sequential
)]
613 public struct SmallStruct10
{
618 public static SmallStruct10
delegate_test_struct (SmallStruct10 ss
) {
627 public delegate SmallStruct10
SmallStructDelegate10 (SmallStruct10 ss
);
629 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate10")]
630 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d
);
632 public static int test_0_marshal_small_struct_delegate10 () {
633 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct
));
636 /* TEST 11: 4+4 byte long MIXED struct */
638 [StructLayout (LayoutKind
.Sequential
)]
639 public struct SmallStruct11
{
644 public static SmallStruct11
delegate_test_struct (SmallStruct11 ss
) {
653 public delegate SmallStruct11
SmallStructDelegate11 (SmallStruct11 ss
);
655 [DllImport ("libtest", EntryPoint
="mono_test_marshal_small_struct_delegate11")]
656 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d
);
658 public static int test_0_marshal_small_struct_delegate11 () {
659 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct
));
665 public delegate int ArrayDelegate1 (int i
,
667 [In
, MarshalAs(UnmanagedType
.LPArray
,
668 ArraySubType
=UnmanagedType
.LPStr
, SizeParamIndex
=0)] string[] arr
);
670 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
671 public static extern int mono_test_marshal_array_delegate1 (string[] arr
, int len
, ArrayDelegate1 d
);
673 public static int array_delegate1 (int i
, string j
, string[] arr
) {
676 if ((arr
[0] != "ABC") || (arr
[1] != "DEF"))
681 public static int test_0_marshal_array_delegate_string () {
682 string[] arr
= new string [] { "ABC", "DEF" }
;
683 return mono_test_marshal_array_delegate1 (arr
, arr
.Length
, new ArrayDelegate1 (array_delegate1
));
686 public static int array_delegate2 (int i
, string j
, string[] arr
) {
687 return (arr
== null) ? 0 : 1;
690 public static int test_0_marshal_array_delegate_null () {
691 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2
));
694 public delegate int ArrayDelegate3 (int i
,
696 [In
, MarshalAs(UnmanagedType
.LPArray
,
697 ArraySubType
=UnmanagedType
.LPStr
, SizeParamIndex
=3)] string[] arr
);
699 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
700 public static extern int mono_test_marshal_array_delegate3 (string[] arr
, int len
, ArrayDelegate3 d
);
702 public static int array_delegate3 (int i
, string j
, string[] arr
) {
703 return (arr
== null) ? 0 : 1;
706 public static int test_0_marshal_array_delegate_bad_paramindex () {
708 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3
));
711 catch (MarshalDirectiveException
) {
716 public delegate int ArrayDelegate4 (int i
,
718 [In
, MarshalAs(UnmanagedType
.LPArray
,
719 ArraySubType
=UnmanagedType
.LPStr
, SizeParamIndex
=1)] string[] arr
);
721 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
722 public static extern int mono_test_marshal_array_delegate4 (string[] arr
, int len
, ArrayDelegate4 d
);
724 public static int array_delegate4 (int i
, string j
, string[] arr
) {
725 return (arr
== null) ? 0 : 1;
728 public static int test_0_marshal_array_delegate_bad_paramtype () {
730 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4
));
733 catch (MarshalDirectiveException
) {
738 public delegate int ArrayDelegate4_2 (int i
,
742 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
743 public static extern int mono_test_marshal_array_delegate4_2 (string[] arr
, int len
, ArrayDelegate4_2 d
);
745 public static int array_delegate4_2 (int i
, string j
, string[] arr
) {
746 return (arr
== null) ? 0 : 1;
749 public static int test_0_marshal_array_delegate_no_marshal_directive () {
751 mono_test_marshal_array_delegate4_2 (null, 0, new ArrayDelegate4_2 (array_delegate4_2
));
754 catch (MarshalDirectiveException
) {
759 public delegate int ArrayDelegate4_3 (int i
,
763 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
764 public static extern int mono_test_marshal_array_delegate4_3 (string[] arr
, int len
, ArrayDelegate4_3 d
);
766 public int array_delegate4_3 (int i
, string j
, string[] arr
) {
767 return (arr
== null) ? 0 : 1;
770 public static int test_0_marshal_array_delegate_no_marshal_directive_instance () {
772 Tests t
= new Tests ();
773 mono_test_marshal_array_delegate4_3 (null, 0, new ArrayDelegate4_3 (t
.array_delegate4_3
));
776 catch (MarshalDirectiveException
) {
781 public delegate int ArrayDelegate5 (int i
,
783 [In
, MarshalAs(UnmanagedType
.LPArray
,
784 ArraySubType
=UnmanagedType
.LPWStr
, SizeParamIndex
=0)] string[] arr
);
786 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate", CharSet
=CharSet
.Unicode
)]
787 public static extern int mono_test_marshal_array_delegate5 (string[] arr
, int len
, ArrayDelegate5 d
);
789 public static int array_delegate5 (int i
, string j
, string[] arr
) {
792 if ((arr
[0] != "ABC") || (arr
[1] != "DEF"))
797 public static int test_0_marshal_array_delegate_unicode_string () {
798 string[] arr
= new string [] { "ABC", "DEF" }
;
799 return mono_test_marshal_array_delegate5 (arr
, arr
.Length
, new ArrayDelegate5 (array_delegate5
));
802 public delegate int ArrayDelegate6 (int i
,
804 [In
, MarshalAs(UnmanagedType
.LPArray
,
805 ArraySubType
=UnmanagedType
.LPStr
, SizeConst
=2)] string[] arr
);
807 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
808 public static extern int mono_test_marshal_array_delegate6 (string[] arr
, int len
, ArrayDelegate6 d
);
810 public static int array_delegate6 (int i
, string j
, string[] arr
) {
813 if ((arr
[0] != "ABC") || (arr
[1] != "DEF"))
818 public static int test_0_marshal_array_delegate_sizeconst () {
819 string[] arr
= new string [] { "ABC", "DEF" }
;
820 return mono_test_marshal_array_delegate6 (arr
, 1024, new ArrayDelegate6 (array_delegate6
));
823 public delegate int ArrayDelegate7 (int i
,
825 [In
, MarshalAs(UnmanagedType
.LPArray
,
826 ArraySubType
=UnmanagedType
.LPStr
, SizeConst
=1, SizeParamIndex
=0)] string[] arr
);
828 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
829 public static extern int mono_test_marshal_array_delegate7 (string[] arr
, int len
, ArrayDelegate7 d
);
831 public static int array_delegate7 (int i
, string j
, string[] arr
) {
834 if ((arr
[0] != "ABC") || (arr
[1] != "DEF"))
839 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
840 string[] arr
= new string [] { "ABC", "DEF" }
;
841 return mono_test_marshal_array_delegate7 (arr
, 1, new ArrayDelegate7 (array_delegate7
));
844 public delegate int ArrayDelegate8 (int i
, string j
,
845 [In
, MarshalAs(UnmanagedType
.LPArray
,
849 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate")]
850 public static extern int mono_test_marshal_array_delegate8 (int[] arr
, int len
, ArrayDelegate8 d
);
852 public static int array_delegate8 (int i
, string j
, int[] arr
) {
855 if ((arr
[0] != 42) || (arr
[1] != 43))
860 public static int test_0_marshal_array_delegate_blittable () {
861 int[] arr
= new int [] { 42, 43 }
;
862 return mono_test_marshal_array_delegate8 (arr
, 2, new ArrayDelegate8 (array_delegate8
));
865 /* Array with size param of type long */
867 public delegate int ArrayDelegate8_2 (long i
,
869 [In
, MarshalAs(UnmanagedType
.LPArray
,
870 ArraySubType
=UnmanagedType
.LPStr
, SizeParamIndex
=0)] string[] arr
);
872 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array_delegate_long")]
873 public static extern int mono_test_marshal_array_delegate8_2 (string[] arr
, long len
, ArrayDelegate8_2 d
);
875 public static int array_delegate8_2 (long i
, string j
, string[] arr
) {
878 if ((arr
[0] != "ABC") || (arr
[1] != "DEF"))
883 public static int test_0_marshal_array_delegate_long_param () {
884 string[] arr
= new string [] { "ABC", "DEF" }
;
885 return mono_test_marshal_array_delegate8_2 (arr
, arr
.Length
, new ArrayDelegate8_2 (array_delegate8_2
));
890 * [Out] blittable arrays
893 public delegate int ArrayDelegate9 (int i
, string j
,
894 [Out
, MarshalAs(UnmanagedType
.LPArray
,
898 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_array_delegate")]
899 public static extern int mono_test_marshal_out_array_delegate (int[] arr
, int len
, ArrayDelegate9 d
);
901 public static int array_delegate9 (int i
, string j
, int[] arr
) {
911 public static int test_0_marshal_out_array_delegate () {
912 int[] arr
= new int [] { 42, 43 }
;
913 return mono_test_marshal_out_array_delegate (arr
, 2, new ArrayDelegate9 (array_delegate9
));
917 * [Out] string arrays
920 public delegate int ArrayDelegate10 (int i
,
922 [Out
, MarshalAs(UnmanagedType
.LPArray
,
923 ArraySubType
=UnmanagedType
.LPStr
, SizeConst
=2)] string[] arr
);
925 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_string_array_delegate")]
926 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr
, int len
, ArrayDelegate10 d
);
928 public static int array_delegate10 (int i
, string j
, string[] arr
) {
938 public static int test_0_marshal_out_string_array_delegate () {
939 string[] arr
= new string [] { "", "" }
;
940 return mono_test_marshal_out_string_array_delegate (arr
, 2, new ArrayDelegate10 (array_delegate10
));
947 public delegate int InOutByvalClassDelegate ([In
, Out
] SimpleClass ss
);
949 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_byval_class_delegate")]
950 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d
);
952 public static int delegate_test_byval_class_inout (SimpleClass ss
) {
953 if ((ss
.a
!= false) || (ss
.b
!= true) || (ss
.c
!= false) || (ss
.d
!= "FOO"))
964 public static int test_0_marshal_inout_byval_class_delegate () {
965 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout
));
969 * Returning unicode strings
971 [return: MarshalAs(UnmanagedType
.LPWStr
)]
972 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType
.LPWStr
)] string message
);
974 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_unicode_string_delegate")]
975 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d
);
977 public static String
return_unicode_string_delegate (string message
) {
981 public static int test_0_marshal_return_unicode_string_delegate () {
982 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate
));
986 * Returning string arrays
988 public delegate string[] ReturnArrayDelegate (int i
);
990 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_string_array_delegate")]
991 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d
);
993 public static String
[] return_array_delegate (int i
) {
994 String
[] arr
= new String
[2];
1002 public static String
[] return_array_delegate_null (int i
) {
1006 public static int test_0_marshal_return_string_array_delegate () {
1007 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate
));
1010 public static int test_3_marshal_return_string_array_delegate_null () {
1011 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null
));
1015 * Byref string marshalling
1017 public delegate int ByrefStringDelegate (ref string s
);
1019 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_string_delegate")]
1020 public static extern int mono_test_marshal_byref_string_delegate (ByrefStringDelegate d
);
1022 public static int byref_string_delegate (ref string s
) {
1031 public static int test_0_marshal_byref_string_delegate () {
1032 return mono_test_marshal_byref_string_delegate (new ByrefStringDelegate (byref_string_delegate
));
1039 public delegate int SimpleDelegate (int i
);
1041 [DllImport ("libtest", EntryPoint
="mono_test_marshal_thread_attach")]
1042 public static extern int mono_test_marshal_thread_attach (SimpleDelegate d
);
1044 public static int test_43_thread_attach () {
1045 int res
= mono_test_marshal_thread_attach (delegate (int i
) {
1046 if (!Thread
.CurrentThread
.IsBackground
)
1054 * Appdomain save/restore
1056 static Func
<int> callback
;
1058 [DllImport ("libtest", EntryPoint
="mono_test_marshal_set_callback")]
1059 public static extern int mono_test_marshal_set_callback (Func
<int> a
);
1061 [DllImport ("libtest", EntryPoint
="mono_test_marshal_call_callback")]
1062 public static extern int mono_test_marshal_call_callback ();
1064 public static int test_0_appdomain_swich () {
1065 callback
= delegate () { return 42; }
;
1066 mono_test_marshal_set_callback (callback
);
1068 // FIXME: The appdomain unload hangs
1071 AppDomain ad = AppDomain.CreateDomain ("foo");
1072 var c = (CallbackClass)ad.CreateInstanceAndUnwrap (
1073 typeof (CallbackClass).Assembly.FullName, "Tests/CallbackClass");
1074 int res = c.OtherDomainTest ();
1075 AppDomain.Unload (ad);
1080 class CallbackClass
: MarshalByRefObject
{
1081 public int OtherDomainTest () {
1082 int appDomainId
= AppDomain
.CurrentDomain
.Id
;
1083 int res
= mono_test_marshal_call_callback ();
1086 return appDomainId
== AppDomain
.CurrentDomain
.Id
? 0 : 1;