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_inout_nonblittable_array", CharSet = CharSet.Unicode)]
231 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
233 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
234 public static extern int mono_test_marshal_struct (SimpleStruct ss);
236 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
237 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
239 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
240 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
242 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
243 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
245 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
246 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
248 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
249 public static extern int mono_test_marshal_byref_struct_in ([In] 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_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
254 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
255 public static extern int mono_test_marshal_point (Point p);
257 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
258 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
260 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
261 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
263 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
264 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
266 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
267 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
269 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
270 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
272 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
273 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
275 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
276 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
278 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
279 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
281 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
282 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
284 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
285 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
287 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
288 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
290 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
291 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
293 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
294 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
296 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder2")]
297 public static extern void mono_test_marshal_stringbuilder2 (StringBuilder sb, int len);
299 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
300 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
302 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
303 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
305 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
306 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
308 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_ref")]
309 public static extern int mono_test_marshal_stringbuilder_ref (ref StringBuilder sb);
311 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
312 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
314 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
315 public static extern void mono_test_last_error (int err);
317 [DllImport ("libtest", EntryPoint="mono_test_asany")]
318 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
320 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
321 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
323 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
324 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
326 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
327 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
328 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
329 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
331 [DllImport ("libtest")]
332 static extern int class_marshal_test0 (SimpleObj obj);
334 [DllImport ("libtest")]
335 static extern void class_marshal_test1 (out SimpleObj obj);
337 [DllImport ("libtest")]
338 static extern int class_marshal_test4 (SimpleObj obj);
340 [DllImport ("libtest")]
341 static extern int string_marshal_test0 (string str);
343 [DllImport ("libtest")]
344 static extern void string_marshal_test1 (out string str);
346 [DllImport ("libtest")]
347 static extern int string_marshal_test2 (ref string str);
349 [DllImport ("libtest")]
350 static extern int string_marshal_test3 (string str);
352 public delegate int SimpleDelegate (int a);
354 public static int Main (string[] args) {
355 return TestDriver.RunTests (typeof (Tests), args);
358 public static int test_0_marshal_char () {
359 return mono_test_marshal_char ('a');
362 public static int test_0_marshal_char_array () {
363 // a unicode char[] is implicitly marshalled as [Out]
364 char[] buf = new char [32];
365 mono_test_marshal_char_array (buf);
366 string s = new string (buf);
367 if (s.StartsWith ("abcdef"))
373 public static int test_1225_marshal_array () {
374 int [] a1 = new int [50];
375 for (int i = 0; i < 50; i++)
378 return mono_test_marshal_array (a1);
381 public static int test_1225_marshal_inout_array () {
382 int [] a1 = new int [50];
383 for (int i = 0; i < 50; i++)
386 int res = mono_test_marshal_inout_array (a1);
388 for (int i = 0; i < 50; i++)
389 if (a1 [i] != 50 - i) {
390 Console.WriteLine ("X: " + i + " " + a1 [i]);
397 public static int test_0_marshal_out_array () {
398 int [] a1 = new int [50];
400 int res = mono_test_marshal_out_array (a1, 0);
402 for (int i = 0; i < 50; i++)
404 Console.WriteLine ("X: " + i + " " + a1 [i]);
411 public static int test_0_marshal_inout_nonblittable_array () {
412 char [] a1 = new char [10];
413 for (int i = 0; i < 10; i++)
414 a1 [i] = "Hello, World" [i];
416 int res = mono_test_marshal_inout_nonblittable_array (a1);
418 for (int i = 0; i < 10; i++)
425 public static int test_0_marshal_struct () {
426 SimpleStruct ss = new SimpleStruct ();
430 return mono_test_marshal_struct (ss);
433 public static int test_0_marshal_struct_gen () {
434 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
438 return mono_test_marshal_struct_gen (ss);
441 public static int test_0_marshal_struct2 () {
442 SimpleStruct2 ss2 = new SimpleStruct2 ();
450 return mono_test_marshal_struct2 (ss2);
453 public static int test_0_marshal_struct3 () {
454 SimpleStruct2 ss2 = new SimpleStruct2 ();
462 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
465 public static int test_0_marshal_empty_struct () {
466 EmptyStruct es = new EmptyStruct ();
468 if (mono_test_empty_struct (1, es, 2) != 0)
474 public static int test_0_marshal_lpstruct () {
475 SimpleStruct ss = new SimpleStruct ();
479 return mono_test_marshal_lpstruct (ss);
482 public static int test_0_marshal_lpstruct_blittable () {
483 Point p = new Point ();
487 return mono_test_marshal_lpstruct_blittable (p);
490 public static int test_0_marshal_struct_array () {
491 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
493 SimpleStruct2 ss2 = new SimpleStruct2 ();
512 return mono_test_marshal_struct_array (ss_arr);
515 public static int test_105_marshal_long_align_struct_array () {
516 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
518 LongAlignStruct ss = new LongAlignStruct ();
531 return mono_test_marshal_long_align_struct_array (ss_arr);
534 /* Test classes as arguments and return values */
535 public static int test_0_marshal_class () {
536 SimpleClass ss = new SimpleClass ();
544 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
547 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
548 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
549 res.g == ss.g && res.h == ss.h))
552 /* Test null arguments and results */
553 res = mono_test_marshal_class (10, 11, 12, null, 14);
560 public static int test_0_marshal_byref_class () {
561 SimpleClass ss = new SimpleClass ();
569 int res = mono_test_marshal_byref_class (ref ss);
570 if (ss.d != "TEST-RES")
576 public static int test_0_marshal_delegate () {
577 SimpleDelegate d = new SimpleDelegate (delegate_test);
579 return mono_test_marshal_delegate (d);
582 public static int test_34_marshal_instance_delegate () {
583 Tests t = new Tests ();
585 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
587 return mono_test_marshal_delegate (d);
590 /* Static delegates closed over their first argument */
591 public static int closed_delegate (Tests t, int a) {
592 return t.int_field + a;
595 public static int test_34_marshal_closed_static_delegate () {
596 Tests t = new Tests ();
598 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
600 return mono_test_marshal_delegate (d);
603 public static int test_0_marshal_return_delegate () {
604 SimpleDelegate d = new SimpleDelegate (delegate_test);
606 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
611 public static int test_0_marshal_delegate_struct () {
612 DelegateStruct s = new DelegateStruct ();
615 s.del = new SimpleDelegate (delegate_test);
616 s.del2 = new SimpleDelegate (delegate_test);
619 DelegateStruct res = mono_test_marshal_delegate_struct (s);
623 if (res.del (2) != 0)
625 if (res.del2 (2) != 0)
627 if (res.del3 != null)
633 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
634 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
636 public static int test_3_marshal_out_delegate () {
637 SimpleDelegate d = null;
639 mono_test_marshal_out_delegate (out d);
644 public static int test_0_marshal_byref_struct () {
645 SimpleStruct s = new SimpleStruct ();
652 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
655 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
660 public static int test_0_marshal_byref_struct_in () {
661 SimpleStruct s = new SimpleStruct ();
668 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
671 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
676 public static int test_0_marshal_byref_struct_inout () {
677 SimpleStruct s = new SimpleStruct ();
684 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
687 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
692 public static int test_0_marshal_point () {
693 Point pt = new Point();
697 return mono_test_marshal_point(pt);
700 public static int test_0_marshal_mixed_point () {
701 MixedPoint mpt = new MixedPoint();
705 return mono_test_marshal_mixed_point(mpt);
708 public static int test_0_marshal_bool_byref () {
710 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
713 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
721 public static int test_0_marshal_bool_as_I1 () {
725 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
728 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
737 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
749 public static int test_0_marshal_bool_as_U1 () {
753 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
756 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
765 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
777 public static int test_0_return_vtype () {
778 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
780 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
786 public static int test_0_marshal_stringbuilder () {
787 StringBuilder sb = new StringBuilder(255);
789 mono_test_marshal_stringbuilder (sb, sb.Capacity);
790 String res = sb.ToString();
792 if (res != "This is my message. Isn't it nice?")
795 // Test that cached_str is cleared
796 mono_test_marshal_stringbuilder2 (sb, sb.Capacity);
801 // Test StringBuilder with default capacity (16)
802 StringBuilder sb2 = new StringBuilder();
803 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
804 if (sb2.ToString () != "This is my messa")
810 public static int test_0_marshal_stringbuilder_unicode () {
811 StringBuilder sb = new StringBuilder(255);
812 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
813 String res = sb.ToString();
815 if (res != "This is my message. Isn't it nice?")
818 // Test StringBuilder with default capacity (16)
819 StringBuilder sb2 = new StringBuilder();
820 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
821 if (sb2.ToString () != "This is my messa")
827 public static int test_0_marshal_stringbuilder_out () {
829 mono_test_marshal_stringbuilder_out (out sb);
831 if (sb.ToString () != "This is my message. Isn't it nice?")
836 public static int test_0_marshal_stringbuilder_out_unicode () {
838 mono_test_marshal_stringbuilder_out_unicode (out sb);
840 if (sb.ToString () != "This is my message. Isn't it nice?")
845 public static int test_0_marshal_stringbuilder_ref () {
846 StringBuilder sb = new StringBuilder ();
848 int res = mono_test_marshal_stringbuilder_ref (ref sb);
852 if (sb.ToString () != "This is my message. Isn't it nice?")
857 public static int test_0_marshal_empty_string_array () {
858 return mono_test_marshal_empty_string_array (null);
861 public static int test_0_marshal_string_array () {
862 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
865 public static int test_0_marshal_unicode_string_array () {
866 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
869 public static int test_0_marshal_stringbuilder_array () {
870 StringBuilder sb1 = new StringBuilder ("ABC");
871 StringBuilder sb2 = new StringBuilder ("DEF");
873 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
876 if (sb1.ToString () != "DEF")
878 if (sb2.ToString () != "ABC")
883 public static int test_0_last_error () {
884 mono_test_last_error (5);
885 if (Marshal.GetLastWin32Error () == 5)
891 public static int test_0_entry_point_not_found () {
894 mono_entry_point_not_found ();
897 catch (EntryPointNotFoundException) {
903 public static int test_0_library_not_found () {
906 mono_library_not_found ();
909 catch (DllNotFoundException) {
915 /* Check that the runtime trims .dll from the library name */
916 public static int test_0_trim_dll_from_name () {
918 mono_test_marshal_char_2 ('A');
923 /* Check that the runtime adds lib to to the library name */
924 public static int test_0_add_lib_to_name () {
926 mono_test_marshal_char_3 ('A');
935 public static int test_0_asany () {
936 if (mono_test_asany (5, 1) != 0)
939 if (mono_test_asany ("ABC", 2) != 0)
942 SimpleStruct2 ss2 = new SimpleStruct2 ();
950 if (mono_test_asany (ss2, 3) != 0)
953 if (mono_test_asany_unicode ("ABC", 4) != 0)
959 mono_test_asany (c, 0);
962 catch (ArgumentException) {
966 mono_test_asany (new Object (), 0);
969 catch (ArgumentException) {
975 /* AsAny marshalling + [In, Out] */
977 public static int test_0_asany_in () {
979 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
980 mono_test_asany_in (str);
983 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
984 mono_test_asany_in (cls);
985 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
989 object obj = new AsAnyStruct(1,2,3, "ABC");
990 mono_test_asany_in (obj);
991 str = (AsAnyStruct)obj;
992 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
998 public static int test_0_asany_out () {
1000 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1001 mono_test_asany_out (str);
1004 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1005 mono_test_asany_out (cls);
1006 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1010 object obj = new AsAnyStruct(1,2,3, "ABC");
1011 mono_test_asany_out (obj);
1012 str = (AsAnyStruct)obj;
1013 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1019 public static int test_0_asany_inout () {
1021 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
1022 mono_test_asany_inout (str);
1025 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
1026 mono_test_asany_inout (cls);
1027 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
1031 object obj = new AsAnyStruct(1,2,3, "ABC");
1032 mono_test_asany_inout (obj);
1033 str = (AsAnyStruct)obj;
1034 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
1040 /* Byref String Array */
1042 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1043 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1045 public static int test_0_byref_string_array () {
1047 string[] arr = null;
1049 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1052 arr = new string[] { "Alpha", "Beta", "Gamma" };
1054 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1057 /* FIXME: Test returned array and out case */
1063 * AMD64 small structs-by-value tests.
1066 /* TEST 1: 16 byte long INTEGER struct */
1068 [StructLayout(LayoutKind.Sequential)]
1069 public struct Amd64Struct1 {
1076 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1077 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1079 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1080 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);
1082 public static int test_0_amd64_struct1 () {
1083 Amd64Struct1 s = new Amd64Struct1 ();
1089 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1091 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1094 public static int test_0_amd64_struct1_many_args () {
1095 Amd64Struct1 s = new Amd64Struct1 ();
1101 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1103 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1106 /* TEST 2: 8 byte long INTEGER struct */
1108 [StructLayout(LayoutKind.Sequential)]
1109 public struct Amd64Struct2 {
1114 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1115 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1117 public static int test_0_amd64_struct2 () {
1118 Amd64Struct2 s = new Amd64Struct2 ();
1122 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1124 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1127 /* TEST 3: 4 byte long INTEGER struct */
1129 [StructLayout(LayoutKind.Sequential)]
1130 public struct Amd64Struct3 {
1134 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1135 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1137 public static int test_0_amd64_struct3 () {
1138 Amd64Struct3 s = new Amd64Struct3 ();
1141 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1143 return (s2.i == -4) ? 0 : 1;
1146 /* Test 4: 16 byte long FLOAT struct */
1148 [StructLayout(LayoutKind.Sequential)]
1149 public struct Amd64Struct4 {
1150 public double d1, d2;
1153 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1154 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1156 public static int test_0_amd64_struct4 () {
1157 Amd64Struct4 s = new Amd64Struct4 ();
1161 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1163 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1170 /* Test 5: Float HFA */
1172 [StructLayout(LayoutKind.Sequential)]
1173 public struct TestStruct5 {
1174 public float d1, d2;
1177 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1178 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1180 public static int test_0_ia64_struct5 () {
1181 TestStruct5 s = new TestStruct5 ();
1185 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1187 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1190 /* Test 6: Double HFA */
1192 [StructLayout(LayoutKind.Sequential)]
1193 public struct TestStruct6 {
1194 public double d1, d2;
1197 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1198 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1200 public static int test_0_ia64_struct6 () {
1201 TestStruct6 s = new TestStruct6 ();
1205 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1207 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1210 /* Blittable class */
1211 [DllImport("libtest")]
1212 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1214 public static int test_0_marshal_blittable_class () {
1215 BlittableClass v1 = new BlittableClass ();
1217 /* Since it is blittable, it looks like it is passed as in/out */
1218 BlittableClass v2 = TestBlittableClass (v1);
1220 if (v1.a != 2 || v1.b != 3)
1223 if (v2.a != 2 || v2.b != 3)
1227 BlittableClass v3 = TestBlittableClass (null);
1229 if (v3.a != 42 || v3.b != 43)
1236 * Generic structures
1239 [StructLayout(LayoutKind.Sequential)]
1240 public struct Amd64Struct1Gen<T> {
1247 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1248 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1250 public static int test_0_amd64_struct1_gen () {
1251 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1257 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1259 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1266 public static int test_0_marshal_byval_class () {
1267 SimpleObj obj0 = new SimpleObj ();
1271 if (class_marshal_test0 (obj0) != 0)
1277 public static int test_0_marshal_byval_class_null () {
1278 if (class_marshal_test4 (null) != 0)
1284 public static int test_0_marshal_out_class () {
1287 class_marshal_test1 (out obj1);
1289 if (obj1.str != "ABC")
1298 public static int test_0_marshal_string () {
1299 return string_marshal_test0 ("TEST0");
1302 public static int test_0_marshal_out_string () {
1305 string_marshal_test1 (out res);
1313 public static int test_0_marshal_byref_string () {
1314 string res = "TEST1";
1316 int r = string_marshal_test2 (ref res);
1324 public static int test_0_marshal_null_string () {
1325 return string_marshal_test3 (null);
1329 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_1", CallingConvention=CallingConvention.StdCall)]
1330 public static extern int mono_test_stdcall_mismatch_1 (int a, int b, int c);
1332 /* Test mismatched called conventions, the native function is cdecl */
1333 public static int test_0_stdcall_mismatch_1 () {
1334 mono_test_stdcall_mismatch_1 (0, 1, 2);
1338 [DllImport ("libtest", EntryPoint="mono_test_stdcall_mismatch_2", CallingConvention=CallingConvention.Cdecl)]
1339 public static extern int mono_test_stdcall_mismatch_2 (int a, int b, int c);
1341 /* Test mismatched called conventions, the native function is stdcall */
1342 public static int test_0_stdcall_mismatch_2 () {
1343 mono_test_stdcall_mismatch_2 (0, 1, 2);
1348 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1349 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1351 public static int test_0_stdcall_name_mangling () {
1352 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1357 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1358 public static extern float mono_test_marshal_pass_return_float (float f);
1360 public static int test_0_pass_return_float () {
1361 float f = mono_test_marshal_pass_return_float (1.5f);
1363 return (f == 2.5f) ? 0 : 1;
1367 * Pointers to structures can not be passed
1370 /* This seems to be allowed by MS in some cases */
1372 public struct CharInfo {
1373 public char Character;
1374 public short Attributes;
1377 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1378 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1380 public static unsafe int test_0_marshal_ptr_to_struct () {
1381 CharInfo [] buffer = new CharInfo [1];
1382 fixed (CharInfo *ptr = &buffer [0]) {
1384 mono_test_marshal_ptr_to_struct (ptr);
1387 catch (MarshalDirectiveException) {
1396 * LPWStr marshalling
1399 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1400 [return: MarshalAs(UnmanagedType.LPWStr)]
1401 private static extern string mono_test_marshal_lpwstr_marshal(
1402 [MarshalAs(UnmanagedType.LPWStr)] string s,
1405 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1406 private static extern string mono_test_marshal_lpwstr_marshal2(
1410 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1411 private static extern void mono_test_marshal_lpwstr_out_marshal(
1412 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1414 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1415 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1418 public static int test_0_pass_return_lwpstr () {
1421 mono_test_marshal_lpwstr_out_marshal (out s);
1427 mono_test_marshal_lpwstr_out_marshal2 (out s);
1435 public static int test_0_out_lwpstr () {
1438 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1443 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1452 * Byref bool marshalling
1455 [DllImport("libtest")]
1456 extern static int marshal_test_ref_bool
1459 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1460 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1464 public static int test_0_pass_byref_bool () {
1465 for (int i = 0; i < 8; i++)
1467 bool b1 = (i & 4) != 0;
1468 bool b2 = (i & 2) != 0;
1469 bool b3 = (i & 1) != 0;
1470 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1471 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1485 * Bool struct field marshalling
1491 [MarshalAs(UnmanagedType.I1)] public bool b1;
1492 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1496 [DllImport("libtest")]
1497 extern static int marshal_test_bool_struct(ref BoolStruct s);
1499 public static int test_0_pass_bool_in_struct () {
1500 for (int i = 0; i < 8; i++)
1502 BoolStruct s = new BoolStruct();
1504 s.b1 = (i & 4) != 0;
1505 s.b2 = (i & 2) != 0;
1506 s.b3 = (i & 1) != 0;
1507 BoolStruct orig = s;
1508 if (marshal_test_bool_struct(ref s) != 0)
1510 if (s.b1 != !orig.b1)
1512 if (s.b2 != !orig.b2)
1514 if (s.b3 != !orig.b3)
1522 * Alignment of structs containing longs
1525 struct LongStruct2 {
1531 public LongStruct2 l;
1534 [DllImport("libtest")]
1535 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1537 public static int test_47_pass_long_struct () {
1538 LongStruct s = new LongStruct ();
1540 s.l = new LongStruct2 ();
1543 return mono_test_marshal_long_struct (ref s);
1547 * Invoking pinvoke methods through delegates
1550 delegate int MyDelegate (string name);
1552 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1553 public static extern int puts_static (string name);
1555 public static int test_0_invoke_pinvoke_through_delegate () {
1556 puts_static ("A simple Test for PInvoke 1");
1558 MyDelegate d = new MyDelegate (puts_static);
1559 d ("A simple Test for PInvoke 2");
1561 object [] args = {"A simple Test for PInvoke 3"};
1562 d.DynamicInvoke (args);
1568 * Missing virtual pinvoke methods
1573 public virtual object MyClone ()
1579 public class T2 : T {
1580 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1581 public override extern object MyClone ();
1584 public static int test_0_missing_virtual_pinvoke_method () {
1589 } catch (Exception ex) {
1597 * Marshalling of type 'object'
1600 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1601 public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1603 public static int test_0_marshal_object () {
1605 mono_test_marshal_object (0, 0, 0, null, 0);
1607 } catch (Exception) {
1613 * Marshalling of DateTime to OLE DATE (double)
1615 [DllImport ("libtest", EntryPoint="mono_test_marshal_date_time")]
1616 public static extern double mono_test_marshal_date_time (DateTime d, out DateTime d2);
1618 public static int test_0_marshal_date_time () {
1619 DateTime d = new DateTime (2009, 12, 6);
1621 double d3 = mono_test_marshal_date_time (d, out d2);
1630 * Calling pinvoke functions dynamically using calli
1633 [DllImport("libtest")]
1634 private static extern IntPtr mono_test_marshal_lookup_symbol (string fileName);
1636 delegate void CalliDel (IntPtr a, int[] f);
1638 public static int test_0_calli_dynamic () {
1639 /* we need the cdecl version because the icall convention demands it under Windows */
1640 IntPtr func = mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1642 DynamicMethod dm = new DynamicMethod ("calli", typeof (void), new Type [] { typeof (IntPtr), typeof (int[]) });
1644 var il = dm.GetILGenerator ();
1645 var signature = SignatureHelper.GetMethodSigHelper (CallingConvention.Cdecl, typeof (void));
1647 il.Emit (OpCodes.Ldarg, 1);
1648 signature.AddArgument (typeof (byte[]));
1650 il.Emit (OpCodes.Ldarg_0);
1652 il.Emit (OpCodes.Calli, signature);
1653 il.Emit (OpCodes.Ret);
1655 var f = (CalliDel)dm.CreateDelegate (typeof (CalliDel));
1657 int[] arr = new int [1000];
1658 for (int i = 0; i < 50; ++i)
1661 if (arr.Length != 1000)
1663 for (int i = 0; i < 50; ++i)
1664 if (arr [i] != 50 - i)
1671 /*char array marshaling */
1672 [DllImport ("libtest", EntryPoint="mono_test_marshal_ansi_char_array", CharSet=CharSet.Ansi)]
1673 public static extern int mono_test_marshal_ansi_char_array (char[] a1);
1675 public static int test_0_marshal_ansi_char_array () {
1676 char[] buf = new char [32];
1682 if (mono_test_marshal_ansi_char_array (buf) != 0)
1685 string s = new string (buf);
1686 if (s.StartsWith ("qwer"))
1692 /*char array marshaling */
1693 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_char_array", CharSet=CharSet.Unicode)]
1694 public static extern int mono_test_marshal_unicode_char_array (char[] a1);
1696 public static int test_0_marshal_unicode_char_array () {
1697 char[] buf = new char [32];
1703 if (mono_test_marshal_unicode_char_array (buf) != 0)
1706 string s = new string (buf);
1707 if (s.StartsWith ("abcdef"))
1713 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstr")]
1714 public static extern int mono_test_marshal_lpstr ([MarshalAs(UnmanagedType.LPStr)] string str);
1716 public static int test_0_mono_test_marshal_lpstr () {
1719 if (mono_test_marshal_lpstr (str) != 0)
1725 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpwstr")]
1726 public static extern int mono_test_marshal_lpwstr ([MarshalAs(UnmanagedType.LPWStr)] string str);
1728 public static int test_0_mono_test_marshal_lpwstr () {
1731 if (mono_test_marshal_lpwstr (str) != 0)
1738 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpstr")]
1739 [return: MarshalAs(UnmanagedType.LPStr)]
1740 public static extern string mono_test_marshal_return_lpstr ();
1742 public static int test_0_mono_test_marshal_return_lpstr () {
1743 string str = mono_test_marshal_return_lpstr ();
1750 [method: DllImport ("libtest", EntryPoint="mono_test_marshal_return_lpwstr")]
1751 [return: MarshalAs(UnmanagedType.LPWStr)]
1752 public static extern string mono_test_marshal_return_lpwstr ();
1754 public static int test_0_mono_test_marshal_return_lpwstr () {
1755 string str = mono_test_marshal_return_lpwstr ();
1762 [DllImport ("libtest", EntryPoint="mono_test_has_thiscall")]
1763 public static extern int mono_test_has_thiscall ();
1765 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1766 public static extern int mono_test_native_thiscall (int a);
1768 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1769 public static extern int mono_test_native_thiscall (int a, int b);
1771 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1772 public static extern int mono_test_native_thiscall (int a, int b, int c);
1774 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall1", CallingConvention=CallingConvention.ThisCall)]
1775 public static extern int mono_test_native_thiscall (TinyStruct a);
1777 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall2", CallingConvention=CallingConvention.ThisCall)]
1778 public static extern int mono_test_native_thiscall (TinyStruct a, int b);
1780 [DllImport ("libtest", EntryPoint = "_mono_test_native_thiscall3", CallingConvention=CallingConvention.ThisCall)]
1781 public static extern int mono_test_native_thiscall (TinyStruct a, int b, int c);
1783 public static int test_0_native_thiscall ()
1785 if (mono_test_has_thiscall () == 0)
1788 if (mono_test_native_thiscall (1968329802) != 1968329802)
1791 if (mono_test_native_thiscall (268894549, 1212675791) != 1481570339)
1794 if (mono_test_native_thiscall (1288082683, -421187449, -1733670329) != -866775098)
1797 if (mono_test_native_thiscall (new TinyStruct(1968329802)) != 1968329802)
1800 if (mono_test_native_thiscall (new TinyStruct(268894549), 1212675791) != 1481570339)
1803 if (mono_test_native_thiscall (new TinyStruct(1288082683), -421187449, -1733670329) != -866775098)
1809 [DllImport ("libtest", EntryPoint = "mono_test_marshal_return_single_double_struct")]
1810 public static extern SingleDoubleStruct mono_test_marshal_return_single_double_struct ();
1812 public static int test_0_x86_single_double_struct_ret () {
1813 double d = mono_test_marshal_return_single_double_struct ().d;