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 SimpleStruct2 {
45 [StructLayout (LayoutKind.Sequential, Size=0)]
46 public struct EmptyStruct {
49 [StructLayout (LayoutKind.Sequential)]
50 public struct DelegateStruct {
52 public SimpleDelegate del;
53 [MarshalAs(UnmanagedType.FunctionPtr)]
54 public SimpleDelegate del2;
57 /* sparcv9 has complex conventions when passing structs with doubles in them
58 by value, some simple tests for them */
59 [StructLayout (LayoutKind.Sequential)]
65 [StructLayout (LayoutKind.Sequential)]
66 public struct MixedPoint {
71 [StructLayout (LayoutKind.Sequential)]
72 public class SimpleClass {
83 [StructLayout (LayoutKind.Sequential)]
84 public class EmptyClass {
87 [StructLayout (LayoutKind.Sequential)]
88 public struct LongAlignStruct {
94 [StructLayout(LayoutKind.Sequential)]
95 public class BlittableClass
101 [StructLayout (LayoutKind.Sequential)]
108 [StructLayout(LayoutKind.Sequential)]
116 public AsAnyStruct(int i, int j, int k, String s) {
124 [StructLayout(LayoutKind.Sequential)]
132 public AsAnyClass(int i, int j, int k, String s) {
139 [DllImport ("libnot-found", EntryPoint="not_found")]
140 public static extern int mono_library_not_found ();
142 [DllImport ("libtest", EntryPoint="not_found")]
143 public static extern int mono_entry_point_not_found ();
145 [DllImport ("libtest.dll", EntryPoint="mono_test_marshal_char")]
146 public static extern int mono_test_marshal_char_2 (char a1);
148 [DllImport ("test", EntryPoint="mono_test_marshal_char")]
149 public static extern int mono_test_marshal_char_3 (char a1);
151 [DllImport ("libtest", EntryPoint="mono_test_marshal_char")]
152 public static extern int mono_test_marshal_char (char a1);
154 [DllImport ("libtest", EntryPoint="mono_test_marshal_char_array", CharSet=CharSet.Unicode)]
155 public static extern int mono_test_marshal_char_array (char[] a1);
157 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_byref")]
158 public static extern int mono_test_marshal_bool_byref (int a, ref bool b, int c);
160 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
161 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType.I1)] bool bTrue, [MarshalAs (UnmanagedType.I1)] bool bFalse);
163 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_in_as_I1_U1")]
164 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType.U1)] bool bTrue, [MarshalAs (UnmanagedType.U1)] bool bFalse);
166 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
167 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType.I1)] out bool bTrue, [MarshalAs (UnmanagedType.I1)] out bool bFalse);
169 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_out_as_I1_U1")]
170 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType.U1)] out bool bTrue, [MarshalAs (UnmanagedType.U1)] out bool bFalse);
172 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
173 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType.I1)] ref bool bTrue, [MarshalAs (UnmanagedType.I1)] ref bool bFalse);
175 [DllImport ("libtest", EntryPoint="mono_test_marshal_bool_ref_as_I1_U1")]
176 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType.U1)] ref bool bTrue, [MarshalAs (UnmanagedType.U1)] ref bool bFalse);
178 [DllImport ("libtest", EntryPoint="mono_test_marshal_array")]
179 public static extern int mono_test_marshal_array (int [] a1);
181 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
182 public static extern int mono_test_marshal_empty_string_array (string [] a1);
184 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
185 public static extern int mono_test_marshal_string_array (string [] a1);
187 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
188 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
190 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
191 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
193 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
194 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
196 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
197 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
199 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
200 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
202 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
203 public static extern int mono_test_marshal_struct (SimpleStruct ss);
205 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
206 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
208 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
209 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
211 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
212 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
214 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
215 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
217 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
218 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
220 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
221 public static extern int mono_test_marshal_point (Point p);
223 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
224 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
226 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
227 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
229 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
230 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
232 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
233 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
235 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
236 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
238 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
239 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
241 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
242 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
244 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
245 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
247 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
248 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
250 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
251 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
253 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
254 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
256 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
257 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
259 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
260 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
262 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
263 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
265 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
266 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
268 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
269 public static extern void mono_test_last_error (int err);
271 [DllImport ("libtest", EntryPoint="mono_test_asany")]
272 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
274 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
275 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
277 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
278 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
280 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
281 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
282 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
283 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
285 [DllImport ("libtest")]
286 static extern int class_marshal_test0 (SimpleObj obj);
288 [DllImport ("libtest")]
289 static extern void class_marshal_test1 (out SimpleObj obj);
291 [DllImport ("libtest")]
292 static extern int class_marshal_test4 (SimpleObj obj);
294 [DllImport ("libtest")]
295 static extern int string_marshal_test0 (string str);
297 [DllImport ("libtest")]
298 static extern void string_marshal_test1 (out string str);
300 [DllImport ("libtest")]
301 static extern int string_marshal_test2 (ref string str);
303 [DllImport ("libtest")]
304 static extern int string_marshal_test3 (string str);
306 public delegate int SimpleDelegate (int a);
308 public static int Main (string[] args) {
309 return TestDriver.RunTests (typeof (Tests), args);
312 public static int test_0_marshal_char () {
313 return mono_test_marshal_char ('a');
316 public static int test_0_marshal_char_array () {
317 // a unicode char[] is implicitly marshalled as [Out]
318 char[] buf = new char [32];
319 mono_test_marshal_char_array (buf);
320 string s = new string (buf);
321 if (s.StartsWith ("abcdef"))
327 public static int test_1225_marshal_array () {
328 int [] a1 = new int [50];
329 for (int i = 0; i < 50; i++)
332 return mono_test_marshal_array (a1);
335 public static int test_1225_marshal_inout_array () {
336 int [] a1 = new int [50];
337 for (int i = 0; i < 50; i++)
340 int res = mono_test_marshal_inout_array (a1);
342 for (int i = 0; i < 50; i++)
343 if (a1 [i] != 50 - i) {
344 Console.WriteLine ("X: " + i + " " + a1 [i]);
351 public static int test_0_marshal_out_array () {
352 int [] a1 = new int [50];
354 int res = mono_test_marshal_out_array (a1, 0);
356 for (int i = 0; i < 50; i++)
358 Console.WriteLine ("X: " + i + " " + a1 [i]);
365 public static int test_0_marshal_inout_nonblittable_array () {
366 char [] a1 = new char [10];
367 for (int i = 0; i < 10; i++)
368 a1 [i] = "Hello, World" [i];
370 int res = mono_test_marshal_inout_nonblittable_array (a1);
372 for (int i = 0; i < 10; i++)
379 public static int test_0_marshal_struct () {
380 SimpleStruct ss = new SimpleStruct ();
384 return mono_test_marshal_struct (ss);
387 public static int test_0_marshal_struct2 () {
388 SimpleStruct2 ss2 = new SimpleStruct2 ();
396 return mono_test_marshal_struct2 (ss2);
399 public static int test_0_marshal_struct3 () {
400 SimpleStruct2 ss2 = new SimpleStruct2 ();
408 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
411 public static int test_0_marshal_empty_struct () {
412 EmptyStruct es = new EmptyStruct ();
414 if (mono_test_empty_struct (1, es, 2) != 0)
420 public static int test_0_marshal_lpstruct () {
421 SimpleStruct ss = new SimpleStruct ();
425 return mono_test_marshal_lpstruct (ss);
428 public static int test_0_marshal_lpstruct_blittable () {
429 Point p = new Point ();
433 return mono_test_marshal_lpstruct_blittable (p);
436 public static int test_0_marshal_struct_array () {
437 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
439 SimpleStruct2 ss2 = new SimpleStruct2 ();
458 return mono_test_marshal_struct_array (ss_arr);
461 public static int test_105_marshal_long_align_struct_array () {
462 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
464 LongAlignStruct ss = new LongAlignStruct ();
477 return mono_test_marshal_long_align_struct_array (ss_arr);
480 /* Test classes as arguments and return values */
481 public static int test_0_marshal_class () {
482 SimpleClass ss = new SimpleClass ();
490 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
493 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
494 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
495 res.g == ss.g && res.h == ss.h))
498 /* Test null arguments and results */
499 res = mono_test_marshal_class (10, 11, 12, null, 14);
506 public static int test_0_marshal_byref_class () {
507 SimpleClass ss = new SimpleClass ();
515 int res = mono_test_marshal_byref_class (ref ss);
516 if (ss.d != "TEST-RES")
522 public static int test_0_marshal_delegate () {
523 SimpleDelegate d = new SimpleDelegate (delegate_test);
525 return mono_test_marshal_delegate (d);
528 public static int test_34_marshal_instance_delegate () {
529 Tests t = new Tests ();
531 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
533 return mono_test_marshal_delegate (d);
536 public static int test_0_marshal_return_delegate () {
537 SimpleDelegate d = new SimpleDelegate (delegate_test);
539 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
544 public static int test_0_marshal_delegate_struct () {
545 DelegateStruct s = new DelegateStruct ();
548 s.del = new SimpleDelegate (delegate_test);
549 s.del2 = new SimpleDelegate (delegate_test);
551 DelegateStruct res = mono_test_marshal_delegate_struct (s);
555 if (res.del (2) != 0)
557 if (res.del2 (2) != 0)
563 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_delegate")]
564 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d);
566 public static int test_3_marshal_out_delegate () {
567 SimpleDelegate d = null;
569 mono_test_marshal_out_delegate (out d);
574 public static int test_0_marshal_byref_struct () {
575 SimpleStruct s = new SimpleStruct ();
582 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
585 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
590 public static int test_0_marshal_byref_struct_in () {
591 SimpleStruct s = new SimpleStruct ();
598 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
601 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
606 public static int test_0_marshal_byref_struct_inout () {
607 SimpleStruct s = new SimpleStruct ();
614 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
617 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
622 public static int test_0_marshal_point () {
623 Point pt = new Point();
627 return mono_test_marshal_point(pt);
630 public static int test_0_marshal_mixed_point () {
631 MixedPoint mpt = new MixedPoint();
635 return mono_test_marshal_mixed_point(mpt);
638 public static int test_0_marshal_bool_byref () {
640 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
643 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
651 public static int test_0_marshal_bool_as_I1 () {
655 if ((ret = mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
658 if ((ret = mono_test_marshal_bool_out_as_I1 (out bTrue, out bFalse)) != 0)
667 if ((ret = mono_test_marshal_bool_ref_as_I1 (ref bTrue, ref bFalse)) != 0)
679 public static int test_0_marshal_bool_as_U1 () {
683 if ((ret = mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
686 if ((ret = mono_test_marshal_bool_out_as_U1 (out bTrue, out bFalse)) != 0)
695 if ((ret = mono_test_marshal_bool_ref_as_U1 (ref bTrue, ref bFalse)) != 0)
707 public static int test_0_return_vtype () {
708 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
710 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
716 public static int test_0_marshal_stringbuilder () {
717 StringBuilder sb = new StringBuilder(255);
719 mono_test_marshal_stringbuilder (sb, sb.Capacity);
720 String res = sb.ToString();
722 if (res != "This is my message. Isn't it nice?")
725 // Test StringBuilder with default capacity (16)
726 StringBuilder sb2 = new StringBuilder();
727 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
728 if (sb2.ToString () != "This is my messa")
734 public static int test_0_marshal_stringbuilder_unicode () {
735 StringBuilder sb = new StringBuilder(255);
736 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
737 String res = sb.ToString();
739 if (res != "This is my message. Isn't it nice?")
742 // Test StringBuilder with default capacity (16)
743 StringBuilder sb2 = new StringBuilder();
744 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
745 if (sb2.ToString () != "This is my messa")
751 public static int test_0_marshal_empty_string_array () {
752 return mono_test_marshal_empty_string_array (null);
755 public static int test_0_marshal_string_array () {
756 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
759 public static int test_0_marshal_unicode_string_array () {
760 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
763 public static int test_0_marshal_stringbuilder_array () {
764 StringBuilder sb1 = new StringBuilder ("ABC");
765 StringBuilder sb2 = new StringBuilder ("DEF");
767 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
770 if (sb1.ToString () != "DEF")
772 if (sb2.ToString () != "ABC")
777 public static int test_0_last_error () {
778 mono_test_last_error (5);
779 if (Marshal.GetLastWin32Error () == 5)
785 public static int test_0_entry_point_not_found () {
788 mono_entry_point_not_found ();
791 catch (EntryPointNotFoundException) {
797 public static int test_0_library_not_found () {
800 mono_library_not_found ();
803 catch (DllNotFoundException) {
809 /* Check that the runtime trims .dll from the library name */
810 public static int test_0_trim_dll_from_name () {
812 mono_test_marshal_char_2 ('A');
817 /* Check that the runtime adds lib to to the library name */
818 public static int test_0_add_lib_to_name () {
820 mono_test_marshal_char_3 ('A');
829 public static int test_0_asany () {
830 if (mono_test_asany (5, 1) != 0)
833 if (mono_test_asany ("ABC", 2) != 0)
836 SimpleStruct2 ss2 = new SimpleStruct2 ();
844 if (mono_test_asany (ss2, 3) != 0)
847 if (mono_test_asany_unicode ("ABC", 4) != 0)
853 mono_test_asany (c, 0);
856 catch (ArgumentException) {
860 mono_test_asany (new Object (), 0);
863 catch (ArgumentException) {
869 /* AsAny marshalling + [In, Out] */
871 public static int test_0_asany_in () {
873 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
874 mono_test_asany_in (str);
877 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
878 mono_test_asany_in (cls);
879 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
883 object obj = new AsAnyStruct(1,2,3, "ABC");
884 mono_test_asany_in (obj);
885 str = (AsAnyStruct)obj;
886 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
892 public static int test_0_asany_out () {
894 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
895 mono_test_asany_out (str);
898 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
899 mono_test_asany_out (cls);
900 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
904 object obj = new AsAnyStruct(1,2,3, "ABC");
905 mono_test_asany_out (obj);
906 str = (AsAnyStruct)obj;
907 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
913 public static int test_0_asany_inout () {
915 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
916 mono_test_asany_inout (str);
919 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
920 mono_test_asany_inout (cls);
921 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
925 object obj = new AsAnyStruct(1,2,3, "ABC");
926 mono_test_asany_inout (obj);
927 str = (AsAnyStruct)obj;
928 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
934 /* Byref String Array */
936 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
937 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
939 public static int test_0_byref_string_array () {
943 if (mono_test_marshal_byref_string_array (ref arr) != 0)
946 arr = new string[] { "Alpha", "Beta", "Gamma" };
948 if (mono_test_marshal_byref_string_array (ref arr) != 1)
951 /* FIXME: Test returned array and out case */
957 * AMD64 small structs-by-value tests.
960 /* TEST 1: 16 byte long INTEGER struct */
962 [StructLayout(LayoutKind.Sequential)]
963 public struct Amd64Struct1 {
970 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
971 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
973 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1_many_args")]
974 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);
976 public static int test_0_amd64_struct1 () {
977 Amd64Struct1 s = new Amd64Struct1 ();
983 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
985 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
988 public static int test_0_amd64_struct1_many_args () {
989 Amd64Struct1 s = new Amd64Struct1 ();
995 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1_many_args (s, 1, 2, 3, 4, 5, 6, 7, 8);
997 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1000 /* TEST 2: 8 byte long INTEGER struct */
1002 [StructLayout(LayoutKind.Sequential)]
1003 public struct Amd64Struct2 {
1008 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
1009 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
1011 public static int test_0_amd64_struct2 () {
1012 Amd64Struct2 s = new Amd64Struct2 ();
1016 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
1018 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
1021 /* TEST 3: 4 byte long INTEGER struct */
1023 [StructLayout(LayoutKind.Sequential)]
1024 public struct Amd64Struct3 {
1028 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
1029 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
1031 public static int test_0_amd64_struct3 () {
1032 Amd64Struct3 s = new Amd64Struct3 ();
1035 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
1037 return (s2.i == -4) ? 0 : 1;
1040 /* Test 4: 16 byte long FLOAT struct */
1042 [StructLayout(LayoutKind.Sequential)]
1043 public struct Amd64Struct4 {
1044 public double d1, d2;
1047 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
1048 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
1050 public static int test_0_amd64_struct4 () {
1051 Amd64Struct4 s = new Amd64Struct4 ();
1055 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
1057 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
1064 /* Test 5: Float HFA */
1066 [StructLayout(LayoutKind.Sequential)]
1067 public struct TestStruct5 {
1068 public float d1, d2;
1071 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
1072 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, int i, double f3, double f4);
1074 public static int test_0_ia64_struct5 () {
1075 TestStruct5 s = new TestStruct5 ();
1079 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 5, 3.0, 4.0);
1081 return (s2.d1 == 13.0 && s2.d2 == 7.0) ? 0 : 1;
1084 /* Test 6: Double HFA */
1086 [StructLayout(LayoutKind.Sequential)]
1087 public struct TestStruct6 {
1088 public double d1, d2;
1091 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
1092 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, int i, double f3, double f4);
1094 public static int test_0_ia64_struct6 () {
1095 TestStruct6 s = new TestStruct6 ();
1099 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3, 4.0, 5.0);
1101 return (s2.d1 == 12.0 && s2.d2 == 3.0) ? 0 : 1;
1104 /* Blittable class */
1105 [DllImport("libtest")]
1106 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1108 public static int test_0_marshal_blittable_class () {
1109 BlittableClass v1 = new BlittableClass ();
1111 /* Since it is blittable, it looks like it is passed as in/out */
1112 BlittableClass v2 = TestBlittableClass (v1);
1114 if (v1.a != 2 || v1.b != 3)
1117 if (v2.a != 2 || v2.b != 3)
1121 BlittableClass v3 = TestBlittableClass (null);
1123 if (v3.a != 42 || v3.b != 43)
1129 public static int test_0_marshal_byval_class () {
1130 SimpleObj obj0 = new SimpleObj ();
1134 if (class_marshal_test0 (obj0) != 0)
1140 public static int test_0_marshal_byval_class_null () {
1141 if (class_marshal_test4 (null) != 0)
1147 public static int test_0_marshal_out_class () {
1150 class_marshal_test1 (out obj1);
1152 if (obj1.str != "ABC")
1161 public static int test_0_marshal_string () {
1162 return string_marshal_test0 ("TEST0");
1165 public static int test_0_marshal_out_string () {
1168 string_marshal_test1 (out res);
1176 public static int test_0_marshal_byref_string () {
1177 string res = "TEST1";
1179 return string_marshal_test2 (ref res);
1182 public static int test_0_marshal_null_string () {
1183 return string_marshal_test3 (null);
1186 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1187 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1189 public static int test_0_stdcall_name_mangling () {
1190 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1195 [DllImport ("libtest", EntryPoint="mono_test_marshal_pass_return_float")]
1196 public static extern float mono_test_marshal_pass_return_float (float f);
1198 public static int test_0_pass_return_float () {
1199 float f = mono_test_marshal_pass_return_float (1.5f);
1201 return (f == 2.5f) ? 0 : 1;
1205 * Pointers to structures can not be passed
1208 public struct CharInfo {
1209 public char Character;
1210 public short Attributes;
1213 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1214 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1216 public static unsafe int test_0_marshal_ptr_to_struct () {
1217 CharInfo [] buffer = new CharInfo [1];
1218 fixed (CharInfo *ptr = &buffer [0]) {
1220 mono_test_marshal_ptr_to_struct (ptr);
1223 catch (MarshalDirectiveException) {
1231 * LPWStr marshalling
1234 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1235 [return: MarshalAs(UnmanagedType.LPWStr)]
1236 private static extern string mono_test_marshal_lpwstr_marshal(
1237 [MarshalAs(UnmanagedType.LPWStr)] string s,
1240 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1241 private static extern string mono_test_marshal_lpwstr_marshal2(
1245 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out")]
1246 private static extern void mono_test_marshal_lpwstr_out_marshal(
1247 [MarshalAs(UnmanagedType.LPWStr)] out string s);
1249 [DllImport("libtest", EntryPoint="test_lpwstr_marshal_out", CharSet=CharSet.Unicode)]
1250 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1253 public static int test_0_pass_return_lwpstr () {
1256 mono_test_marshal_lpwstr_out_marshal (out s);
1262 mono_test_marshal_lpwstr_out_marshal2 (out s);
1270 public static int test_0_out_lwpstr () {
1273 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1278 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1287 * Byref bool marshalling
1290 [DllImport("libtest")]
1291 extern static int marshal_test_ref_bool
1294 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1295 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1299 public static int test_0_pass_byref_bool () {
1300 for (int i = 0; i < 8; i++)
1302 bool b1 = (i & 4) != 0;
1303 bool b2 = (i & 2) != 0;
1304 bool b3 = (i & 1) != 0;
1305 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1306 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1320 * Bool struct field marshalling
1326 [MarshalAs(UnmanagedType.I1)] public bool b1;
1327 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1331 [DllImport("libtest")]
1332 extern static int marshal_test_bool_struct(ref BoolStruct s);
1334 public static int test_0_pass_bool_in_struct () {
1335 for (int i = 0; i < 8; i++)
1337 BoolStruct s = new BoolStruct();
1339 s.b1 = (i & 4) != 0;
1340 s.b2 = (i & 2) != 0;
1341 s.b3 = (i & 1) != 0;
1342 BoolStruct orig = s;
1343 if (marshal_test_bool_struct(ref s) != 0)
1345 if (s.b1 != !orig.b1)
1347 if (s.b2 != !orig.b2)
1349 if (s.b3 != !orig.b3)
1357 * Invoking pinvoke methods through delegates
1360 delegate int MyDelegate (string name);
1362 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1363 public static extern int puts_static (string name);
1365 public static int test_0_invoke_pinvoke_through_delegate () {
1366 puts_static ("A simple Test for PInvoke 1");
1368 MyDelegate d = new MyDelegate (puts_static);
1369 d ("A simple Test for PInvoke 2");
1371 object [] args = {"A simple Test for PInvoke 3"};
1372 d.DynamicInvoke (args);
1378 * Missing virtual pinvoke methods
1383 public virtual object MyClone ()
1389 public class T2 : T {
1390 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1391 public override extern object MyClone ();
1394 public static int test_0_missing_virtual_pinvoke_method () {
1399 } catch (Exception ex) {