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 [StructLayout (LayoutKind.Sequential)]
75 public struct SingleDoubleStruct {
79 /* sparcv9 has complex conventions when passing structs with doubles in them
80 by value, some simple tests for them */
81 [StructLayout (LayoutKind.Sequential)]
87 [StructLayout (LayoutKind.Sequential)]
88 public struct MixedPoint {
93 [StructLayout (LayoutKind.Sequential)]
94 public struct TinyStruct {
95 public TinyStruct (int i)
102 [StructLayout (LayoutKind.Sequential)]
103 public class SimpleClass {
114 [StructLayout (LayoutKind.Sequential)]
115 public class EmptyClass {
118 [StructLayout (LayoutKind.Sequential)]
119 public struct LongAlignStruct {
125 [StructLayout(LayoutKind.Sequential)]
126 public class BlittableClass
132 [StructLayout (LayoutKind.Sequential)]
139 [StructLayout(LayoutKind.Sequential)]
147 public AsAnyStruct(int i, int j, int k, String s) {
155 [StructLayout(LayoutKind.Sequential)]
163 public AsAnyClass(int i, int j, int k, String s) {
170 [DllImport ("libnot-found", EntryPoint="not_found")]
171 public static extern int mono_library_not_found ();
173 [DllImport ("libtest", EntryPoint="not_found")]
174 public static extern int mono_entry_point_not_found ();
176 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
177 public static extern int mono_test_marshal_char_2 (char a1);
179 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
180 public static extern int mono_test_marshal_char_3 (char a1);
182 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
183 public static extern int mono_test_marshal_char (char a1);
185 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
186 public static extern int mono_test_marshal_char_array (char[] a1);
188 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
189 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
191 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
192 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
194 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
195 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
197 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
198 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
200 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
201 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
203 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
204 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
206 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
207 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
209 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
210 public static extern int mono_test_marshal_array (int [] a1);
212 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
213 public static extern int mono_test_marshal_empty_string_array (string [] a1);
215 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
216 public static extern int mono_test_marshal_string_array (string [] a1);
218 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
219 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
221 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
222 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
224 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
225 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
227 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
228 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
230 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_byref_array_out_size_param")]
231 public static extern int mono_test_marshal_out_byref_array_out_size_param ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out int [] a1, out int n);
233 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
234 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
236 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
237 public static extern int mono_test_marshal_struct (SimpleStruct ss);
239 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
240 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
242 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
243 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
245 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
246 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
248 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
249 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
251 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
252 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
254 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
255 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
257 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
258 public static extern int mono_test_marshal_point (Point p);
260 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
261 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
263 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
264 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
266 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
267 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
269 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
270 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
272 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
273 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
275 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
276 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
278 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
279 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
281 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
282 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
284 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
285 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
287 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
288 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
290 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
291 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
293 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
294 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
296 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
297 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
299 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder2")]
300 public static extern void mono_test_marshal_stringbuilder2 (StringBuilder sb, int len);
302 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
303 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
305 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
306 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
308 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
309 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
311 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
312 public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
314 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
315 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
317 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
318 public static extern void mono_test_last_error (int err);
320 [DllImport ("libtest", EntryPoint="mono_test_asany")]
321 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
323 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
324 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
326 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
327 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
329 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
330 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
331 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
332 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
334 [DllImport ("libtest")]
335 static extern int class_marshal_test0 (SimpleObj obj);
337 [DllImport ("libtest")]
338 static extern void class_marshal_test1 (out SimpleObj obj);
340 [DllImport ("libtest")]
341 static extern int class_marshal_test4 (SimpleObj obj);
343 [DllImport ("libtest")]
344 static extern int string_marshal_test0 (string str);
346 [DllImport ("libtest")]
347 static extern void string_marshal_test1 (out string str);
349 [DllImport ("libtest")]
350 static extern int string_marshal_test2 (ref string str);
352 [DllImport ("libtest")]
353 static extern int string_marshal_test3 (string str);
355 public delegate int SimpleDelegate (int a);
357 public static int Main (string[] args) {
358 return TestDriver.RunTests (typeof (Tests), args);
361 public static int test_0_marshal_char () {
362 return mono_test_marshal_char ('a');
365 public static int test_0_marshal_char_array () {
366 // a unicode char[] is implicitly marshalled as [Out]
367 char[] buf = new char [32];
368 mono_test_marshal_char_array (buf);
369 string s = new string (buf);
370 if (s.StartsWith ("abcdef"))
376 public static int test_1225_marshal_array () {
377 int [] a1 = new int [50];
378 for (int i = 0; i < 50; i++)
381 return mono_test_marshal_array (a1);
384 public static int test_1225_marshal_inout_array () {
385 int [] a1 = new int [50];
386 for (int i = 0; i < 50; i++)
389 int res = mono_test_marshal_inout_array (a1);
391 for (int i = 0; i < 50; i++)
392 if (a1 [i] != 50 - i) {
393 Console.WriteLine ("X: " + i + " " + a1 [i]);
400 public static int test_0_marshal_out_array () {
401 int [] a1 = new int [50];
403 int res = mono_test_marshal_out_array (a1, 0);
405 for (int i = 0; i < 50; i++)
407 Console.WriteLine ("X: " + i + " " + a1 [i]);
414 public static int test_0_marshal_out_byref_array_out_size_param () {
418 int res = mono_test_marshal_out_byref_array_out_size_param (out a1, out len);
421 for (int i = 0; i < len; i++)
427 public static int test_0_marshal_inout_nonblittable_array () {
428 char [] a1 = new char [10];
429 for (int i = 0; i < 10; i++)
430 a1 [i] = "Hello, World" [i];
432 int res = mono_test_marshal_inout_nonblittable_array (a1);
434 for (int i = 0; i < 10; i++)
441 public static int test_0_marshal_struct () {
442 SimpleStruct ss = new SimpleStruct ();
446 return mono_test_marshal_struct (ss);
449 public static int test_0_marshal_struct_gen () {
450 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
454 return mono_test_marshal_struct_gen (ss);
457 public static int test_0_marshal_struct2 () {
458 SimpleStruct2 ss2 = new SimpleStruct2 ();
466 return mono_test_marshal_struct2 (ss2);
469 public static int test_0_marshal_struct3 () {
470 SimpleStruct2 ss2 = new SimpleStruct2 ();
478 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
481 public static int test_0_marshal_empty_struct () {
482 EmptyStruct es = new EmptyStruct ();
484 if (mono_test_empty_struct (1, es, 2) != 0)
490 public static int test_0_marshal_lpstruct () {
491 SimpleStruct ss = new SimpleStruct ();
495 return mono_test_marshal_lpstruct (ss);
498 public static int test_0_marshal_lpstruct_blittable () {
499 Point p = new Point ();
503 return mono_test_marshal_lpstruct_blittable (p);
506 public static int test_0_marshal_struct_array () {
507 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
509 SimpleStruct2 ss2 = new SimpleStruct2 ();
528 return mono_test_marshal_struct_array (ss_arr);
531 public static int test_105_marshal_long_align_struct_array () {
532 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
534 LongAlignStruct ss = new LongAlignStruct ();
547 return mono_test_marshal_long_align_struct_array (ss_arr);
550 /* Test classes as arguments and return values */
551 public static int test_0_marshal_class () {
552 SimpleClass ss = new SimpleClass ();
560 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
563 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
564 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
565 res.g == ss.g && res.h == ss.h))
568 /* Test null arguments and results */
569 res = mono_test_marshal_class (10, 11, 12, null, 14);
576 public static int test_0_marshal_byref_class () {
577 SimpleClass ss = new SimpleClass ();
585 int res = mono_test_marshal_byref_class (ref ss);
586 if (ss.d != "TEST-RES")
592 public static int test_0_marshal_delegate () {
593 SimpleDelegate d = new SimpleDelegate (delegate_test);
595 return mono_test_marshal_delegate (d);
598 public static int test_34_marshal_instance_delegate () {
599 Tests t = new Tests ();
601 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
603 return mono_test_marshal_delegate (d);
606 /* Static delegates closed over their first argument */
607 public static int closed_delegate (Tests t, int a) {
608 return t.int_field + a;
611 public static int test_34_marshal_closed_static_delegate () {
612 Tests t = new Tests ();
614 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
616 return mono_test_marshal_delegate (d);
619 public static int test_0_marshal_return_delegate () {
620 SimpleDelegate d = new SimpleDelegate (delegate_test);
622 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
627 public static int test_0_marshal_delegate_struct () {
628 DelegateStruct s = new DelegateStruct ();
631 s.del = new SimpleDelegate (delegate_test);
632 s.del2 = new SimpleDelegate (delegate_test);
635 DelegateStruct res = mono_test_marshal_delegate_struct (s);
639 if (res.del (2) != 0)
641 if (res.del2 (2) != 0)
643 if (res.del3 != null)
649 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
650 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
652 public static int test_3_marshal_out_delegate () {
653 SimpleDelegate d = null;
655 mono_test_marshal_out_delegate (out d);
660 public static int test_0_marshal_byref_struct () {
661 SimpleStruct s = new SimpleStruct ();
668 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
671 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
676 public static int test_0_marshal_byref_struct_in () {
677 SimpleStruct s = new SimpleStruct ();
684 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
687 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
692 public static int test_0_marshal_byref_struct_inout () {
693 SimpleStruct s = new SimpleStruct ();
700 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
703 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
708 public static int test_0_marshal_point () {
709 Point pt = new Point();
713 return mono_test_marshal_point(pt);
716 public static int test_0_marshal_mixed_point () {
717 MixedPoint mpt = new MixedPoint();
721 return mono_test_marshal_mixed_point(mpt);
724 public static int test_0_marshal_bool_byref () {
726 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
729 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
737 public static int test_0_marshal_bool_as_I1 () {
741 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
744 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
753 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
765 public static int test_0_marshal_bool_as_U1 () {
769 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
772 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
781 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
793 public static int test_0_return_vtype () {
794 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
796 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
802 public static int test_0_marshal_stringbuilder () {
803 StringBuilder sb = new StringBuilder(255);
805 mono_test_marshal_stringbuilder (sb, sb.Capacity);
806 String res = sb.ToString();
808 if (res != "This is my message. Isn't it nice?")
811 // Test that cached_str is cleared
812 mono_test_marshal_stringbuilder2 (sb, sb.Capacity);
817 // Test StringBuilder with default capacity (16)
818 StringBuilder sb2 = new StringBuilder();
819 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
820 if (sb2.ToString () != "This is my messa")
826 public static int test_0_marshal_stringbuilder_unicode () {
827 StringBuilder sb = new StringBuilder(255);
828 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
829 String res = sb.ToString();
831 if (res != "This is my message. Isn't it nice?")
834 // Test StringBuilder with default capacity (16)
835 StringBuilder sb2 = new StringBuilder();
836 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
837 if (sb2.ToString () != "This is my messa")
843 public static int test_0_marshal_stringbuilder_out () {
845 mono_test_marshal_stringbuilder_out (out sb);
847 if (sb.ToString () != "This is my message. Isn't it nice?")
852 public static int test_0_marshal_stringbuilder_out_unicode () {
854 mono_test_marshal_stringbuilder_out_unicode (out sb);
856 if (sb.ToString () != "This is my message. Isn't it nice?")
861 public static int test_0_marshal_stringbuilder_ref () {
862 StringBuilder sb = new StringBuilder ();
864 int res = mono_test_marshal_stringbuilder_ref (ref sb);
868 if (sb.ToString () != "This is my message. Isn't it nice?")
873 public static int test_0_marshal_empty_string_array () {
874 return mono_test_marshal_empty_string_array (null);
877 public static int test_0_marshal_string_array () {
878 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
881 public static int test_0_marshal_unicode_string_array () {
882 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
885 public static int test_0_marshal_stringbuilder_array () {
886 StringBuilder sb1 = new StringBuilder ("ABC");
887 StringBuilder sb2 = new StringBuilder ("DEF");
889 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
892 if (sb1.ToString () != "DEF")
894 if (sb2.ToString () != "ABC")
899 public static int test_0_last_error () {
900 mono_test_last_error (5);
901 if (Marshal.GetLastWin32Error () == 5)
907 public static int test_0_entry_point_not_found () {
910 mono_entry_point_not_found ();
913 catch (EntryPointNotFoundException) {
919 public static int test_0_library_not_found () {
922 mono_library_not_found ();
925 catch (DllNotFoundException) {
931 /* Check that the runtime trims .dll from the library name */
932 public static int test_0_trim_dll_from_name () {
934 mono_test_marshal_char_2 ('A');
939 /* Check that the runtime adds lib to to the library name */
940 public static int test_0_add_lib_to_name () {
942 mono_test_marshal_char_3 ('A');
951 public static int test_0_asany () {
952 if (mono_test_asany (5, 1) != 0)
955 if (mono_test_asany ("ABC", 2) != 0)
958 SimpleStruct2 ss2 = new SimpleStruct2 ();
966 if (mono_test_asany (ss2, 3) != 0)
969 if (mono_test_asany_unicode ("ABC", 4) != 0)
975 mono_test_asany (c, 0);
978 catch (ArgumentException) {
982 mono_test_asany (new Object (), 0);
985 catch (ArgumentException) {
991 /* AsAny marshalling + [In, Out] */
993 public static int test_0_asany_in () {
995 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
996 mono_test_asany_in (str);
999 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1000 mono_test_asany_in (cls);
1001 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
1005 object obj = new AsAnyStruct(1,2,3, "ABC");
1006 mono_test_asany_in (obj);
1007 str = (AsAnyStruct)obj;
1008 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
1014 public static int test_0_asany_out () {
1016 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1017 mono_test_asany_out (str);
1020 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1021 mono_test_asany_out (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_out (obj);
1028 str = (AsAnyStruct)obj;
1029 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1035 public static int test_0_asany_inout () {
1037 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1038 mono_test_asany_inout (str);
1041 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1042 mono_test_asany_inout (cls);
1043 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1047 object obj = new AsAnyStruct(1,2,3, "ABC");
1048 mono_test_asany_inout (obj);
1049 str = (AsAnyStruct)obj;
1050 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1056 /* Byref String Array */
1058 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1059 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1061 public static int test_0_byref_string_array () {
1063 string[] arr = null;
1065 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1068 arr = new string[] { "Alpha", "Beta", "Gamma" };
1070 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1073 /* FIXME: Test returned array and out case */
1079 * AMD64 small structs-by-value tests.
1082 /* TEST 1: 16 byte long INTEGER struct */
1084 [StructLayout(LayoutKind.Sequential)]
1085 public struct Amd64Struct1 {
1092 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1093 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1095 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1096 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);
1098 public static int test_0_amd64_struct1 () {
1099 Amd64Struct1 s = new Amd64Struct1 ();
1105 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1107 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1110 public static int test_0_amd64_struct1_many_args () {
1111 Amd64Struct1 s = new Amd64Struct1 ();
1117 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1119 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1122 /* TEST 2: 8 byte long INTEGER struct */
1124 [StructLayout(LayoutKind.Sequential)]
1125 public struct Amd64Struct2 {
1130 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1131 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1133 public static int test_0_amd64_struct2 () {
1134 Amd64Struct2 s = new Amd64Struct2 ();
1138 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1140 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1143 /* TEST 3: 4 byte long INTEGER struct */
1145 [StructLayout(LayoutKind.Sequential)]
1146 public struct Amd64Struct3 {
1150 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1151 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1153 public static int test_0_amd64_struct3 () {
1154 Amd64Struct3 s = new Amd64Struct3 ();
1157 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1159 return (s2.i == -4) ? 0 : 1;
1162 /* Test 4: 16 byte long FLOAT struct */
1164 [StructLayout(LayoutKind.Sequential)]
1165 public struct Amd64Struct4 {
1166 public double d1, d2;
1169 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1170 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1172 public static int test_0_amd64_struct4 () {
1173 Amd64Struct4 s = new Amd64Struct4 ();
1177 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1179 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1186 /* Test 5: Float HFA */
1188 [StructLayout(LayoutKind.Sequential)]
1189 public struct TestStruct5 {
1190 public float d1, d2;
1193 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1194 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1196 public static int test_0_ia64_struct5 () {
1197 TestStruct5 s = new TestStruct5 ();
1201 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1203 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1206 /* Test 6: Double HFA */
1208 [StructLayout(LayoutKind.Sequential)]
1209 public struct TestStruct6 {
1210 public double d1, d2;
1213 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1214 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1216 public static int test_0_ia64_struct6 () {
1217 TestStruct6 s = new TestStruct6 ();
1221 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1223 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1226 /* Blittable class */
1227 [DllImport("libtest")]
1228 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1230 public static int test_0_marshal_blittable_class () {
1231 BlittableClass v1 = new BlittableClass ();
1233 /* Since it is blittable, it looks like it is passed as in/out */
1234 BlittableClass v2 = TestBlittableClass (v1);
1236 if (v1.a != 2 || v1.b != 3)
1239 if (v2.a != 2 || v2.b != 3)
1243 BlittableClass v3 = TestBlittableClass (null);
1245 if (v3.a != 42 || v3.b != 43)
1252 * Generic structures
1255 [StructLayout(LayoutKind.Sequential)]
1256 public struct Amd64Struct1Gen<T> {
1263 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1264 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1266 public static int test_0_amd64_struct1_gen () {
1267 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1273 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1275 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1282 public static int test_0_marshal_byval_class () {
1283 SimpleObj obj0 = new SimpleObj ();
1287 if (class_marshal_test0 (obj0) != 0)
1293 public static int test_0_marshal_byval_class_null () {
1294 if (class_marshal_test4 (null) != 0)
1300 public static int test_0_marshal_out_class () {
1303 class_marshal_test1 (out obj1);
1305 if (obj1.str != "ABC")
1314 public static int test_0_marshal_string () {
1315 return string_marshal_test0 ("TEST0");
1318 public static int test_0_marshal_out_string () {
1321 string_marshal_test1 (out res);
1329 public static int test_0_marshal_byref_string () {
1330 string res = "TEST1";
1332 int r = string_marshal_test2 (ref res);
1340 public static int test_0_marshal_null_string () {
1341 return string_marshal_test3 (null);
1345 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_1", CallingConvention=CallingConvention.StdCall)]
1346 public static extern int mono_test_stdcall_mismatch_1 (int a, int b, int c);
1348 /* Test mismatched called conventions, the native function is cdecl */
1349 public static int test_0_stdcall_mismatch_1 () {
1350 mono_test_stdcall_mismatch_1 (0, 1, 2);
1354 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_2", CallingConvention=CallingConvention.Cdecl)]
1355 public static extern int mono_test_stdcall_mismatch_2 (int a, int b, int c);
1357 /* Test mismatched called conventions, the native function is stdcall */
1358 public static int test_0_stdcall_mismatch_2 () {
1359 mono_test_stdcall_mismatch_2 (0, 1, 2);
1364 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1365 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1367 public static int test_0_stdcall_name_mangling () {
1368 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1373 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1374 public static extern float mono_test_marshal_pass_return_float (float f);
1376 public static int test_0_pass_return_float () {
1377 float f = mono_test_marshal_pass_return_float (1.5f);
1379 return (f == 2.5f) ? 0 : 1;
1383 * Pointers to structures can not be passed
1386 /* This seems to be allowed by MS in some cases */
1388 public struct CharInfo {
1389 public char Character;
1390 public short Attributes;
1393 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1394 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1396 public static unsafe int test_0_marshal_ptr_to_struct () {
1397 CharInfo [] buffer = new CharInfo [1];
1398 fixed (CharInfo *ptr = &buffer [0]) {
1400 mono_test_marshal_ptr_to_struct (ptr);
1403 catch (MarshalDirectiveException) {
1412 * LPWStr marshalling
1415 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1416 [return: MarshalAs(UnmanagedType.LPWStr)]
1417 private static extern string mono_test_marshal_lpwstr_marshal(
1418 [MarshalAs(UnmanagedType.LPWStr)] string s,
1421 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1422 private static extern string mono_test_marshal_lpwstr_marshal2(
1426 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1427 private static extern void mono_test_marshal_lpwstr_out_marshal(
1428 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1430 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1431 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1434 public static int test_0_pass_return_lwpstr () {
1437 mono_test_marshal_lpwstr_out_marshal (out s);
1443 mono_test_marshal_lpwstr_out_marshal2 (out s);
1451 public static int test_0_out_lwpstr () {
1454 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1459 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1468 * Byref bool marshalling
1471 [DllImport("libtest")]
1472 extern static int marshal_test_ref_bool
1475 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1476 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1480 public static int test_0_pass_byref_bool () {
1481 for (int i = 0; i < 8; i++)
1483 bool b1 = (i & 4) != 0;
1484 bool b2 = (i & 2) != 0;
1485 bool b3 = (i & 1) != 0;
1486 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1487 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1501 * Bool struct field marshalling
1507 [MarshalAs(UnmanagedType.I1)] public bool b1;
1508 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1512 [DllImport("libtest")]
1513 extern static int marshal_test_bool_struct(ref BoolStruct s);
1515 public static int test_0_pass_bool_in_struct () {
1516 for (int i = 0; i < 8; i++)
1518 BoolStruct s = new BoolStruct();
1520 s.b1 = (i & 4) != 0;
1521 s.b2 = (i & 2) != 0;
1522 s.b3 = (i & 1) != 0;
1523 BoolStruct orig = s;
1524 if (marshal_test_bool_struct(ref s) != 0)
1526 if (s.b1 != !orig.b1)
1528 if (s.b2 != !orig.b2)
1530 if (s.b3 != !orig.b3)
1538 * Alignment of structs containing longs
1541 struct LongStruct2 {
1547 public LongStruct2 l;
1550 [DllImport("libtest")]
1551 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1553 public static int test_47_pass_long_struct () {
1554 LongStruct s = new LongStruct ();
1556 s.l = new LongStruct2 ();
1559 return mono_test_marshal_long_struct (ref s);
1563 * Invoking pinvoke methods through delegates
1566 delegate int MyDelegate (string name);
1568 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1569 public static extern int puts_static (string name);
1571 public static int test_0_invoke_pinvoke_through_delegate () {
1572 puts_static ("A simple Test for PInvoke 1");
1574 MyDelegate d = new MyDelegate (puts_static);
1575 d ("A simple Test for PInvoke 2");
1577 object [] args = {"A simple Test for PInvoke 3"};
1578 d.DynamicInvoke (args);
1584 * Missing virtual pinvoke methods
1589 public virtual object MyClone ()
1595 public class T2 : T {
1596 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1597 public override extern object MyClone ();
1600 public static int test_0_missing_virtual_pinvoke_method () {
1605 } catch (Exception ex) {
1613 * Marshalling of type 'object'
1616 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1617 public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1619 public static int test_0_marshal_object () {
1621 mono_test_marshal_object (0, 0, 0, null, 0);
1623 } catch (Exception) {
1629 * Marshalling of DateTime to OLE DATE (double)
1631 [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
1632 public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
1634 public static int test_0_marshal_date_time () {
1635 DateTime d = new DateTime (2009, 12, 6);
1637 double d3 = mono_test_marshal_date_time (d, out d2);
1646 * Calling pinvoke functions dynamically using calli
1649 [DllImport("libtest")]
1650 private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
1652 delegate void CalliDel (IntPtr a, int[] f);
1654 public static int test_0_calli_dynamic () {
1655 /* we need the cdecl version because the icall convention demands it under Windows */
1656 IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1658 DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
1660 var il = dm.GetILGenerator ();
1661 var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
1663 il.Emit (OpCodes.Ldarg, 1);
1664 signature.AddArgument (typeof (byte[]));
1666 il.Emit (OpCodes.Ldarg_0);
1668 il.Emit (OpCodes.Calli, signature);
1669 il.Emit (OpCodes.Ret);
1671 var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
1673 int[] arr = new int [1000];
1674 for (int i = 0; i < 50; ++i)
1677 if (arr.Length != 1000)
1679 for (int i = 0; i < 50; ++i)
1680 if (arr [i] != 50 - i)
1687 /*char array marshaling */
1688 [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
1689 public static extern int mono_test_marshal_ansi_char_array (char[] a1);
1691 public static int test_0_marshal_ansi_char_array () {
1692 char[] buf = new char [32];
1698 if (mono_test_marshal_ansi_char_array (buf) != 0)
1701 string s = new string (buf);
1702 if (s.StartsWith ("qwer"))
1708 /*char array marshaling */
1709 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
1710 public static extern int mono_test_marshal_unicode_char_array (char[] a1);
1712 public static int test_0_marshal_unicode_char_array () {
1713 char[] buf = new char [32];
1719 if (mono_test_marshal_unicode_char_array (buf) != 0)
1722 string s = new string (buf);
1723 if (s.StartsWith ("abcdef"))
1729 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
1730 public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
1732 public static int test_0_mono_test_marshal_lpstr () {
1735 if (mono_test_marshal_lpstr (str) != 0)
1741 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
1742 public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
1744 public static int test_0_mono_test_marshal_lpwstr () {
1747 if (mono_test_marshal_lpwstr (str) != 0)
1754 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
1755 [return: MarshalAs(UnmanagedType.LPStr)]
1756 public static extern string mono_test_marshal_return_lpstr ();
1758 public static int test_0_mono_test_marshal_return_lpstr () {
1759 string str = mono_test_marshal_return_lpstr ();
1766 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
1767 [return: MarshalAs(UnmanagedType.LPWStr)]
1768 public static extern string mono_test_marshal_return_lpwstr ();
1770 public static int test_0_mono_test_marshal_return_lpwstr () {
1771 string str = mono_test_marshal_return_lpwstr ();
1778 [DllImport ("libtest", EntryPoint="mono_test_has_thiscall")]
1779 public static extern int mono_test_has_thiscall ();
1781 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1782 public static extern int mono_test_native_thiscall (int a);
1784 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1785 public static extern int mono_test_native_thiscall (int a, int b);
1787 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1788 public static extern int mono_test_native_thiscall (int a, int b, int c);
1790 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1791 public static extern int mono_test_native_thiscall (TinyStruct a);
1793 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1794 public static extern int mono_test_native_thiscall (TinyStruct a, int b);
1796 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1797 public static extern int mono_test_native_thiscall (TinyStruct a, int b, int c);
1799 public static int test_0_native_thiscall ()
1801 if (mono_test_has_thiscall () == 0)
1804 if (mono_test_native_thiscall (1968329802) != 1968329802)
1807 if (mono_test_native_thiscall (268894549, 1212675791) != 1481570339)
1810 if (mono_test_native_thiscall (1288082683, -421187449, -1733670329) != -866775098)
1813 if (mono_test_native_thiscall (new TinyStruct(1968329802)) != 1968329802)
1816 if (mono_test_native_thiscall (new TinyStruct(268894549), 1212675791) != 1481570339)
1819 if (mono_test_native_thiscall (new TinyStruct(1288082683), -421187449, -1733670329) != -866775098)
1825 [DllImport ("libtest", EntryPoint = "mono_test_marshal_return_single_double_struct")]
1826 public static extern SingleDoubleStruct mono_test_marshal_return_single_double_struct ();
1828 public static int test_0_x86_single_double_struct_ret () {
1829 double d = mono_test_marshal_return_single_double_struct ().d;