2007-12-06 Jb Evain <jbevain@novell.com>
[mono.git] / mono / tests / pinvoke3.cs
blob981f542968455794f54d8d14c3ba8c2a7ef261e2
1 //
2 // pinvoke3.cs:
3 //
4 // Tests for native->managed marshalling
5 //
7 using System;
8 using System.Runtime.InteropServices;
10 public class Tests {
12 [StructLayout (LayoutKind.Sequential)]
13 public struct SimpleStruct {
14 public bool a;
15 public bool b;
16 public bool c;
17 public string d;
18 [MarshalAs(UnmanagedType.LPWStr)]
19 public string d2;
22 [StructLayout (LayoutKind.Sequential)]
23 public class SimpleClass {
24 public bool a;
25 public bool b;
26 public bool c;
27 public string d;
30 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
32 SimpleStruct res;
34 res.a = !ss.a;
35 res.b = !ss.b;
36 res.c = !ss.c;
37 res.d = ss.d + "-RES";
38 res.d2 = ss.d2 + "-RES";
40 return res;
43 public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
45 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
46 ss.a = true;
47 ss.b = true;
48 ss.c = true;
49 ss.d = "TEST3";
50 return 0;
53 return 1;
56 public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
58 ss.a = true;
59 ss.b = true;
60 ss.c = true;
61 ss.d = "TEST3";
62 ss.d2 = "TEST4";
64 return 0;
67 public static SimpleClass delegate_test_class (SimpleClass ss)
69 if (ss == null)
70 return null;
72 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
73 return null;
75 SimpleClass res = ss;
77 return res;
80 public static int delegate_test_class_byref (ref SimpleClass ss)
82 if (ss == null)
83 return -1;
85 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
86 ss.a = true;
87 ss.b = false;
88 ss.c = true;
89 ss.d = "RES";
91 return 0;
94 return 1;
97 public static int delegate_test_class_out (out SimpleClass ss)
99 ss = new SimpleClass ();
100 ss.a = true;
101 ss.b = false;
102 ss.c = true;
103 ss.d = "RES";
105 return 0;
108 public static int delegate_test_primitive_byref (ref int i)
110 if (i != 1)
111 return 1;
113 i = 2;
114 return 0;
117 public static int delegate_test_string_marshalling (string s)
119 return s == "ABC" ? 0 : 1;
122 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
123 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
125 public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
127 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
128 public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
130 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
131 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
133 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
134 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
136 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
137 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
139 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
140 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
142 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
143 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
145 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
146 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
148 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
149 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
151 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
152 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
154 [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
155 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
157 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
158 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
160 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
162 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
164 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
166 public delegate int SimpleDelegate5 (ref SimpleClass ss);
168 public delegate int SimpleDelegate7 (out SimpleClass ss);
170 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
172 public delegate int return_int_delegate (int i);
174 public delegate int SimpleDelegate9 (return_int_delegate del);
176 public delegate int PrimitiveByrefDelegate (ref int i);
178 public delegate return_int_delegate ReturnDelegateDelegate ();
180 public static int Main () {
181 return TestDriver.RunTests (typeof (Tests));
184 /* Test structures as arguments and return values of delegates */
185 public static int test_0_marshal_struct_delegate () {
186 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
188 return mono_test_marshal_delegate2 (d);
191 /* Test structures as byref arguments of delegates */
192 public static int test_0_marshal_byref_struct_delegate () {
193 SimpleStruct ss = new SimpleStruct ();
194 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
196 ss.b = true;
197 ss.d = "TEST1";
199 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
200 return 1;
202 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
203 return 2;
205 return 0;
208 /* Test structures as out arguments of delegates */
209 public static int test_0_marshal_out_struct_delegate () {
210 SimpleStruct ss = new SimpleStruct ();
211 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
213 return mono_test_marshal_out_struct (1, out ss, 2, d);
216 /* Test classes as arguments and return values of delegates */
217 public static int test_0_marshal_class_delegate () {
218 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
220 return mono_test_marshal_delegate4 (d);
223 /* Test classes as byref arguments of delegates */
224 public static int test_0_marshal_byref_class_delegate () {
225 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
227 return mono_test_marshal_delegate5 (d);
230 /* Test classes as out arguments of delegates */
231 public static int test_0_marshal_out_class_delegate () {
232 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
234 return mono_test_marshal_delegate7 (d);
237 /* Test string marshalling with delegates */
238 public static int test_0_marshal_string_delegate () {
239 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
241 return mono_test_marshal_delegate8 (d, "ABC");
244 /* Test that the delegate wrapper correctly catches null byref arguments */
245 public static int test_0_marshal_byref_class_delegate_null () {
246 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
248 try {
249 mono_test_marshal_delegate6 (d);
250 return 1;
252 catch (ArgumentNullException ex) {
253 return 0;
257 static int return_self (int i) {
258 return i;
261 static int call_int_delegate (return_int_delegate d) {
262 return d (55);
265 public static int test_55_marshal_delegate_delegate () {
266 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
268 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
271 public static int test_0_marshal_primitive_byref_delegate () {
272 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
274 return mono_test_marshal_primitive_byref_delegate (d);
277 public static return_int_delegate return_delegate () {
278 return new return_int_delegate (return_self);
281 public static int test_55_marshal_return_delegate_delegate () {
282 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
285 /* Passing and returning strings */
287 public delegate String ReturnStringDelegate (String s);
289 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
290 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
292 public static String managed_return_string (String s) {
293 if (s != "TEST")
294 return "";
295 else
296 return "12345";
299 public static int test_0_marshal_return_string_delegate () {
300 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
301 String s = mono_test_marshal_return_string_delegate (d);
303 return (s == "12345") ? 0 : 1;
306 /* Passing and returning enums */
308 public enum FooEnum {
309 Foo1,
310 Foo2,
311 Foo3
314 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
316 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
317 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
319 public static FooEnum managed_return_enum (FooEnum e) {
320 return (FooEnum)((int)e + 1);
323 public static int test_0_marshal_return_enum_delegate () {
324 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
325 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
327 return e == FooEnum.Foo3 ? 0 : 1;
330 /* Passing and returning blittable structs */
332 [StructLayout (LayoutKind.Sequential)]
333 public struct BlittableStruct {
334 public int a, b, c;
335 public long d;
338 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
340 BlittableStruct res;
342 res.a = -ss.a;
343 res.b = -ss.b;
344 res.c = -ss.c;
345 res.d = -ss.d;
347 return res;
350 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
352 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
353 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
355 public static int test_0_marshal_blittable_struct_delegate () {
356 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));
360 * Passing and returning small structs
363 /* TEST 1: 4 byte long INTEGER struct */
365 [StructLayout (LayoutKind.Sequential)]
366 public struct SmallStruct1 {
367 public int i;
370 public static SmallStruct1 delegate_test_struct (SmallStruct1 ss) {
371 SmallStruct1 res;
373 res.i = -ss.i;
375 return res;
378 public delegate SmallStruct1 SmallStructDelegate1 (SmallStruct1 ss);
380 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate1")]
381 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate1 d);
383 public static int test_0_marshal_small_struct_delegate1 () {
384 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate1 (delegate_test_struct));
387 /* TEST 2: 2+2 byte long INTEGER struct */
389 [StructLayout (LayoutKind.Sequential)]
390 public struct SmallStruct2 {
391 public short i, j;
394 public static SmallStruct2 delegate_test_struct (SmallStruct2 ss) {
395 SmallStruct2 res;
397 res.i = (short)-ss.i;
398 res.j = (short)-ss.j;
400 return res;
403 public delegate SmallStruct2 SmallStructDelegate2 (SmallStruct2 ss);
405 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate2")]
406 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate2 d);
408 public static int test_0_marshal_small_struct_delegate2 () {
409 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate2 (delegate_test_struct));
412 /* TEST 3: 2+1 byte long INTEGER struct */
414 [StructLayout (LayoutKind.Sequential)]
415 public struct SmallStruct3 {
416 public short i;
417 public byte j;
420 public static SmallStruct3 delegate_test_struct (SmallStruct3 ss) {
421 SmallStruct3 res;
423 res.i = (short)-ss.i;
424 res.j = (byte)-ss.j;
426 return res;
429 public delegate SmallStruct3 SmallStructDelegate3 (SmallStruct3 ss);
431 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate3")]
432 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate3 d);
434 public static int test_0_marshal_small_struct_delegate3 () {
435 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate3 (delegate_test_struct));
438 /* TEST 4: 2 byte long INTEGER struct */
440 [StructLayout (LayoutKind.Sequential)]
441 public struct SmallStruct4 {
442 public short i;
445 public static SmallStruct4 delegate_test_struct (SmallStruct4 ss) {
446 SmallStruct4 res;
448 res.i = (short)-ss.i;
450 return res;
453 public delegate SmallStruct4 SmallStructDelegate4 (SmallStruct4 ss);
455 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate4")]
456 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate4 d);
458 public static int test_0_marshal_small_struct_delegate4 () {
459 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate4 (delegate_test_struct));
462 /* TEST 5: 8 byte long INTEGER struct */
464 [StructLayout (LayoutKind.Sequential)]
465 public struct SmallStruct5 {
466 public long l;
469 public static SmallStruct5 delegate_test_struct (SmallStruct5 ss) {
470 SmallStruct5 res;
472 res.l = -ss.l;
474 return res;
477 public delegate SmallStruct5 SmallStructDelegate5 (SmallStruct5 ss);
479 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate5")]
480 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate5 d);
482 public static int test_0_marshal_small_struct_delegate5 () {
483 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate5 (delegate_test_struct));
486 /* TEST 6: 4+4 byte long INTEGER struct */
488 [StructLayout (LayoutKind.Sequential)]
489 public struct SmallStruct6 {
490 public int i, j;
493 public static SmallStruct6 delegate_test_struct (SmallStruct6 ss) {
494 SmallStruct6 res;
496 res.i = -ss.i;
497 res.j = -ss.j;
499 return res;
502 public delegate SmallStruct6 SmallStructDelegate6 (SmallStruct6 ss);
504 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate6")]
505 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate6 d);
507 public static int test_0_marshal_small_struct_delegate6 () {
508 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate6 (delegate_test_struct));
511 /* TEST 7: 4+2 byte long INTEGER struct */
513 [StructLayout (LayoutKind.Sequential)]
514 public struct SmallStruct7 {
515 public int i;
516 public short j;
519 public static SmallStruct7 delegate_test_struct (SmallStruct7 ss) {
520 SmallStruct7 res;
522 res.i = -ss.i;
523 res.j = (short)-ss.j;
525 return res;
528 public delegate SmallStruct7 SmallStructDelegate7 (SmallStruct7 ss);
530 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate7")]
531 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate7 d);
533 public static int test_0_marshal_small_struct_delegate7 () {
534 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate7 (delegate_test_struct));
537 /* TEST 8: 4 byte long FLOAT struct */
539 [StructLayout (LayoutKind.Sequential)]
540 public struct SmallStruct8 {
541 public float i;
544 public static SmallStruct8 delegate_test_struct (SmallStruct8 ss) {
545 SmallStruct8 res;
547 res.i = -ss.i;
549 return res;
552 public delegate SmallStruct8 SmallStructDelegate8 (SmallStruct8 ss);
554 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate8")]
555 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate8 d);
557 public static int test_0_marshal_small_struct_delegate8 () {
558 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate8 (delegate_test_struct));
561 /* TEST 9: 8 byte long FLOAT struct */
563 [StructLayout (LayoutKind.Sequential)]
564 public struct SmallStruct9 {
565 public double i;
568 public static SmallStruct9 delegate_test_struct (SmallStruct9 ss) {
569 SmallStruct9 res;
571 res.i = -ss.i;
573 return res;
576 public delegate SmallStruct9 SmallStructDelegate9 (SmallStruct9 ss);
578 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate9")]
579 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate9 d);
581 public static int test_0_marshal_small_struct_delegate9 () {
582 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate9 (delegate_test_struct));
585 /* TEST 10: 4+4 byte long FLOAT struct */
587 [StructLayout (LayoutKind.Sequential)]
588 public struct SmallStruct10 {
589 public float i;
590 public float j;
593 public static SmallStruct10 delegate_test_struct (SmallStruct10 ss) {
594 SmallStruct10 res;
596 res.i = -ss.i;
597 res.j = -ss.j;
599 return res;
602 public delegate SmallStruct10 SmallStructDelegate10 (SmallStruct10 ss);
604 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate10")]
605 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate10 d);
607 public static int test_0_marshal_small_struct_delegate10 () {
608 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate10 (delegate_test_struct));
611 /* TEST 11: 4+4 byte long MIXED struct */
613 [StructLayout (LayoutKind.Sequential)]
614 public struct SmallStruct11 {
615 public float i;
616 public int j;
619 public static SmallStruct11 delegate_test_struct (SmallStruct11 ss) {
620 SmallStruct11 res;
622 res.i = -ss.i;
623 res.j = -ss.j;
625 return res;
628 public delegate SmallStruct11 SmallStructDelegate11 (SmallStruct11 ss);
630 [DllImport ("libtest", EntryPoint="mono_test_marshal_small_struct_delegate11")]
631 public static extern int mono_test_marshal_small_struct_delegate (SmallStructDelegate11 d);
633 public static int test_0_marshal_small_struct_delegate11 () {
634 return mono_test_marshal_small_struct_delegate (new SmallStructDelegate11 (delegate_test_struct));
638 * Passing arrays
640 public delegate int ArrayDelegate1 (int i,
641 string j,
642 [In, MarshalAs(UnmanagedType.LPArray,
643 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=0)] string[] arr);
645 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
646 public static extern int mono_test_marshal_array_delegate1 (string[] arr, int len, ArrayDelegate1 d);
648 public static int array_delegate1 (int i, string j, string[] arr) {
649 if (arr.Length != 2)
650 return 1;
651 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
652 return 2;
653 return 0;
656 public static int test_0_marshal_array_delegate_string () {
657 string[] arr = new string [] { "ABC", "DEF" };
658 return mono_test_marshal_array_delegate1 (arr, arr.Length, new ArrayDelegate1 (array_delegate1));
661 public static int array_delegate2 (int i, string j, string[] arr) {
662 return (arr == null) ? 0 : 1;
665 public static int test_0_marshal_array_delegate_null () {
666 return mono_test_marshal_array_delegate1 (null, 0, new ArrayDelegate1 (array_delegate2));
669 public delegate int ArrayDelegate3 (int i,
670 string j,
671 [In, MarshalAs(UnmanagedType.LPArray,
672 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=3)] string[] arr);
674 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
675 public static extern int mono_test_marshal_array_delegate3 (string[] arr, int len, ArrayDelegate3 d);
677 public static int array_delegate3 (int i, string j, string[] arr) {
678 return (arr == null) ? 0 : 1;
681 public static int test_0_marshal_array_delegate_bad_paramindex () {
682 try {
683 mono_test_marshal_array_delegate3 (null, 0, new ArrayDelegate3 (array_delegate3));
684 return 1;
686 catch (MarshalDirectiveException) {
687 return 0;
691 public delegate int ArrayDelegate4 (int i,
692 string j,
693 [In, MarshalAs(UnmanagedType.LPArray,
694 ArraySubType=UnmanagedType.LPStr, SizeParamIndex=1)] string[] arr);
696 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
697 public static extern int mono_test_marshal_array_delegate4 (string[] arr, int len, ArrayDelegate4 d);
699 public static int array_delegate4 (int i, string j, string[] arr) {
700 return (arr == null) ? 0 : 1;
703 public static int test_0_marshal_array_delegate_bad_paramtype () {
704 try {
705 mono_test_marshal_array_delegate4 (null, 0, new ArrayDelegate4 (array_delegate4));
706 return 1;
708 catch (MarshalDirectiveException) {
709 return 0;
713 public delegate int ArrayDelegate5 (int i,
714 string j,
715 [In, MarshalAs(UnmanagedType.LPArray,
716 ArraySubType=UnmanagedType.LPWStr, SizeParamIndex=0)] string[] arr);
718 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate", CharSet=CharSet.Unicode)]
719 public static extern int mono_test_marshal_array_delegate5 (string[] arr, int len, ArrayDelegate5 d);
721 public static int array_delegate5 (int i, string j, string[] arr) {
722 if (arr.Length != 2)
723 return 1;
724 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
725 return 2;
726 return 0;
729 public static int test_0_marshal_array_delegate_unicode_string () {
730 string[] arr = new string [] { "ABC", "DEF" };
731 return mono_test_marshal_array_delegate5 (arr, arr.Length, new ArrayDelegate5 (array_delegate5));
734 public delegate int ArrayDelegate6 (int i,
735 string j,
736 [In, MarshalAs(UnmanagedType.LPArray,
737 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
739 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
740 public static extern int mono_test_marshal_array_delegate6 (string[] arr, int len, ArrayDelegate6 d);
742 public static int array_delegate6 (int i, string j, string[] arr) {
743 if (arr.Length != 2)
744 return 1;
745 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
746 return 2;
747 return 0;
750 public static int test_0_marshal_array_delegate_sizeconst () {
751 string[] arr = new string [] { "ABC", "DEF" };
752 return mono_test_marshal_array_delegate6 (arr, 1024, new ArrayDelegate6 (array_delegate6));
755 public delegate int ArrayDelegate7 (int i,
756 string j,
757 [In, MarshalAs(UnmanagedType.LPArray,
758 ArraySubType=UnmanagedType.LPStr, SizeConst=1, SizeParamIndex=0)] string[] arr);
760 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
761 public static extern int mono_test_marshal_array_delegate7 (string[] arr, int len, ArrayDelegate7 d);
763 public static int array_delegate7 (int i, string j, string[] arr) {
764 if (arr.Length != 2)
765 return 1;
766 if ((arr [0] != "ABC") || (arr [1] != "DEF"))
767 return 2;
768 return 0;
771 public static int test_0_marshal_array_delegate_sizeconst_paramindex () {
772 string[] arr = new string [] { "ABC", "DEF" };
773 return mono_test_marshal_array_delegate7 (arr, 1, new ArrayDelegate7 (array_delegate7));
776 public delegate int ArrayDelegate8 (int i, string j,
777 [In, MarshalAs(UnmanagedType.LPArray,
778 SizeParamIndex=0)]
779 int[] arr);
781 [DllImport ("libtest", EntryPoint="mono_test_marshal_array_delegate")]
782 public static extern int mono_test_marshal_array_delegate8 (int[] arr, int len, ArrayDelegate8 d);
784 public static int array_delegate8 (int i, string j, int[] arr) {
785 if (arr.Length != 2)
786 return 1;
787 if ((arr [0] != 42) || (arr [1] != 43))
788 return 2;
789 return 0;
792 public static int test_0_marshal_array_delegate_blittable () {
793 int[] arr = new int [] { 42, 43 };
794 return mono_test_marshal_array_delegate8 (arr, 2, new ArrayDelegate8 (array_delegate8));
798 * [Out] blittable arrays
801 public delegate int ArrayDelegate9 (int i, string j,
802 [Out, MarshalAs(UnmanagedType.LPArray,
803 SizeParamIndex=0)]
804 int[] arr);
806 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array_delegate")]
807 public static extern int mono_test_marshal_out_array_delegate (int[] arr, int len, ArrayDelegate9 d);
809 public static int array_delegate9 (int i, string j, int[] arr) {
810 if (arr.Length != 2)
811 return 1;
813 arr [0] = 1;
814 arr [1] = 2;
816 return 0;
819 public static int test_0_marshal_out_array_delegate () {
820 int[] arr = new int [] { 42, 43 };
821 return mono_test_marshal_out_array_delegate (arr, 2, new ArrayDelegate9 (array_delegate9));
825 * [Out] string arrays
828 public delegate int ArrayDelegate10 (int i,
829 string j,
830 [Out, MarshalAs(UnmanagedType.LPArray,
831 ArraySubType=UnmanagedType.LPStr, SizeConst=2)] string[] arr);
833 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_string_array_delegate")]
834 public static extern int mono_test_marshal_out_string_array_delegate (string[] arr, int len, ArrayDelegate10 d);
836 public static int array_delegate10 (int i, string j, string[] arr) {
837 if (arr.Length != 2)
838 return 1;
840 arr [0] = "ABC";
841 arr [1] = "DEF";
843 return 0;
846 public static int test_0_marshal_out_string_array_delegate () {
847 string[] arr = new string [] { "", "" };
848 return mono_test_marshal_out_string_array_delegate (arr, 2, new ArrayDelegate10 (array_delegate10));
852 * [In, Out] classes
855 public delegate int InOutByvalClassDelegate ([In, Out] SimpleClass ss);
857 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_byval_class_delegate")]
858 public static extern int mono_test_marshal_inout_byval_class_delegate (InOutByvalClassDelegate d);
860 public static int delegate_test_byval_class_inout (SimpleClass ss) {
861 if ((ss.a != false) || (ss.b != true) || (ss.c != false) || (ss.d != "FOO"))
862 return 1;
864 ss.a = true;
865 ss.b = false;
866 ss.c = true;
867 ss.d = "RES";
869 return 0;
872 public static int test_0_marshal_inout_byval_class_delegate () {
873 return mono_test_marshal_inout_byval_class_delegate (new InOutByvalClassDelegate (delegate_test_byval_class_inout));
877 * Returning unicode strings
879 [return: MarshalAs(UnmanagedType.LPWStr)]
880 public delegate string ReturnUnicodeStringDelegate([MarshalAs(UnmanagedType.LPWStr)] string message);
882 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_unicode_string_delegate")]
883 public static extern int mono_test_marshal_return_unicode_string_delegate (ReturnUnicodeStringDelegate d);
885 public static String return_unicode_string_delegate (string message) {
886 return message;
889 public static int test_0_marshal_return_unicode_string_delegate () {
890 return mono_test_marshal_return_unicode_string_delegate (new ReturnUnicodeStringDelegate (return_unicode_string_delegate));
894 * Returning string arrays
896 public delegate string[] ReturnArrayDelegate (int i);
898 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_string_array_delegate")]
899 public static extern int mono_test_marshal_return_string_array_delegate (ReturnArrayDelegate d);
901 public static String[] return_array_delegate (int i) {
902 String[] arr = new String [2];
904 arr [0] = "ABC";
905 arr [1] = "DEF";
907 return arr;
910 public static String[] return_array_delegate_null (int i) {
911 return null;
914 public static int test_0_marshal_return_string_array_delegate () {
915 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate));
918 public static int test_3_marshal_return_string_array_delegate_null () {
919 return mono_test_marshal_return_string_array_delegate (new ReturnArrayDelegate (return_array_delegate_null));