2 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
7 using System.Runtime.InteropServices;
8 using System.Runtime.CompilerServices;
9 using System.Reflection.Emit;
15 public static int delegate_test (int a)
23 public int delegate_test_instance (int a)
28 [StructLayout (LayoutKind.Sequential)]
29 public struct SimpleStruct {
34 [MarshalAs(UnmanagedType.LPWStr)]
38 [StructLayout (LayoutKind.Sequential)]
39 public struct SimpleStructGen<T> {
44 [MarshalAs(UnmanagedType.LPWStr)]
48 [StructLayout (LayoutKind.Sequential)]
49 public struct SimpleStruct2 {
60 [StructLayout (LayoutKind.Sequential, Size=0)]
61 public struct EmptyStruct {
64 [StructLayout (LayoutKind.Sequential)]
65 public struct DelegateStruct {
67 public SimpleDelegate del;
68 [MarshalAs(UnmanagedType.FunctionPtr)]
69 public SimpleDelegate del2;
70 [MarshalAs(UnmanagedType.FunctionPtr)]
71 public SimpleDelegate del3;
74 /* sparcv9 has complex conventions when passing structs with doubles in them
75 by value, some simple tests for them */
76 [StructLayout (LayoutKind.Sequential)]
82 [StructLayout (LayoutKind.Sequential)]
83 public struct MixedPoint {
88 [StructLayout (LayoutKind.Sequential)]
89 public class SimpleClass {
100 [StructLayout (LayoutKind.Sequential)]
101 public class EmptyClass {
104 [StructLayout (LayoutKind.Sequential)]
105 public struct LongAlignStruct {
111 [StructLayout(LayoutKind.Sequential)]
112 public class BlittableClass
118 [StructLayout (LayoutKind.Sequential)]
125 [StructLayout(LayoutKind.Sequential)]
133 public AsAnyStruct(int i, int j, int k, String s) {
141 [StructLayout(LayoutKind.Sequential)]
149 public AsAnyClass(int i, int j, int k, String s) {
156 [DllImport ("libnot-found", EntryPoint="not_found")]
157 public static extern int mono_library_not_found ();
159 [DllImport ("libtest", EntryPoint="not_found")]
160 public static extern int mono_entry_point_not_found ();
162 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
163 public static extern int mono_test_marshal_char_2 (char a1);
165 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
166 public static extern int mono_test_marshal_char_3 (char a1);
168 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
169 public static extern int mono_test_marshal_char (char a1);
171 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
172 public static extern int mono_test_marshal_char_array (char[] a1);
174 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
175 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
177 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
178 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
180 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
181 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
183 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
184 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
186 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
187 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
189 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
190 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
192 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
193 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
195 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
196 public static extern int mono_test_marshal_array (int [] a1);
198 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
199 public static extern int mono_test_marshal_empty_string_array (string [] a1);
201 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
202 public static extern int mono_test_marshal_string_array (string [] a1);
204 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
205 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
207 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
208 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
210 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
211 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
213 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
214 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
216 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
217 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
219 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
220 public static extern int mono_test_marshal_struct (SimpleStruct ss);
222 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
223 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
225 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
226 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
228 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
229 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
231 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
232 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
234 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
235 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
237 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
238 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
240 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
241 public static extern int mono_test_marshal_point (Point p);
243 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
244 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
246 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
247 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
249 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
250 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
252 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
253 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
255 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
256 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
258 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
259 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
261 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
262 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
264 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
265 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
267 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
268 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
270 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
271 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
273 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
274 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
276 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
277 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
279 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
280 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
282 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
283 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
285 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
286 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
288 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
289 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
291 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
292 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
294 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
295 public static extern void mono_test_last_error (int err);
297 [DllImport ("libtest", EntryPoint="mono_test_asany")]
298 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
300 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
301 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
303 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
304 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
306 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
307 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
308 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
309 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
311 [DllImport ("libtest")]
312 static extern int class_marshal_test0 (SimpleObj obj);
314 [DllImport ("libtest")]
315 static extern void class_marshal_test1 (out SimpleObj obj);
317 [DllImport ("libtest")]
318 static extern int class_marshal_test4 (SimpleObj obj);
320 [DllImport ("libtest")]
321 static extern int string_marshal_test0 (string str);
323 [DllImport ("libtest")]
324 static extern void string_marshal_test1 (out string str);
326 [DllImport ("libtest")]
327 static extern int string_marshal_test2 (ref string str);
329 [DllImport ("libtest")]
330 static extern int string_marshal_test3 (string str);
332 public delegate int SimpleDelegate (int a);
334 public static int Main (string[] args) {
335 return TestDriver.RunTests (typeof (Tests), args);
338 public static int test_0_marshal_char () {
339 return mono_test_marshal_char ('a');
342 public static int test_0_marshal_char_array () {
343 // a unicode char[] is implicitly marshalled as [Out]
344 char[] buf = new char [32];
345 mono_test_marshal_char_array (buf);
346 string s = new string (buf);
347 if (s.StartsWith ("abcdef"))
353 public static int test_1225_marshal_array () {
354 int [] a1 = new int [50];
355 for (int i = 0; i < 50; i++)
358 return mono_test_marshal_array (a1);
361 public static int test_1225_marshal_inout_array () {
362 int [] a1 = new int [50];
363 for (int i = 0; i < 50; i++)
366 int res = mono_test_marshal_inout_array (a1);
368 for (int i = 0; i < 50; i++)
369 if (a1 [i] != 50 - i) {
370 Console.WriteLine ("X: " + i + " " + a1 [i]);
377 public static int test_0_marshal_out_array () {
378 int [] a1 = new int [50];
380 int res = mono_test_marshal_out_array (a1, 0);
382 for (int i = 0; i < 50; i++)
384 Console.WriteLine ("X: " + i + " " + a1 [i]);
391 public static int test_0_marshal_inout_nonblittable_array () {
392 char [] a1 = new char [10];
393 for (int i = 0; i < 10; i++)
394 a1 [i] = "Hello, World" [i];
396 int res = mono_test_marshal_inout_nonblittable_array (a1);
398 for (int i = 0; i < 10; i++)
405 public static int test_0_marshal_struct () {
406 SimpleStruct ss = new SimpleStruct ();
410 return mono_test_marshal_struct (ss);
413 public static int test_0_marshal_struct_gen () {
414 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
418 return mono_test_marshal_struct_gen (ss);
421 public static int test_0_marshal_struct2 () {
422 SimpleStruct2 ss2 = new SimpleStruct2 ();
430 return mono_test_marshal_struct2 (ss2);
433 public static int test_0_marshal_struct3 () {
434 SimpleStruct2 ss2 = new SimpleStruct2 ();
442 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
445 public static int test_0_marshal_empty_struct () {
446 EmptyStruct es = new EmptyStruct ();
448 if (mono_test_empty_struct (1, es, 2) != 0)
454 public static int test_0_marshal_lpstruct () {
455 SimpleStruct ss = new SimpleStruct ();
459 return mono_test_marshal_lpstruct (ss);
462 public static int test_0_marshal_lpstruct_blittable () {
463 Point p = new Point ();
467 return mono_test_marshal_lpstruct_blittable (p);
470 public static int test_0_marshal_struct_array () {
471 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
473 SimpleStruct2 ss2 = new SimpleStruct2 ();
492 return mono_test_marshal_struct_array (ss_arr);
495 public static int test_105_marshal_long_align_struct_array () {
496 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
498 LongAlignStruct ss = new LongAlignStruct ();
511 return mono_test_marshal_long_align_struct_array (ss_arr);
514 /* Test classes as arguments and return values */
515 public static int test_0_marshal_class () {
516 SimpleClass ss = new SimpleClass ();
524 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
527 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
528 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
529 res.g == ss.g && res.h == ss.h))
532 /* Test null arguments and results */
533 res = mono_test_marshal_class (10, 11, 12, null, 14);
540 public static int test_0_marshal_byref_class () {
541 SimpleClass ss = new SimpleClass ();
549 int res = mono_test_marshal_byref_class (ref ss);
550 if (ss.d != "TEST-RES")
556 public static int test_0_marshal_delegate () {
557 SimpleDelegate d = new SimpleDelegate (delegate_test);
559 return mono_test_marshal_delegate (d);
562 public static int test_34_marshal_instance_delegate () {
563 Tests t = new Tests ();
565 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
567 return mono_test_marshal_delegate (d);
570 /* Static delegates closed over their first argument */
571 public static int closed_delegate (Tests t, int a) {
572 return t.int_field + a;
575 public static int test_34_marshal_closed_static_delegate () {
576 Tests t = new Tests ();
578 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
580 return mono_test_marshal_delegate (d);
583 public static int test_0_marshal_return_delegate () {
584 SimpleDelegate d = new SimpleDelegate (delegate_test);
586 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
591 public static int test_0_marshal_delegate_struct () {
592 DelegateStruct s = new DelegateStruct ();
595 s.del = new SimpleDelegate (delegate_test);
596 s.del2 = new SimpleDelegate (delegate_test);
599 DelegateStruct res = mono_test_marshal_delegate_struct (s);
603 if (res.del (2) != 0)
605 if (res.del2 (2) != 0)
607 if (res.del3 != null)
613 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
614 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
616 public static int test_3_marshal_out_delegate () {
617 SimpleDelegate d = null;
619 mono_test_marshal_out_delegate (out d);
624 public static int test_0_marshal_byref_struct () {
625 SimpleStruct s = new SimpleStruct ();
632 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
635 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
640 public static int test_0_marshal_byref_struct_in () {
641 SimpleStruct s = new SimpleStruct ();
648 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
651 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
656 public static int test_0_marshal_byref_struct_inout () {
657 SimpleStruct s = new SimpleStruct ();
664 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
667 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
672 public static int test_0_marshal_point () {
673 Point pt = new Point();
677 return mono_test_marshal_point(pt);
680 public static int test_0_marshal_mixed_point () {
681 MixedPoint mpt = new MixedPoint();
685 return mono_test_marshal_mixed_point(mpt);
688 public static int test_0_marshal_bool_byref () {
690 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
693 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
701 public static int test_0_marshal_bool_as_I1 () {
705 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
708 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
717 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
729 public static int test_0_marshal_bool_as_U1 () {
733 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
736 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
745 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
757 public static int test_0_return_vtype () {
758 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
760 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
766 public static int test_0_marshal_stringbuilder () {
767 StringBuilder sb = new StringBuilder(255);
769 mono_test_marshal_stringbuilder (sb, sb.Capacity);
770 String res = sb.ToString();
772 if (res != "This is my message. Isn't it nice?")
775 // Test StringBuilder with default capacity (16)
776 StringBuilder sb2 = new StringBuilder();
777 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
778 if (sb2.ToString () != "This is my messa")
784 public static int test_0_marshal_stringbuilder_unicode () {
785 StringBuilder sb = new StringBuilder(255);
786 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
787 String res = sb.ToString();
789 if (res != "This is my message. Isn't it nice?")
792 // Test StringBuilder with default capacity (16)
793 StringBuilder sb2 = new StringBuilder();
794 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
795 if (sb2.ToString () != "This is my messa")
801 public static int test_0_marshal_stringbuilder_out () {
803 mono_test_marshal_stringbuilder_out (out sb);
805 if (sb.ToString () != "This is my message. Isn't it nice?")
810 public static int test_0_marshal_stringbuilder_out_unicode () {
812 mono_test_marshal_stringbuilder_out_unicode (out sb);
814 if (sb.ToString () != "This is my message. Isn't it nice?")
819 public static int test_0_marshal_empty_string_array () {
820 return mono_test_marshal_empty_string_array (null);
823 public static int test_0_marshal_string_array () {
824 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
827 public static int test_0_marshal_unicode_string_array () {
828 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
831 public static int test_0_marshal_stringbuilder_array () {
832 StringBuilder sb1 = new StringBuilder ("ABC");
833 StringBuilder sb2 = new StringBuilder ("DEF");
835 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
838 if (sb1.ToString () != "DEF")
840 if (sb2.ToString () != "ABC")
845 public static int test_0_last_error () {
846 mono_test_last_error (5);
847 if (Marshal.GetLastWin32Error () == 5)
853 public static int test_0_entry_point_not_found () {
856 mono_entry_point_not_found ();
859 catch (EntryPointNotFoundException) {
865 public static int test_0_library_not_found () {
868 mono_library_not_found ();
871 catch (DllNotFoundException) {
877 /* Check that the runtime trims .dll from the library name */
878 public static int test_0_trim_dll_from_name () {
880 mono_test_marshal_char_2 ('A');
885 /* Check that the runtime adds lib to to the library name */
886 public static int test_0_add_lib_to_name () {
888 mono_test_marshal_char_3 ('A');
897 public static int test_0_asany () {
898 if (mono_test_asany (5, 1) != 0)
901 if (mono_test_asany ("ABC", 2) != 0)
904 SimpleStruct2 ss2 = new SimpleStruct2 ();
912 if (mono_test_asany (ss2, 3) != 0)
915 if (mono_test_asany_unicode ("ABC", 4) != 0)
921 mono_test_asany (c, 0);
924 catch (ArgumentException) {
928 mono_test_asany (new Object (), 0);
931 catch (ArgumentException) {
937 /* AsAny marshalling + [In, Out] */
939 public static int test_0_asany_in () {
941 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
942 mono_test_asany_in (str);
945 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
946 mono_test_asany_in (cls);
947 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
951 object obj = new AsAnyStruct(1,2,3, "ABC");
952 mono_test_asany_in (obj);
953 str = (AsAnyStruct)obj;
954 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
960 public static int test_0_asany_out () {
962 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
963 mono_test_asany_out (str);
966 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
967 mono_test_asany_out (cls);
968 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
972 object obj = new AsAnyStruct(1,2,3, "ABC");
973 mono_test_asany_out (obj);
974 str = (AsAnyStruct)obj;
975 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
981 public static int test_0_asany_inout () {
983 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
984 mono_test_asany_inout (str);
987 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
988 mono_test_asany_inout (cls);
989 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
993 object obj = new AsAnyStruct(1,2,3, "ABC");
994 mono_test_asany_inout (obj);
995 str = (AsAnyStruct)obj;
996 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1002 /* Byref String Array */
1004 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1005 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1007 public static int test_0_byref_string_array () {
1009 string[] arr = null;
1011 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1014 arr = new string[] { "Alpha", "Beta", "Gamma" };
1016 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1019 /* FIXME: Test returned array and out case */
1025 * AMD64 small structs-by-value tests.
1028 /* TEST 1: 16 byte long INTEGER struct */
1030 [StructLayout(LayoutKind.Sequential)]
1031 public struct Amd64Struct1 {
1038 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1039 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1041 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1042 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1_many_args (Amd64Struct1 s, int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8);
1044 public static int test_0_amd64_struct1 () {
1045 Amd64Struct1 s = new Amd64Struct1 ();
1051 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1053 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1056 public static int test_0_amd64_struct1_many_args () {
1057 Amd64Struct1 s = new Amd64Struct1 ();
1063 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1065 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1068 /* TEST 2: 8 byte long INTEGER struct */
1070 [StructLayout(LayoutKind.Sequential)]
1071 public struct Amd64Struct2 {
1076 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1077 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1079 public static int test_0_amd64_struct2 () {
1080 Amd64Struct2 s = new Amd64Struct2 ();
1084 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1086 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1089 /* TEST 3: 4 byte long INTEGER struct */
1091 [StructLayout(LayoutKind.Sequential)]
1092 public struct Amd64Struct3 {
1096 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1097 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1099 public static int test_0_amd64_struct3 () {
1100 Amd64Struct3 s = new Amd64Struct3 ();
1103 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1105 return (s2.i == -4) ? 0 : 1;
1108 /* Test 4: 16 byte long FLOAT struct */
1110 [StructLayout(LayoutKind.Sequential)]
1111 public struct Amd64Struct4 {
1112 public double d1, d2;
1115 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1116 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1118 public static int test_0_amd64_struct4 () {
1119 Amd64Struct4 s = new Amd64Struct4 ();
1123 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1125 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1132 /* Test 5: Float HFA */
1134 [StructLayout(LayoutKind.Sequential)]
1135 public struct TestStruct5 {
1136 public float d1, d2;
1139 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1140 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1142 public static int test_0_ia64_struct5 () {
1143 TestStruct5 s = new TestStruct5 ();
1147 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1149 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1152 /* Test 6: Double HFA */
1154 [StructLayout(LayoutKind.Sequential)]
1155 public struct TestStruct6 {
1156 public double d1, d2;
1159 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1160 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1162 public static int test_0_ia64_struct6 () {
1163 TestStruct6 s = new TestStruct6 ();
1167 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1169 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1172 /* Blittable class */
1173 [DllImport("libtest")]
1174 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1176 public static int test_0_marshal_blittable_class () {
1177 BlittableClass v1 = new BlittableClass ();
1179 /* Since it is blittable, it looks like it is passed as in/out */
1180 BlittableClass v2 = TestBlittableClass (v1);
1182 if (v1.a != 2 || v1.b != 3)
1185 if (v2.a != 2 || v2.b != 3)
1189 BlittableClass v3 = TestBlittableClass (null);
1191 if (v3.a != 42 || v3.b != 43)
1198 * Generic structures
1201 [StructLayout(LayoutKind.Sequential)]
1202 public struct Amd64Struct1Gen<T> {
1209 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1210 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1212 public static int test_0_amd64_struct1_gen () {
1213 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1219 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1221 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1228 public static int test_0_marshal_byval_class () {
1229 SimpleObj obj0 = new SimpleObj ();
1233 if (class_marshal_test0 (obj0) != 0)
1239 public static int test_0_marshal_byval_class_null () {
1240 if (class_marshal_test4 (null) != 0)
1246 public static int test_0_marshal_out_class () {
1249 class_marshal_test1 (out obj1);
1251 if (obj1.str != "ABC")
1260 public static int test_0_marshal_string () {
1261 return string_marshal_test0 ("TEST0");
1264 public static int test_0_marshal_out_string () {
1267 string_marshal_test1 (out res);
1275 public static int test_0_marshal_byref_string () {
1276 string res = "TEST1";
1278 return string_marshal_test2 (ref res);
1281 public static int test_0_marshal_null_string () {
1282 return string_marshal_test3 (null);
1285 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1286 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1288 public static int test_0_stdcall_name_mangling () {
1289 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1294 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1295 public static extern float mono_test_marshal_pass_return_float (float f);
1297 public static int test_0_pass_return_float () {
1298 float f = mono_test_marshal_pass_return_float (1.5f);
1300 return (f == 2.5f) ? 0 : 1;
1304 * Pointers to structures can not be passed
1307 /* This seems to be allowed by MS in some cases */
1309 public struct CharInfo {
1310 public char Character;
1311 public short Attributes;
1314 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1315 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1317 public static unsafe int test_0_marshal_ptr_to_struct () {
1318 CharInfo [] buffer = new CharInfo [1];
1319 fixed (CharInfo *ptr = &buffer [0]) {
1321 mono_test_marshal_ptr_to_struct (ptr);
1324 catch (MarshalDirectiveException) {
1333 * LPWStr marshalling
1336 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1337 [return: MarshalAs(UnmanagedType.LPWStr)]
1338 private static extern string mono_test_marshal_lpwstr_marshal(
1339 [MarshalAs(UnmanagedType.LPWStr)] string s,
1342 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1343 private static extern string mono_test_marshal_lpwstr_marshal2(
1347 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1348 private static extern void mono_test_marshal_lpwstr_out_marshal(
1349 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1351 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1352 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1355 public static int test_0_pass_return_lwpstr () {
1358 mono_test_marshal_lpwstr_out_marshal (out s);
1364 mono_test_marshal_lpwstr_out_marshal2 (out s);
1372 public static int test_0_out_lwpstr () {
1375 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1380 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1389 * Byref bool marshalling
1392 [DllImport("libtest")]
1393 extern static int marshal_test_ref_bool
1396 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1397 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1401 public static int test_0_pass_byref_bool () {
1402 for (int i = 0; i < 8; i++)
1404 bool b1 = (i & 4) != 0;
1405 bool b2 = (i & 2) != 0;
1406 bool b3 = (i & 1) != 0;
1407 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1408 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1422 * Bool struct field marshalling
1428 [MarshalAs(UnmanagedType.I1)] public bool b1;
1429 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1433 [DllImport("libtest")]
1434 extern static int marshal_test_bool_struct(ref BoolStruct s);
1436 public static int test_0_pass_bool_in_struct () {
1437 for (int i = 0; i < 8; i++)
1439 BoolStruct s = new BoolStruct();
1441 s.b1 = (i & 4) != 0;
1442 s.b2 = (i & 2) != 0;
1443 s.b3 = (i & 1) != 0;
1444 BoolStruct orig = s;
1445 if (marshal_test_bool_struct(ref s) != 0)
1447 if (s.b1 != !orig.b1)
1449 if (s.b2 != !orig.b2)
1451 if (s.b3 != !orig.b3)
1459 * Alignment of structs containing longs
1462 struct LongStruct2 {
1468 public LongStruct2 l;
1471 [DllImport("libtest")]
1472 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1474 public static int test_47_pass_long_struct () {
1475 LongStruct s = new LongStruct ();
1477 s.l = new LongStruct2 ();
1480 return mono_test_marshal_long_struct (ref s);
1484 * Invoking pinvoke methods through delegates
1487 delegate int MyDelegate (string name);
1489 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1490 public static extern int puts_static (string name);
1492 public static int test_0_invoke_pinvoke_through_delegate () {
1493 puts_static ("A simple Test for PInvoke 1");
1495 MyDelegate d = new MyDelegate (puts_static);
1496 d ("A simple Test for PInvoke 2");
1498 object [] args = {"A simple Test for PInvoke 3"};
1499 d.DynamicInvoke (args);
1505 * Missing virtual pinvoke methods
1510 public virtual object MyClone ()
1516 public class T2 : T {
1517 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1518 public override extern object MyClone ();
1521 public static int test_0_missing_virtual_pinvoke_method () {
1526 } catch (Exception ex) {
1534 * Marshalling of type 'object'
1537 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1538 public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1540 public static int test_0_marshal_object () {
1542 mono_test_marshal_object (0, 0, 0, null, 0);
1544 } catch (Exception) {
1550 * Marshalling of DateTime to OLE DATE (double)
1552 [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
1553 public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
1555 public static int test_0_marshal_date_time () {
1556 DateTime d = new DateTime (2009, 12, 6);
1558 double d3 = mono_test_marshal_date_time (d, out d2);
1567 * Calling pinvoke functions dynamically using calli
1570 [DllImport("libtest")]
1571 private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
1573 delegate void CalliDel (IntPtr a, int[] f);
1575 public static int test_0_calli_dynamic () {
1576 /* we need the cdecl version because the icall convention demands it under Windows */
1577 IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1579 DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
1581 var il = dm.GetILGenerator ();
1582 var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
1584 il.Emit (OpCodes.Ldarg, 1);
1585 signature.AddArgument (typeof (byte[]));
1587 il.Emit (OpCodes.Ldarg_0);
1589 il.Emit (OpCodes.Calli, signature);
1590 il.Emit (OpCodes.Ret);
1592 var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
1594 int[] arr = new int [1000];
1595 for (int i = 0; i < 50; ++i)
1598 if (arr.Length != 1000)
1600 for (int i = 0; i < 50; ++i)
1601 if (arr [i] != 50 - i)
1608 /*char array marshaling */
1609 [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
1610 public static extern int mono_test_marshal_ansi_char_array (char[] a1);
1612 public static int test_0_marshal_ansi_char_array () {
1613 char[] buf = new char [32];
1619 if (mono_test_marshal_ansi_char_array (buf) != 0)
1622 string s = new string (buf);
1623 if (s.StartsWith ("qwer"))
1629 /*char array marshaling */
1630 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
1631 public static extern int mono_test_marshal_unicode_char_array (char[] a1);
1633 public static int test_0_marshal_unicode_char_array () {
1634 char[] buf = new char [32];
1640 if (mono_test_marshal_unicode_char_array (buf) != 0)
1643 string s = new string (buf);
1644 if (s.StartsWith ("abcdef"))
1650 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
1651 public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
1653 public static int test_0_mono_test_marshal_lpstr () {
1656 if (mono_test_marshal_lpstr (str) != 0)
1662 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
1663 public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
1665 public static int test_0_mono_test_marshal_lpwstr () {
1668 if (mono_test_marshal_lpwstr (str) != 0)
1675 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
1676 [return: MarshalAs(UnmanagedType.LPStr)]
1677 public static extern string mono_test_marshal_return_lpstr ();
1679 public static int test_0_mono_test_marshal_return_lpstr () {
1680 string str = mono_test_marshal_return_lpstr ();
1687 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
1688 [return: MarshalAs(UnmanagedType.LPWStr)]
1689 public static extern string mono_test_marshal_return_lpwstr ();
1691 public static int test_0_mono_test_marshal_return_lpwstr () {
1692 string str = mono_test_marshal_return_lpwstr ();