3 using System.Runtime.InteropServices;
4 using System.Runtime.CompilerServices;
10 public static int delegate_test (int a)
18 public int delegate_test_instance (int a)
23 [StructLayout (LayoutKind.Sequential)]
24 public struct SimpleStruct {
29 [MarshalAs(UnmanagedType.LPWStr)]
33 [StructLayout (LayoutKind.Sequential)]
34 public struct SimpleStructGen<T> {
39 [MarshalAs(UnmanagedType.LPWStr)]
43 [StructLayout (LayoutKind.Sequential)]
44 public struct SimpleStruct2 {
55 [StructLayout (LayoutKind.Sequential, Size=0)]
56 public struct EmptyStruct {
59 [StructLayout (LayoutKind.Sequential)]
60 public struct DelegateStruct {
62 public SimpleDelegate del;
63 [MarshalAs(UnmanagedType.FunctionPtr)]
64 public SimpleDelegate del2;
65 [MarshalAs(UnmanagedType.FunctionPtr)]
66 public SimpleDelegate del3;
69 /* sparcv9 has complex conventions when passing structs with doubles in them
70 by value, some simple tests for them */
71 [StructLayout (LayoutKind.Sequential)]
77 [StructLayout (LayoutKind.Sequential)]
78 public struct MixedPoint {
83 [StructLayout (LayoutKind.Sequential)]
84 public class SimpleClass {
95 [StructLayout (LayoutKind.Sequential)]
96 public class EmptyClass {
99 [StructLayout (LayoutKind.Sequential)]
100 public struct LongAlignStruct {
106 [StructLayout(LayoutKind.Sequential)]
107 public class BlittableClass
113 [StructLayout (LayoutKind.Sequential)]
120 [StructLayout(LayoutKind.Sequential)]
128 public AsAnyStruct(int i, int j, int k, String s) {
136 [StructLayout(LayoutKind.Sequential)]
144 public AsAnyClass(int i, int j, int k, String s) {
151 [DllImport ("libnot-found", EntryPoint="not_found")]
152 public static extern int mono_library_not_found ();
154 [DllImport ("libtest", EntryPoint="not_found")]
155 public static extern int mono_entry_point_not_found ();
157 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
158 public static extern int mono_test_marshal_char_2 (char a1);
160 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
161 public static extern int mono_test_marshal_char_3 (char a1);
163 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
164 public static extern int mono_test_marshal_char (char a1);
166 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
167 public static extern int mono_test_marshal_char_array (char[] a1);
169 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
170 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
172 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
173 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
175 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
176 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
178 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
179 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
181 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
182 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
184 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
185 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
187 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
188 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
190 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
191 public static extern int mono_test_marshal_array (int [] a1);
193 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
194 public static extern int mono_test_marshal_empty_string_array (string [] a1);
196 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
197 public static extern int mono_test_marshal_string_array (string [] a1);
199 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
200 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
202 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
203 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
205 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
206 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
208 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
209 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
211 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
212 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
214 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
215 public static extern int mono_test_marshal_struct (SimpleStruct ss);
217 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
218 public static extern int mono_test_marshal_struct_gen (SimpleStructGen<string> ss);
220 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
221 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
223 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
224 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
226 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
227 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
229 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
230 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
232 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
233 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
235 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
236 public static extern int mono_test_marshal_point (Point p);
238 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
239 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
241 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
242 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
244 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
245 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
247 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
248 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
250 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
251 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
253 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
254 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
256 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
257 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
259 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
260 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
262 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
263 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
265 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
266 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
268 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
269 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
271 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
272 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
274 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
275 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
277 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
278 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
280 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
281 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
283 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out")]
284 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb);
286 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_out_unicode", CharSet=CharSet.Unicode)]
287 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb);
289 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
290 public static extern void mono_test_last_error (int err);
292 [DllImport ("libtest", EntryPoint="mono_test_asany")]
293 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
295 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
296 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
298 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
299 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
301 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
302 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
303 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
304 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
306 [DllImport ("libtest")]
307 static extern int class_marshal_test0 (SimpleObj obj);
309 [DllImport ("libtest")]
310 static extern void class_marshal_test1 (out SimpleObj obj);
312 [DllImport ("libtest")]
313 static extern int class_marshal_test4 (SimpleObj obj);
315 [DllImport ("libtest")]
316 static extern int string_marshal_test0 (string str);
318 [DllImport ("libtest")]
319 static extern void string_marshal_test1 (out string str);
321 [DllImport ("libtest")]
322 static extern int string_marshal_test2 (ref string str);
324 [DllImport ("libtest")]
325 static extern int string_marshal_test3 (string str);
327 public delegate int SimpleDelegate (int a);
329 public static int Main (string[] args) {
330 return TestDriver.RunTests (typeof (Tests), args);
333 public static int test_0_marshal_char () {
334 return mono_test_marshal_char ('a');
337 public static int test_0_marshal_char_array () {
338 // a unicode char[] is implicitly marshalled as [Out]
339 char[] buf = new char [32];
340 mono_test_marshal_char_array (buf);
341 string s = new string (buf);
342 if (s.StartsWith ("abcdef"))
348 public static int test_1225_marshal_array () {
349 int [] a1 = new int [50];
350 for (int i = 0; i < 50; i++)
353 return mono_test_marshal_array (a1);
356 public static int test_1225_marshal_inout_array () {
357 int [] a1 = new int [50];
358 for (int i = 0; i < 50; i++)
361 int res = mono_test_marshal_inout_array (a1);
363 for (int i = 0; i < 50; i++)
364 if (a1 [i] != 50 - i) {
365 Console.WriteLine ("X: " + i + " " + a1 [i]);
372 public static int test_0_marshal_out_array () {
373 int [] a1 = new int [50];
375 int res = mono_test_marshal_out_array (a1, 0);
377 for (int i = 0; i < 50; i++)
379 Console.WriteLine ("X: " + i + " " + a1 [i]);
386 public static int test_0_marshal_inout_nonblittable_array () {
387 char [] a1 = new char [10];
388 for (int i = 0; i < 10; i++)
389 a1 [i] = "Hello, World" [i];
391 int res = mono_test_marshal_inout_nonblittable_array (a1);
393 for (int i = 0; i < 10; i++)
400 public static int test_0_marshal_struct () {
401 SimpleStruct ss = new SimpleStruct ();
405 return mono_test_marshal_struct (ss);
408 public static int test_0_marshal_struct_gen () {
409 SimpleStructGen<string> ss = new SimpleStructGen<string> ();
413 return mono_test_marshal_struct_gen (ss);
416 public static int test_0_marshal_struct2 () {
417 SimpleStruct2 ss2 = new SimpleStruct2 ();
425 return mono_test_marshal_struct2 (ss2);
428 public static int test_0_marshal_struct3 () {
429 SimpleStruct2 ss2 = new SimpleStruct2 ();
437 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
440 public static int test_0_marshal_empty_struct () {
441 EmptyStruct es = new EmptyStruct ();
443 if (mono_test_empty_struct (1, es, 2) != 0)
449 public static int test_0_marshal_lpstruct () {
450 SimpleStruct ss = new SimpleStruct ();
454 return mono_test_marshal_lpstruct (ss);
457 public static int test_0_marshal_lpstruct_blittable () {
458 Point p = new Point ();
462 return mono_test_marshal_lpstruct_blittable (p);
465 public static int test_0_marshal_struct_array () {
466 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
468 SimpleStruct2 ss2 = new SimpleStruct2 ();
487 return mono_test_marshal_struct_array (ss_arr);
490 public static int test_105_marshal_long_align_struct_array () {
491 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
493 LongAlignStruct ss = new LongAlignStruct ();
506 return mono_test_marshal_long_align_struct_array (ss_arr);
509 /* Test classes as arguments and return values */
510 public static int test_0_marshal_class () {
511 SimpleClass ss = new SimpleClass ();
519 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
522 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
523 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
524 res.g == ss.g && res.h == ss.h))
527 /* Test null arguments and results */
528 res = mono_test_marshal_class (10, 11, 12, null, 14);
535 public static int test_0_marshal_byref_class () {
536 SimpleClass ss = new SimpleClass ();
544 int res = mono_test_marshal_byref_class (ref ss);
545 if (ss.d != "TEST-RES")
551 public static int test_0_marshal_delegate () {
552 SimpleDelegate d = new SimpleDelegate (delegate_test);
554 return mono_test_marshal_delegate (d);
557 public static int test_34_marshal_instance_delegate () {
558 Tests t = new Tests ();
560 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
562 return mono_test_marshal_delegate (d);
565 /* Static delegates closed over their first argument */
566 public static int closed_delegate (Tests t, int a) {
567 return t.int_field + a;
570 public static int test_34_marshal_closed_static_delegate () {
571 Tests t = new Tests ();
573 SimpleDelegate d = (SimpleDelegate)Delegate.CreateDelegate (typeof (SimpleDelegate), t, typeof (Tests).GetMethod ("closed_delegate"));
575 return mono_test_marshal_delegate (d);
578 public static int test_0_marshal_return_delegate () {
579 SimpleDelegate d = new SimpleDelegate (delegate_test);
581 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
586 public static int test_0_marshal_delegate_struct () {
587 DelegateStruct s = new DelegateStruct ();
590 s.del = new SimpleDelegate (delegate_test);
591 s.del2 = new SimpleDelegate (delegate_test);
594 DelegateStruct res = mono_test_marshal_delegate_struct (s);
598 if (res.del (2) != 0)
600 if (res.del2 (2) != 0)
602 if (res.del3 != null)
608 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
609 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
611 public static int test_3_marshal_out_delegate () {
612 SimpleDelegate d = null;
614 mono_test_marshal_out_delegate (out d);
619 public static int test_0_marshal_byref_struct () {
620 SimpleStruct s = new SimpleStruct ();
627 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
630 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
635 public static int test_0_marshal_byref_struct_in () {
636 SimpleStruct s = new SimpleStruct ();
643 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
646 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
651 public static int test_0_marshal_byref_struct_inout () {
652 SimpleStruct s = new SimpleStruct ();
659 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
662 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
667 public static int test_0_marshal_point () {
668 Point pt = new Point();
672 return mono_test_marshal_point(pt);
675 public static int test_0_marshal_mixed_point () {
676 MixedPoint mpt = new MixedPoint();
680 return mono_test_marshal_mixed_point(mpt);
683 public static int test_0_marshal_bool_byref () {
685 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
688 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
696 public static int test_0_marshal_bool_as_I1 () {
700 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
703 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
712 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
724 public static int test_0_marshal_bool_as_U1 () {
728 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
731 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
740 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
752 public static int test_0_return_vtype () {
753 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
755 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
761 public static int test_0_marshal_stringbuilder () {
762 StringBuilder sb = new StringBuilder(255);
764 mono_test_marshal_stringbuilder (sb, sb.Capacity);
765 String res = sb.ToString();
767 if (res != "This is my message. Isn't it nice?")
770 // Test StringBuilder with default capacity (16)
771 StringBuilder sb2 = new StringBuilder();
772 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
773 if (sb2.ToString () != "This is my messa")
779 public static int test_0_marshal_stringbuilder_unicode () {
780 StringBuilder sb = new StringBuilder(255);
781 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
782 String res = sb.ToString();
784 if (res != "This is my message. Isn't it nice?")
787 // Test StringBuilder with default capacity (16)
788 StringBuilder sb2 = new StringBuilder();
789 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
790 if (sb2.ToString () != "This is my messa")
796 public static int test_0_marshal_stringbuilder_out () {
798 mono_test_marshal_stringbuilder_out (out sb);
800 if (sb.ToString () != "This is my message. Isn't it nice?")
805 public static int test_0_marshal_stringbuilder_out_unicode () {
807 mono_test_marshal_stringbuilder_out_unicode (out sb);
809 if (sb.ToString () != "This is my message. Isn't it nice?")
814 public static int test_0_marshal_empty_string_array () {
815 return mono_test_marshal_empty_string_array (null);
818 public static int test_0_marshal_string_array () {
819 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
822 public static int test_0_marshal_unicode_string_array () {
823 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
826 public static int test_0_marshal_stringbuilder_array () {
827 StringBuilder sb1 = new StringBuilder ("ABC");
828 StringBuilder sb2 = new StringBuilder ("DEF");
830 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
833 if (sb1.ToString () != "DEF")
835 if (sb2.ToString () != "ABC")
840 public static int test_0_last_error () {
841 mono_test_last_error (5);
842 if (Marshal.GetLastWin32Error () == 5)
848 public static int test_0_entry_point_not_found () {
851 mono_entry_point_not_found ();
854 catch (EntryPointNotFoundException) {
860 public static int test_0_library_not_found () {
863 mono_library_not_found ();
866 catch (DllNotFoundException) {
872 /* Check that the runtime trims .dll from the library name */
873 public static int test_0_trim_dll_from_name () {
875 mono_test_marshal_char_2 ('A');
880 /* Check that the runtime adds lib to to the library name */
881 public static int test_0_add_lib_to_name () {
883 mono_test_marshal_char_3 ('A');
892 public static int test_0_asany () {
893 if (mono_test_asany (5, 1) != 0)
896 if (mono_test_asany ("ABC", 2) != 0)
899 SimpleStruct2 ss2 = new SimpleStruct2 ();
907 if (mono_test_asany (ss2, 3) != 0)
910 if (mono_test_asany_unicode ("ABC", 4) != 0)
916 mono_test_asany (c, 0);
919 catch (ArgumentException) {
923 mono_test_asany (new Object (), 0);
926 catch (ArgumentException) {
932 /* AsAny marshalling + [In, Out] */
934 public static int test_0_asany_in () {
936 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
937 mono_test_asany_in (str);
940 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
941 mono_test_asany_in (cls);
942 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
946 object obj = new AsAnyStruct(1,2,3, "ABC");
947 mono_test_asany_in (obj);
948 str = (AsAnyStruct)obj;
949 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
955 public static int test_0_asany_out () {
957 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
958 mono_test_asany_out (str);
961 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
962 mono_test_asany_out (cls);
963 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
967 object obj = new AsAnyStruct(1,2,3, "ABC");
968 mono_test_asany_out (obj);
969 str = (AsAnyStruct)obj;
970 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
976 public static int test_0_asany_inout () {
978 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
979 mono_test_asany_inout (str);
982 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
983 mono_test_asany_inout (cls);
984 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
988 object obj = new AsAnyStruct(1,2,3, "ABC");
989 mono_test_asany_inout (obj);
990 str = (AsAnyStruct)obj;
991 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
997 /* Byref String Array */
999 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
1000 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
1002 public static int test_0_byref_string_array () {
1004 string[] arr = null;
1006 if (mono_test_marshal_byref_string_array (ref arr) != 0)
1009 arr = new string[] { "Alpha", "Beta", "Gamma" };
1011 if (mono_test_marshal_byref_string_array (ref arr) != 1)
1014 /* FIXME: Test returned array and out case */
1020 * AMD64 small structs-by-value tests.
1023 /* TEST 1: 16 byte long INTEGER struct */
1025 [StructLayout(LayoutKind.Sequential)]
1026 public struct Amd64Struct1 {
1033 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1034 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
1036 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1037 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);
1039 public static int test_0_amd64_struct1 () {
1040 Amd64Struct1 s = new Amd64Struct1 ();
1046 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
1048 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1051 public static int test_0_amd64_struct1_many_args () {
1052 Amd64Struct1 s = new Amd64Struct1 ();
1058 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
1060 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1063 /* TEST 2: 8 byte long INTEGER struct */
1065 [StructLayout(LayoutKind.Sequential)]
1066 public struct Amd64Struct2 {
1071 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1072 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1074 public static int test_0_amd64_struct2 () {
1075 Amd64Struct2 s = new Amd64Struct2 ();
1079 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1081 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1084 /* TEST 3: 4 byte long INTEGER struct */
1086 [StructLayout(LayoutKind.Sequential)]
1087 public struct Amd64Struct3 {
1091 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1092 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1094 public static int test_0_amd64_struct3 () {
1095 Amd64Struct3 s = new Amd64Struct3 ();
1098 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1100 return (s2.i == -4) ? 0 : 1;
1103 /* Test 4: 16 byte long FLOAT struct */
1105 [StructLayout(LayoutKind.Sequential)]
1106 public struct Amd64Struct4 {
1107 public double d1, d2;
1110 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1111 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1113 public static int test_0_amd64_struct4 () {
1114 Amd64Struct4 s = new Amd64Struct4 ();
1118 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1120 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1127 /* Test 5: Float HFA */
1129 [StructLayout(LayoutKind.Sequential)]
1130 public struct TestStruct5 {
1131 public float d1, d2;
1134 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1135 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1137 public static int test_0_ia64_struct5 () {
1138 TestStruct5 s = new TestStruct5 ();
1142 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1144 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1147 /* Test 6: Double HFA */
1149 [StructLayout(LayoutKind.Sequential)]
1150 public struct TestStruct6 {
1151 public double d1, d2;
1154 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1155 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1157 public static int test_0_ia64_struct6 () {
1158 TestStruct6 s = new TestStruct6 ();
1162 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1164 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1167 /* Blittable class */
1168 [DllImport("libtest")]
1169 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1171 public static int test_0_marshal_blittable_class () {
1172 BlittableClass v1 = new BlittableClass ();
1174 /* Since it is blittable, it looks like it is passed as in/out */
1175 BlittableClass v2 = TestBlittableClass (v1);
1177 if (v1.a != 2 || v1.b != 3)
1180 if (v2.a != 2 || v2.b != 3)
1184 BlittableClass v3 = TestBlittableClass (null);
1186 if (v3.a != 42 || v3.b != 43)
1193 * Generic structures
1196 [StructLayout(LayoutKind.Sequential)]
1197 public struct Amd64Struct1Gen<T> {
1204 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
1205 public static extern Amd64Struct1Gen<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen<int> s);
1207 public static int test_0_amd64_struct1_gen () {
1208 Amd64Struct1Gen<int> s = new Amd64Struct1Gen<int> ();
1214 Amd64Struct1Gen<int> s2 = mono_test_marshal_amd64_pass_return_struct1_gen (s);
1216 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
1223 public static int test_0_marshal_byval_class () {
1224 SimpleObj obj0 = new SimpleObj ();
1228 if (class_marshal_test0 (obj0) != 0)
1234 public static int test_0_marshal_byval_class_null () {
1235 if (class_marshal_test4 (null) != 0)
1241 public static int test_0_marshal_out_class () {
1244 class_marshal_test1 (out obj1);
1246 if (obj1.str != "ABC")
1255 public static int test_0_marshal_string () {
1256 return string_marshal_test0 ("TEST0");
1259 public static int test_0_marshal_out_string () {
1262 string_marshal_test1 (out res);
1270 public static int test_0_marshal_byref_string () {
1271 string res = "TEST1";
1273 return string_marshal_test2 (ref res);
1276 public static int test_0_marshal_null_string () {
1277 return string_marshal_test3 (null);
1280 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1281 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1283 public static int test_0_stdcall_name_mangling () {
1284 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1289 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1290 public static extern float mono_test_marshal_pass_return_float (float f);
1292 public static int test_0_pass_return_float () {
1293 float f = mono_test_marshal_pass_return_float (1.5f);
1295 return (f == 2.5f) ? 0 : 1;
1299 * Pointers to structures can not be passed
1302 public struct CharInfo {
1303 public char Character;
1304 public short Attributes;
1307 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1308 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1310 public static unsafe int test_0_marshal_ptr_to_struct () {
1311 CharInfo [] buffer = new CharInfo [1];
1312 fixed (CharInfo *ptr = &buffer [0]) {
1314 mono_test_marshal_ptr_to_struct (ptr);
1317 catch (MarshalDirectiveException) {
1325 * LPWStr marshalling
1328 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1329 [return: MarshalAs(UnmanagedType.LPWStr)]
1330 private static extern string mono_test_marshal_lpwstr_marshal(
1331 [MarshalAs(UnmanagedType.LPWStr)] string s,
1334 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1335 private static extern string mono_test_marshal_lpwstr_marshal2(
1339 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1340 private static extern void mono_test_marshal_lpwstr_out_marshal(
1341 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1343 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1344 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1347 public static int test_0_pass_return_lwpstr () {
1350 mono_test_marshal_lpwstr_out_marshal (out s);
1356 mono_test_marshal_lpwstr_out_marshal2 (out s);
1364 public static int test_0_out_lwpstr () {
1367 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1372 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1381 * Byref bool marshalling
1384 [DllImport("libtest")]
1385 extern static int marshal_test_ref_bool
1388 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1389 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1393 public static int test_0_pass_byref_bool () {
1394 for (int i = 0; i < 8; i++)
1396 bool b1 = (i & 4) != 0;
1397 bool b2 = (i & 2) != 0;
1398 bool b3 = (i & 1) != 0;
1399 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1400 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1414 * Bool struct field marshalling
1420 [MarshalAs(UnmanagedType.I1)] public bool b1;
1421 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1425 [DllImport("libtest")]
1426 extern static int marshal_test_bool_struct(ref BoolStruct s);
1428 public static int test_0_pass_bool_in_struct () {
1429 for (int i = 0; i < 8; i++)
1431 BoolStruct s = new BoolStruct();
1433 s.b1 = (i & 4) != 0;
1434 s.b2 = (i & 2) != 0;
1435 s.b3 = (i & 1) != 0;
1436 BoolStruct orig = s;
1437 if (marshal_test_bool_struct(ref s) != 0)
1439 if (s.b1 != !orig.b1)
1441 if (s.b2 != !orig.b2)
1443 if (s.b3 != !orig.b3)
1451 * Alignment of structs containing longs
1454 struct LongStruct2 {
1460 public LongStruct2 l;
1463 [DllImport("libtest")]
1464 extern static int mono_test_marshal_long_struct (ref LongStruct s);
1466 public static int test_47_pass_long_struct () {
1467 LongStruct s = new LongStruct ();
1469 s.l = new LongStruct2 ();
1472 return mono_test_marshal_long_struct (ref s);
1476 * Invoking pinvoke methods through delegates
1479 delegate int MyDelegate (string name);
1481 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1482 public static extern int puts_static (string name);
1484 public static int test_0_invoke_pinvoke_through_delegate () {
1485 puts_static ("A simple Test for PInvoke 1");
1487 MyDelegate d = new MyDelegate (puts_static);
1488 d ("A simple Test for PInvoke 2");
1490 object [] args = {"A simple Test for PInvoke 3"};
1491 d.DynamicInvoke (args);
1497 * Missing virtual pinvoke methods
1502 public virtual object MyClone ()
1508 public class T2 : T {
1509 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1510 public override extern object MyClone ();
1513 public static int test_0_missing_virtual_pinvoke_method () {
1518 } catch (Exception ex) {
1526 * Marshalling of type 'object'
1529 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
1530 public static extern SimpleClass mono_test_marshal_object (int i, int j, int k, object ss, int l);
1532 public static int test_0_marshal_object () {
1534 mono_test_marshal_object (0, 0, 0, null, 0);
1536 } catch (Exception) {