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 struct TinyStruct {
90 public TinyStruct (int i)
97 [StructLayout (LayoutKind.Sequential)]
98 public class SimpleClass {
109 [StructLayout (LayoutKind.Sequential)]
110 public class EmptyClass {
113 [StructLayout (LayoutKind.Sequential)]
114 public struct LongAlignStruct {
120 [StructLayout(LayoutKind.Sequential)]
121 public class BlittableClass
127 [StructLayout (LayoutKind.Sequential)]
134 [StructLayout(LayoutKind.Sequential)]
142 public AsAnyStruct(int i, int j, int k, String s) {
150 [StructLayout(LayoutKind.Sequential)]
158 public AsAnyClass(int i, int j, int k, String s) {
165 [DllImport ("libnot-found", EntryPoint="not_found")]
166 public static extern int mono_library_not_found ();
168 [DllImport ("libtest", EntryPoint="not_found")]
169 public static extern int mono_entry_point_not_found ();
171 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
172 public static extern int mono_test_marshal_char_2 (char a1);
174 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
175 public static extern int mono_test_marshal_char_3 (char a1);
177 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
178 public static extern int mono_test_marshal_char (char a1);
180 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
181 public static extern int mono_test_marshal_char_array (char[] a1);
183 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
184 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
186 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
187 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
189 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
190 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
192 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
193 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
195 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
196 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
198 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
199 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
201 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
202 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
204 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
205 public static extern int mono_test_marshal_array (int [] a1);
207 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
208 public static extern int mono_test_marshal_empty_string_array (string [] a1);
210 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
211 public static extern int mono_test_marshal_string_array (string [] a1);
213 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
214 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
216 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
217 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
219 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
220 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
222 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
223 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
225 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
226 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
228 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
229 public static extern int mono_test_marshal_struct (SimpleStruct ss);
231 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
232 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
234 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
235 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
237 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
238 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
240 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
241 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
243 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
244 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
246 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
247 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
249 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
250 public static extern int mono_test_marshal_point (Point p);
252 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
253 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
255 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
256 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
258 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
259 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
261 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
262 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
264 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
265 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
267 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
268 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
270 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
271 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
273 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
274 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
276 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
277 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
279 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
280 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
282 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
283 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
285 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
286 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
288 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
289 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
291 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder2")]
292 public static extern void mono_test_marshal_stringbuilder2 (StringBuilder sb, int len);
294 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
295 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
297 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
298 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
300 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
301 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
303 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
304 public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
306 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
307 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
309 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
310 public static extern void mono_test_last_error (int err);
312 [DllImport ("libtest", EntryPoint="mono_test_asany")]
313 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
315 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
316 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
318 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
319 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
321 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
322 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
323 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
324 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
326 [DllImport ("libtest")]
327 static extern int class_marshal_test0 (SimpleObj obj);
329 [DllImport ("libtest")]
330 static extern void class_marshal_test1 (out SimpleObj obj);
332 [DllImport ("libtest")]
333 static extern int class_marshal_test4 (SimpleObj obj);
335 [DllImport ("libtest")]
336 static extern int string_marshal_test0 (string str);
338 [DllImport ("libtest")]
339 static extern void string_marshal_test1 (out string str);
341 [DllImport ("libtest")]
342 static extern int string_marshal_test2 (ref string str);
344 [DllImport ("libtest")]
345 static extern int string_marshal_test3 (string str);
347 public delegate int SimpleDelegate (int a);
349 public static int Main (string[] args) {
350 return TestDriver.RunTests (typeof (Tests), args);
353 public static int test_0_marshal_char () {
354 return mono_test_marshal_char ('a');
357 public static int test_0_marshal_char_array () {
358 // a unicode char[] is implicitly marshalled as [Out]
359 char[] buf = new char [32];
360 mono_test_marshal_char_array (buf);
361 string s = new string (buf);
362 if (s.StartsWith ("abcdef"))
368 public static int test_1225_marshal_array () {
369 int [] a1 = new int [50];
370 for (int i = 0; i < 50; i++)
373 return mono_test_marshal_array (a1);
376 public static int test_1225_marshal_inout_array () {
377 int [] a1 = new int [50];
378 for (int i = 0; i < 50; i++)
381 int res = mono_test_marshal_inout_array (a1);
383 for (int i = 0; i < 50; i++)
384 if (a1 [i] != 50 - i) {
385 Console.WriteLine ("X: " + i + " " + a1 [i]);
392 public static int test_0_marshal_out_array () {
393 int [] a1 = new int [50];
395 int res = mono_test_marshal_out_array (a1, 0);
397 for (int i = 0; i < 50; i++)
399 Console.WriteLine ("X: " + i + " " + a1 [i]);
406 public static int test_0_marshal_inout_nonblittable_array () {
407 char [] a1 = new char [10];
408 for (int i = 0; i < 10; i++)
409 a1 [i] = "Hello, World" [i];
411 int res = mono_test_marshal_inout_nonblittable_array (a1);
413 for (int i = 0; i < 10; i++)
420 public static int test_0_marshal_struct () {
421 SimpleStruct ss = new SimpleStruct ();
425 return mono_test_marshal_struct (ss);
428 public static int test_0_marshal_struct_gen () {
429 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
433 return mono_test_marshal_struct_gen (ss);
436 public static int test_0_marshal_struct2 () {
437 SimpleStruct2 ss2 = new SimpleStruct2 ();
445 return mono_test_marshal_struct2 (ss2);
448 public static int test_0_marshal_struct3 () {
449 SimpleStruct2 ss2 = new SimpleStruct2 ();
457 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
460 public static int test_0_marshal_empty_struct () {
461 EmptyStruct es = new EmptyStruct ();
463 if (mono_test_empty_struct (1, es, 2) != 0)
469 public static int test_0_marshal_lpstruct () {
470 SimpleStruct ss = new SimpleStruct ();
474 return mono_test_marshal_lpstruct (ss);
477 public static int test_0_marshal_lpstruct_blittable () {
478 Point p = new Point ();
482 return mono_test_marshal_lpstruct_blittable (p);
485 public static int test_0_marshal_struct_array () {
486 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
488 SimpleStruct2 ss2 = new SimpleStruct2 ();
507 return mono_test_marshal_struct_array (ss_arr);
510 public static int test_105_marshal_long_align_struct_array () {
511 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
513 LongAlignStruct ss = new LongAlignStruct ();
526 return mono_test_marshal_long_align_struct_array (ss_arr);
529 /* Test classes as arguments and return values */
530 public static int test_0_marshal_class () {
531 SimpleClass ss = new SimpleClass ();
539 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
542 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
543 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
544 res.g == ss.g && res.h == ss.h))
547 /* Test null arguments and results */
548 res = mono_test_marshal_class (10, 11, 12, null, 14);
555 public static int test_0_marshal_byref_class () {
556 SimpleClass ss = new SimpleClass ();
564 int res = mono_test_marshal_byref_class (ref ss);
565 if (ss.d != "TEST-RES")
571 public static int test_0_marshal_delegate () {
572 SimpleDelegate d = new SimpleDelegate (delegate_test);
574 return mono_test_marshal_delegate (d);
577 public static int test_34_marshal_instance_delegate () {
578 Tests t = new Tests ();
580 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
582 return mono_test_marshal_delegate (d);
585 /* Static delegates closed over their first argument */
586 public static int closed_delegate (Tests t, int a) {
587 return t.int_field + a;
590 public static int test_34_marshal_closed_static_delegate () {
591 Tests t = new Tests ();
593 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
595 return mono_test_marshal_delegate (d);
598 public static int test_0_marshal_return_delegate () {
599 SimpleDelegate d = new SimpleDelegate (delegate_test);
601 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
606 public static int test_0_marshal_delegate_struct () {
607 DelegateStruct s = new DelegateStruct ();
610 s.del = new SimpleDelegate (delegate_test);
611 s.del2 = new SimpleDelegate (delegate_test);
614 DelegateStruct res = mono_test_marshal_delegate_struct (s);
618 if (res.del (2) != 0)
620 if (res.del2 (2) != 0)
622 if (res.del3 != null)
628 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
629 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
631 public static int test_3_marshal_out_delegate () {
632 SimpleDelegate d = null;
634 mono_test_marshal_out_delegate (out d);
639 public static int test_0_marshal_byref_struct () {
640 SimpleStruct s = new SimpleStruct ();
647 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
650 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
655 public static int test_0_marshal_byref_struct_in () {
656 SimpleStruct s = new SimpleStruct ();
663 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
666 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
671 public static int test_0_marshal_byref_struct_inout () {
672 SimpleStruct s = new SimpleStruct ();
679 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
682 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
687 public static int test_0_marshal_point () {
688 Point pt = new Point();
692 return mono_test_marshal_point(pt);
695 public static int test_0_marshal_mixed_point () {
696 MixedPoint mpt = new MixedPoint();
700 return mono_test_marshal_mixed_point(mpt);
703 public static int test_0_marshal_bool_byref () {
705 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
708 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
716 public static int test_0_marshal_bool_as_I1 () {
720 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
723 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
732 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
744 public static int test_0_marshal_bool_as_U1 () {
748 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
751 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
760 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
772 public static int test_0_return_vtype () {
773 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
775 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
781 public static int test_0_marshal_stringbuilder () {
782 StringBuilder sb = new StringBuilder(255);
784 mono_test_marshal_stringbuilder (sb, sb.Capacity);
785 String res = sb.ToString();
787 if (res != "This is my message. Isn't it nice?")
790 // Test that cached_str is cleared
791 mono_test_marshal_stringbuilder2 (sb, sb.Capacity);
796 // Test StringBuilder with default capacity (16)
797 StringBuilder sb2 = new StringBuilder();
798 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
799 if (sb2.ToString () != "This is my messa")
805 public static int test_0_marshal_stringbuilder_unicode () {
806 StringBuilder sb = new StringBuilder(255);
807 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
808 String res = sb.ToString();
810 if (res != "This is my message. Isn't it nice?")
813 // Test StringBuilder with default capacity (16)
814 StringBuilder sb2 = new StringBuilder();
815 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
816 if (sb2.ToString () != "This is my messa")
822 public static int test_0_marshal_stringbuilder_out () {
824 mono_test_marshal_stringbuilder_out (out sb);
826 if (sb.ToString () != "This is my message. Isn't it nice?")
831 public static int test_0_marshal_stringbuilder_out_unicode () {
833 mono_test_marshal_stringbuilder_out_unicode (out sb);
835 if (sb.ToString () != "This is my message. Isn't it nice?")
840 public static int test_0_marshal_stringbuilder_ref () {
841 StringBuilder sb = new StringBuilder ();
843 int res = mono_test_marshal_stringbuilder_ref (ref sb);
847 if (sb.ToString () != "This is my message. Isn't it nice?")
852 public static int test_0_marshal_empty_string_array () {
853 return mono_test_marshal_empty_string_array (null);
856 public static int test_0_marshal_string_array () {
857 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
860 public static int test_0_marshal_unicode_string_array () {
861 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
864 public static int test_0_marshal_stringbuilder_array () {
865 StringBuilder sb1 = new StringBuilder ("ABC");
866 StringBuilder sb2 = new StringBuilder ("DEF");
868 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
871 if (sb1.ToString () != "DEF")
873 if (sb2.ToString () != "ABC")
878 public static int test_0_last_error () {
879 mono_test_last_error (5);
880 if (Marshal.GetLastWin32Error () == 5)
886 public static int test_0_entry_point_not_found () {
889 mono_entry_point_not_found ();
892 catch (EntryPointNotFoundException) {
898 public static int test_0_library_not_found () {
901 mono_library_not_found ();
904 catch (DllNotFoundException) {
910 /* Check that the runtime trims .dll from the library name */
911 public static int test_0_trim_dll_from_name () {
913 mono_test_marshal_char_2 ('A');
918 /* Check that the runtime adds lib to to the library name */
919 public static int test_0_add_lib_to_name () {
921 mono_test_marshal_char_3 ('A');
930 public static int test_0_asany () {
931 if (mono_test_asany (5, 1) != 0)
934 if (mono_test_asany ("ABC", 2) != 0)
937 SimpleStruct2 ss2 = new SimpleStruct2 ();
945 if (mono_test_asany (ss2, 3) != 0)
948 if (mono_test_asany_unicode ("ABC", 4) != 0)
954 mono_test_asany (c, 0);
957 catch (ArgumentException) {
961 mono_test_asany (new Object (), 0);
964 catch (ArgumentException) {
970 /* AsAny marshalling + [In, Out] */
972 public static int test_0_asany_in () {
974 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
975 mono_test_asany_in (str);
978 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
979 mono_test_asany_in (cls);
980 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
984 object obj = new AsAnyStruct(1,2,3, "ABC");
985 mono_test_asany_in (obj);
986 str = (AsAnyStruct)obj;
987 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
993 public static int test_0_asany_out () {
995 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
996 mono_test_asany_out (str);
999 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1000 mono_test_asany_out (cls);
1001 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1005 object obj = new AsAnyStruct(1,2,3, "ABC");
1006 mono_test_asany_out (obj);
1007 str = (AsAnyStruct)obj;
1008 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1014 public static int test_0_asany_inout () {
1016 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1017 mono_test_asany_inout (str);
1020 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1021 mono_test_asany_inout (cls);
1022 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1026 object obj = new AsAnyStruct(1,2,3, "ABC");
1027 mono_test_asany_inout (obj);
1028 str = (AsAnyStruct)obj;
1029 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1035 /* Byref String Array */
1037 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1038 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1040 public static int test_0_byref_string_array () {
1042 string[] arr = null;
1044 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1047 arr = new string[] { "Alpha", "Beta", "Gamma" };
1049 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1052 /* FIXME: Test returned array and out case */
1058 * AMD64 small structs-by-value tests.
1061 /* TEST 1: 16 byte long INTEGER struct */
1063 [StructLayout(LayoutKind.Sequential)]
1064 public struct Amd64Struct1 {
1071 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1072 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1074 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1075 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);
1077 public static int test_0_amd64_struct1 () {
1078 Amd64Struct1 s = new Amd64Struct1 ();
1084 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1086 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1089 public static int test_0_amd64_struct1_many_args () {
1090 Amd64Struct1 s = new Amd64Struct1 ();
1096 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1098 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1101 /* TEST 2: 8 byte long INTEGER struct */
1103 [StructLayout(LayoutKind.Sequential)]
1104 public struct Amd64Struct2 {
1109 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1110 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1112 public static int test_0_amd64_struct2 () {
1113 Amd64Struct2 s = new Amd64Struct2 ();
1117 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1119 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1122 /* TEST 3: 4 byte long INTEGER struct */
1124 [StructLayout(LayoutKind.Sequential)]
1125 public struct Amd64Struct3 {
1129 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1130 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1132 public static int test_0_amd64_struct3 () {
1133 Amd64Struct3 s = new Amd64Struct3 ();
1136 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1138 return (s2.i == -4) ? 0 : 1;
1141 /* Test 4: 16 byte long FLOAT struct */
1143 [StructLayout(LayoutKind.Sequential)]
1144 public struct Amd64Struct4 {
1145 public double d1, d2;
1148 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1149 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1151 public static int test_0_amd64_struct4 () {
1152 Amd64Struct4 s = new Amd64Struct4 ();
1156 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1158 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1165 /* Test 5: Float HFA */
1167 [StructLayout(LayoutKind.Sequential)]
1168 public struct TestStruct5 {
1169 public float d1, d2;
1172 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1173 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1175 public static int test_0_ia64_struct5 () {
1176 TestStruct5 s = new TestStruct5 ();
1180 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1182 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1185 /* Test 6: Double HFA */
1187 [StructLayout(LayoutKind.Sequential)]
1188 public struct TestStruct6 {
1189 public double d1, d2;
1192 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1193 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1195 public static int test_0_ia64_struct6 () {
1196 TestStruct6 s = new TestStruct6 ();
1200 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1202 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1205 /* Blittable class */
1206 [DllImport("libtest")]
1207 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1209 public static int test_0_marshal_blittable_class () {
1210 BlittableClass v1 = new BlittableClass ();
1212 /* Since it is blittable, it looks like it is passed as in/out */
1213 BlittableClass v2 = TestBlittableClass (v1);
1215 if (v1.a != 2 || v1.b != 3)
1218 if (v2.a != 2 || v2.b != 3)
1222 BlittableClass v3 = TestBlittableClass (null);
1224 if (v3.a != 42 || v3.b != 43)
1231 * Generic structures
1234 [StructLayout(LayoutKind.Sequential)]
1235 public struct Amd64Struct1Gen<T> {
1242 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1243 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1245 public static int test_0_amd64_struct1_gen () {
1246 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1252 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1254 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1261 public static int test_0_marshal_byval_class () {
1262 SimpleObj obj0 = new SimpleObj ();
1266 if (class_marshal_test0 (obj0) != 0)
1272 public static int test_0_marshal_byval_class_null () {
1273 if (class_marshal_test4 (null) != 0)
1279 public static int test_0_marshal_out_class () {
1282 class_marshal_test1 (out obj1);
1284 if (obj1.str != "ABC")
1293 public static int test_0_marshal_string () {
1294 return string_marshal_test0 ("TEST0");
1297 public static int test_0_marshal_out_string () {
1300 string_marshal_test1 (out res);
1308 public static int test_0_marshal_byref_string () {
1309 string res = "TEST1";
1311 int r = string_marshal_test2 (ref res);
1319 public static int test_0_marshal_null_string () {
1320 return string_marshal_test3 (null);
1324 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_1", CallingConvention=CallingConvention.StdCall)]
1325 public static extern int mono_test_stdcall_mismatch_1 (int a, int b, int c);
1327 /* Test mismatched called conventions, the native function is cdecl */
1328 public static int test_0_stdcall_mismatch_1 () {
1329 mono_test_stdcall_mismatch_1 (0, 1, 2);
1333 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_2", CallingConvention=CallingConvention.Cdecl)]
1334 public static extern int mono_test_stdcall_mismatch_2 (int a, int b, int c);
1336 /* Test mismatched called conventions, the native function is stdcall */
1337 public static int test_0_stdcall_mismatch_2 () {
1338 mono_test_stdcall_mismatch_2 (0, 1, 2);
1343 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1344 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1346 public static int test_0_stdcall_name_mangling () {
1347 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1352 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1353 public static extern float mono_test_marshal_pass_return_float (float f);
1355 public static int test_0_pass_return_float () {
1356 float f = mono_test_marshal_pass_return_float (1.5f);
1358 return (f == 2.5f) ? 0 : 1;
1362 * Pointers to structures can not be passed
1365 /* This seems to be allowed by MS in some cases */
1367 public struct CharInfo {
1368 public char Character;
1369 public short Attributes;
1372 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1373 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1375 public static unsafe int test_0_marshal_ptr_to_struct () {
1376 CharInfo [] buffer = new CharInfo [1];
1377 fixed (CharInfo *ptr = &buffer [0]) {
1379 mono_test_marshal_ptr_to_struct (ptr);
1382 catch (MarshalDirectiveException) {
1391 * LPWStr marshalling
1394 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1395 [return: MarshalAs(UnmanagedType.LPWStr)]
1396 private static extern string mono_test_marshal_lpwstr_marshal(
1397 [MarshalAs(UnmanagedType.LPWStr)] string s,
1400 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1401 private static extern string mono_test_marshal_lpwstr_marshal2(
1405 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1406 private static extern void mono_test_marshal_lpwstr_out_marshal(
1407 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1409 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1410 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1413 public static int test_0_pass_return_lwpstr () {
1416 mono_test_marshal_lpwstr_out_marshal (out s);
1422 mono_test_marshal_lpwstr_out_marshal2 (out s);
1430 public static int test_0_out_lwpstr () {
1433 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1438 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1447 * Byref bool marshalling
1450 [DllImport("libtest")]
1451 extern static int marshal_test_ref_bool
1454 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1455 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1459 public static int test_0_pass_byref_bool () {
1460 for (int i = 0; i < 8; i++)
1462 bool b1 = (i & 4) != 0;
1463 bool b2 = (i & 2) != 0;
1464 bool b3 = (i & 1) != 0;
1465 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1466 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1480 * Bool struct field marshalling
1486 [MarshalAs(UnmanagedType.I1)] public bool b1;
1487 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1491 [DllImport("libtest")]
1492 extern static int marshal_test_bool_struct(ref BoolStruct s);
1494 public static int test_0_pass_bool_in_struct () {
1495 for (int i = 0; i < 8; i++)
1497 BoolStruct s = new BoolStruct();
1499 s.b1 = (i & 4) != 0;
1500 s.b2 = (i & 2) != 0;
1501 s.b3 = (i & 1) != 0;
1502 BoolStruct orig = s;
1503 if (marshal_test_bool_struct(ref s) != 0)
1505 if (s.b1 != !orig.b1)
1507 if (s.b2 != !orig.b2)
1509 if (s.b3 != !orig.b3)
1517 * Alignment of structs containing longs
1520 struct LongStruct2 {
1526 public LongStruct2 l;
1529 [DllImport("libtest")]
1530 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1532 public static int test_47_pass_long_struct () {
1533 LongStruct s = new LongStruct ();
1535 s.l = new LongStruct2 ();
1538 return mono_test_marshal_long_struct (ref s);
1542 * Invoking pinvoke methods through delegates
1545 delegate int MyDelegate (string name);
1547 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1548 public static extern int puts_static (string name);
1550 public static int test_0_invoke_pinvoke_through_delegate () {
1551 puts_static ("A simple Test for PInvoke 1");
1553 MyDelegate d = new MyDelegate (puts_static);
1554 d ("A simple Test for PInvoke 2");
1556 object [] args = {"A simple Test for PInvoke 3"};
1557 d.DynamicInvoke (args);
1563 * Missing virtual pinvoke methods
1568 public virtual object MyClone ()
1574 public class T2 : T {
1575 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1576 public override extern object MyClone ();
1579 public static int test_0_missing_virtual_pinvoke_method () {
1584 } catch (Exception ex) {
1592 * Marshalling of type 'object'
1595 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1596 public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1598 public static int test_0_marshal_object () {
1600 mono_test_marshal_object (0, 0, 0, null, 0);
1602 } catch (Exception) {
1608 * Marshalling of DateTime to OLE DATE (double)
1610 [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
1611 public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
1613 public static int test_0_marshal_date_time () {
1614 DateTime d = new DateTime (2009, 12, 6);
1616 double d3 = mono_test_marshal_date_time (d, out d2);
1625 * Calling pinvoke functions dynamically using calli
1628 [DllImport("libtest")]
1629 private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
1631 delegate void CalliDel (IntPtr a, int[] f);
1633 public static int test_0_calli_dynamic () {
1634 /* we need the cdecl version because the icall convention demands it under Windows */
1635 IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1637 DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
1639 var il = dm.GetILGenerator ();
1640 var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
1642 il.Emit (OpCodes.Ldarg, 1);
1643 signature.AddArgument (typeof (byte[]));
1645 il.Emit (OpCodes.Ldarg_0);
1647 il.Emit (OpCodes.Calli, signature);
1648 il.Emit (OpCodes.Ret);
1650 var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
1652 int[] arr = new int [1000];
1653 for (int i = 0; i < 50; ++i)
1656 if (arr.Length != 1000)
1658 for (int i = 0; i < 50; ++i)
1659 if (arr [i] != 50 - i)
1666 /*char array marshaling */
1667 [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
1668 public static extern int mono_test_marshal_ansi_char_array (char[] a1);
1670 public static int test_0_marshal_ansi_char_array () {
1671 char[] buf = new char [32];
1677 if (mono_test_marshal_ansi_char_array (buf) != 0)
1680 string s = new string (buf);
1681 if (s.StartsWith ("qwer"))
1687 /*char array marshaling */
1688 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
1689 public static extern int mono_test_marshal_unicode_char_array (char[] a1);
1691 public static int test_0_marshal_unicode_char_array () {
1692 char[] buf = new char [32];
1698 if (mono_test_marshal_unicode_char_array (buf) != 0)
1701 string s = new string (buf);
1702 if (s.StartsWith ("abcdef"))
1708 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
1709 public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
1711 public static int test_0_mono_test_marshal_lpstr () {
1714 if (mono_test_marshal_lpstr (str) != 0)
1720 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
1721 public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
1723 public static int test_0_mono_test_marshal_lpwstr () {
1726 if (mono_test_marshal_lpwstr (str) != 0)
1733 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
1734 [return: MarshalAs(UnmanagedType.LPStr)]
1735 public static extern string mono_test_marshal_return_lpstr ();
1737 public static int test_0_mono_test_marshal_return_lpstr () {
1738 string str = mono_test_marshal_return_lpstr ();
1745 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
1746 [return: MarshalAs(UnmanagedType.LPWStr)]
1747 public static extern string mono_test_marshal_return_lpwstr ();
1749 public static int test_0_mono_test_marshal_return_lpwstr () {
1750 string str = mono_test_marshal_return_lpwstr ();
1757 [DllImport ("libtest", EntryPoint="mono_test_has_thiscall")]
1758 public static extern int mono_test_has_thiscall ();
1760 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1761 public static extern int mono_test_native_thiscall (int a);
1763 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1764 public static extern int mono_test_native_thiscall (int a, int b);
1766 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1767 public static extern int mono_test_native_thiscall (int a, int b, int c);
1769 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1770 public static extern int mono_test_native_thiscall (TinyStruct a);
1772 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1773 public static extern int mono_test_native_thiscall (TinyStruct a, int b);
1775 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1776 public static extern int mono_test_native_thiscall (TinyStruct a, int b, int c);
1778 public static int test_0_native_thiscall ()
1780 if (mono_test_has_thiscall () == 0)
1783 if (mono_test_native_thiscall (1968329802) != 1968329802)
1786 if (mono_test_native_thiscall (268894549, 1212675791) != 1481570339)
1789 if (mono_test_native_thiscall (1288082683, -421187449, -1733670329) != -866775098)
1792 if (mono_test_native_thiscall (new TinyStruct(1968329802)) != 1968329802)
1795 if (mono_test_native_thiscall (new TinyStruct(268894549), 1212675791) != 1481570339)
1798 if (mono_test_native_thiscall (new TinyStruct(1288082683), -421187449, -1733670329) != -866775098)