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_array")]
161 public static extern int mono_test_marshal_array (int [] a1);
163 [DllImport ("libtest", EntryPoint="mono_test_marshal_empty_string_array")]
164 public static extern int mono_test_marshal_empty_string_array (string [] a1);
166 [DllImport ("libtest", EntryPoint="mono_test_marshal_string_array")]
167 public static extern int mono_test_marshal_string_array (string [] a1);
169 [DllImport ("libtest", EntryPoint="mono_test_marshal_unicode_string_array", CharSet=CharSet.Unicode)]
170 public static extern int mono_test_marshal_unicode_string_array (string [] a1, [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)]string [] a2);
172 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_array")]
173 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder [] a1);
175 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_array")]
176 public static extern int mono_test_marshal_inout_array ([In, Out] int [] a1);
178 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_array")]
179 public static extern int mono_test_marshal_out_array ([Out] [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] int [] a1, int n);
181 [DllImport ("libtest", EntryPoint="mono_test_marshal_inout_nonblittable_array", CharSet = CharSet.Unicode)]
182 public static extern int mono_test_marshal_inout_nonblittable_array ([In, Out] char [] a1);
184 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
185 public static extern int mono_test_marshal_struct (SimpleStruct ss);
187 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2")]
188 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss);
190 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct2_2")]
191 public static extern int mono_test_marshal_struct2_2 (int i, int j, int k, SimpleStruct2 ss);
193 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
194 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss, bool a, bool b, bool c, String d);
196 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
197 public static extern int mono_test_marshal_byref_struct_in ([In] ref SimpleStruct ss, bool a, bool b, bool c, String d);
199 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_struct")]
200 public static extern int mono_test_marshal_byref_struct_inout ([In, Out] ref SimpleStruct ss, bool a, bool b, bool c, String d);
202 [DllImport ("libtest", EntryPoint="mono_test_marshal_point")]
203 public static extern int mono_test_marshal_point (Point p);
205 [DllImport ("libtest", EntryPoint="mono_test_marshal_mixed_point")]
206 public static extern int mono_test_marshal_mixed_point (MixedPoint p);
208 [DllImport ("libtest", EntryPoint="mono_test_empty_struct")]
209 public static extern int mono_test_empty_struct (int a, EmptyStruct es, int b);
211 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct")]
212 public static extern int mono_test_marshal_lpstruct ([In, MarshalAs(UnmanagedType.LPStruct)] SimpleStruct ss);
214 [DllImport ("libtest", EntryPoint="mono_test_marshal_lpstruct_blittable")]
215 public static extern int mono_test_marshal_lpstruct_blittable ([In, MarshalAs(UnmanagedType.LPStruct)] Point p);
217 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct_array")]
218 public static extern int mono_test_marshal_struct_array (SimpleStruct2[] ss);
220 [DllImport ("libtest", EntryPoint="mono_test_marshal_long_align_struct_array")]
221 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct[] ss);
223 [DllImport ("libtest", EntryPoint="mono_test_marshal_class")]
224 public static extern SimpleClass mono_test_marshal_class (int i, int j, int k, SimpleClass ss, int l);
226 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_class")]
227 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss);
229 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
230 public static extern int mono_test_marshal_delegate (SimpleDelegate d);
232 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate_struct")]
233 public static extern DelegateStruct mono_test_marshal_delegate_struct (DelegateStruct d);
235 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
236 public static extern SimpleDelegate mono_test_marshal_return_delegate (SimpleDelegate d);
238 [DllImport ("libtest", EntryPoint="mono_test_return_vtype")]
239 public static extern SimpleStruct mono_test_return_vtype (IntPtr i);
241 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder")]
242 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb, int len);
244 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_default")]
245 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb, int len);
247 [DllImport ("libtest", EntryPoint="mono_test_marshal_stringbuilder_unicode", CharSet=CharSet.Unicode)]
248 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb, int len);
250 [DllImport ("libtest", EntryPoint="mono_test_last_error", SetLastError=true)]
251 public static extern void mono_test_last_error (int err);
253 [DllImport ("libtest", EntryPoint="mono_test_asany")]
254 public static extern int mono_test_asany ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
256 [DllImport ("libtest", EntryPoint="mono_test_asany", CharSet=CharSet.Unicode)]
257 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType.AsAny)] object o, int what);
259 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_in")]
260 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType.AsAny)][In] object obj);
262 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_out")]
263 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType.AsAny)][Out] object obj);
264 [DllImport("libtest", EntryPoint="mono_test_marshal_asany_inout")]
265 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType.AsAny)][In, Out] object obj);
267 [DllImport ("libtest")]
268 static extern int class_marshal_test0 (SimpleObj obj);
270 [DllImport ("libtest")]
271 static extern void class_marshal_test1 (out SimpleObj obj);
273 [DllImport ("libtest")]
274 static extern int class_marshal_test4 (SimpleObj obj);
276 [DllImport ("libtest")]
277 static extern int string_marshal_test0 (string str);
279 [DllImport ("libtest")]
280 static extern void string_marshal_test1 (out string str);
282 [DllImport ("libtest")]
283 static extern int string_marshal_test2 (ref string str);
285 [DllImport ("libtest")]
286 static extern int string_marshal_test3 (string str);
288 public delegate int SimpleDelegate (int a);
290 public static int Main (string[] args) {
291 return TestDriver.RunTests (typeof (Tests), args);
294 public static int test_0_marshal_char () {
295 return mono_test_marshal_char ('a');
298 public static int test_0_marshal_char_array () {
299 // a unicode char[] is implicitly marshalled as [Out]
300 char[] buf = new char [32];
301 mono_test_marshal_char_array (buf);
302 string s = new string (buf);
303 if (s.StartsWith ("abcdef"))
309 public static int test_1225_marshal_array () {
310 int [] a1 = new int [50];
311 for (int i = 0; i < 50; i++)
314 return mono_test_marshal_array (a1);
317 public static int test_1225_marshal_inout_array () {
318 int [] a1 = new int [50];
319 for (int i = 0; i < 50; i++)
322 int res = mono_test_marshal_inout_array (a1);
324 for (int i = 0; i < 50; i++)
325 if (a1 [i] != 50 - i) {
326 Console.WriteLine ("X: " + i + " " + a1 [i]);
333 public static int test_0_marshal_out_array () {
334 int [] a1 = new int [50];
336 int res = mono_test_marshal_out_array (a1, 0);
338 for (int i = 0; i < 50; i++)
340 Console.WriteLine ("X: " + i + " " + a1 [i]);
347 public static int test_0_marshal_inout_nonblittable_array () {
348 char [] a1 = new char [10];
349 for (int i = 0; i < 10; i++)
350 a1 [i] = "Hello, World" [i];
352 int res = mono_test_marshal_inout_nonblittable_array (a1);
354 for (int i = 0; i < 10; i++)
361 public static int test_0_marshal_struct () {
362 SimpleStruct ss = new SimpleStruct ();
366 return mono_test_marshal_struct (ss);
369 public static int test_0_marshal_struct2 () {
370 SimpleStruct2 ss2 = new SimpleStruct2 ();
378 return mono_test_marshal_struct2 (ss2);
381 public static int test_0_marshal_struct3 () {
382 SimpleStruct2 ss2 = new SimpleStruct2 ();
390 return mono_test_marshal_struct2_2 (10, 11, 12, ss2);
393 public static int test_0_marshal_empty_struct () {
394 EmptyStruct es = new EmptyStruct ();
396 if (mono_test_empty_struct (1, es, 2) != 0)
402 public static int test_0_marshal_lpstruct () {
403 SimpleStruct ss = new SimpleStruct ();
407 return mono_test_marshal_lpstruct (ss);
410 public static int test_0_marshal_lpstruct_blittable () {
411 Point p = new Point ();
415 return mono_test_marshal_lpstruct_blittable (p);
418 public static int test_0_marshal_struct_array () {
419 SimpleStruct2[] ss_arr = new SimpleStruct2 [2];
421 SimpleStruct2 ss2 = new SimpleStruct2 ();
440 return mono_test_marshal_struct_array (ss_arr);
443 public static int test_105_marshal_long_align_struct_array () {
444 LongAlignStruct[] ss_arr = new LongAlignStruct [2];
446 LongAlignStruct ss = new LongAlignStruct ();
459 return mono_test_marshal_long_align_struct_array (ss_arr);
462 /* Test classes as arguments and return values */
463 public static int test_0_marshal_class () {
464 SimpleClass ss = new SimpleClass ();
472 SimpleClass res = mono_test_marshal_class (10, 11, 12, ss, 14);
475 if (! (res.a == ss.a && res.b == ss.b && res.c == ss.c &&
476 res.d == ss.d && res.e == ss.e && res.f == ss.f &&
477 res.g == ss.g && res.h == ss.h))
480 /* Test null arguments and results */
481 res = mono_test_marshal_class (10, 11, 12, null, 14);
488 public static int test_0_marshal_byref_class () {
489 SimpleClass ss = new SimpleClass ();
497 int res = mono_test_marshal_byref_class (ref ss);
498 if (ss.d != "TEST-RES")
504 public static int test_0_marshal_delegate () {
505 SimpleDelegate d = new SimpleDelegate (delegate_test);
507 return mono_test_marshal_delegate (d);
510 public static int test_34_marshal_instance_delegate () {
511 Tests t = new Tests ();
513 SimpleDelegate d = new SimpleDelegate (t.delegate_test_instance);
515 return mono_test_marshal_delegate (d);
518 public static int test_0_marshal_return_delegate () {
519 SimpleDelegate d = new SimpleDelegate (delegate_test);
521 SimpleDelegate d2 = mono_test_marshal_return_delegate (d);
526 public static int test_0_marshal_delegate_struct () {
527 DelegateStruct s = new DelegateStruct ();
530 s.del = new SimpleDelegate (delegate_test);
531 s.del2 = new SimpleDelegate (delegate_test);
533 DelegateStruct res = mono_test_marshal_delegate_struct (s);
537 if (res.del (2) != 0)
539 if (res.del2 (2) != 0)
545 public static int test_0_marshal_byref_struct () {
546 SimpleStruct s = new SimpleStruct ();
553 int res = mono_test_marshal_byref_struct (ref s, true, false, true, "ABC");
556 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
561 public static int test_0_marshal_byref_struct_in () {
562 SimpleStruct s = new SimpleStruct ();
569 int res = mono_test_marshal_byref_struct_in (ref s, true, false, true, "ABC");
572 if (s.a != true || s.b != false || s.c != true || s.d != "ABC")
577 public static int test_0_marshal_byref_struct_inout () {
578 SimpleStruct s = new SimpleStruct ();
585 int res = mono_test_marshal_byref_struct_inout (ref s, true, false, true, "ABC");
588 if (s.a != false || s.b != true || s.c != false || s.d != "DEF")
593 public static int test_0_marshal_point () {
594 Point pt = new Point();
598 return mono_test_marshal_point(pt);
601 public static int test_0_marshal_mixed_point () {
602 MixedPoint mpt = new MixedPoint();
606 return mono_test_marshal_mixed_point(mpt);
609 public static int test_0_marshal_bool_byref () {
611 if (mono_test_marshal_bool_byref (99, ref b, 100) != 1)
614 if (mono_test_marshal_bool_byref (99, ref b, 100) != 0)
622 public static int test_0_return_vtype () {
623 SimpleStruct ss = mono_test_return_vtype (new IntPtr (5));
625 if (!ss.a && ss.b && !ss.c && ss.d == "TEST" && ss.d2 == "TEST2")
631 public static int test_0_marshal_stringbuilder () {
632 StringBuilder sb = new StringBuilder(255);
634 mono_test_marshal_stringbuilder (sb, sb.Capacity);
635 String res = sb.ToString();
637 if (res != "This is my message. Isn't it nice?")
640 // Test StringBuilder with default capacity (16)
641 StringBuilder sb2 = new StringBuilder();
642 mono_test_marshal_stringbuilder_default (sb2, sb2.Capacity);
643 if (sb2.ToString () != "This is my messa")
649 public static int test_0_marshal_stringbuilder_unicode () {
650 StringBuilder sb = new StringBuilder(255);
651 mono_test_marshal_stringbuilder_unicode (sb, sb.Capacity);
652 String res = sb.ToString();
654 if (res != "This is my message. Isn't it nice?")
657 // Test StringBuilder with default capacity (16)
658 StringBuilder sb2 = new StringBuilder();
659 mono_test_marshal_stringbuilder_unicode (sb2, sb2.Capacity);
660 if (sb2.ToString () != "This is my messa")
666 public static int test_0_marshal_empty_string_array () {
667 return mono_test_marshal_empty_string_array (null);
670 public static int test_0_marshal_string_array () {
671 return mono_test_marshal_string_array (new String [] { "ABC", "DEF" });
674 public static int test_0_marshal_unicode_string_array () {
675 return mono_test_marshal_unicode_string_array (new String [] { "ABC", "DEF" }, new String [] { "ABC", "DEF" });
678 public static int test_0_marshal_stringbuilder_array () {
679 StringBuilder sb1 = new StringBuilder ("ABC");
680 StringBuilder sb2 = new StringBuilder ("DEF");
682 int res = mono_test_marshal_stringbuilder_array (new StringBuilder [] { sb1, sb2 });
685 if (sb1.ToString () != "DEF")
687 if (sb2.ToString () != "ABC")
692 public static int test_0_last_error () {
693 mono_test_last_error (5);
694 if (Marshal.GetLastWin32Error () == 5)
700 public static int test_0_library_not_found () {
703 mono_entry_point_not_found ();
706 catch (EntryPointNotFoundException) {
712 public static int test_0_entry_point_not_found () {
715 mono_library_not_found ();
718 catch (DllNotFoundException) {
724 /* Check that the runtime trims .dll from the library name */
725 public static int test_0_trim_dll_from_name () {
727 mono_test_marshal_char_2 ('A');
732 /* Check that the runtime adds lib to to the library name */
733 public static int test_0_add_lib_to_name () {
735 mono_test_marshal_char_3 ('A');
744 public static int test_0_asany () {
745 if (mono_test_asany (5, 1) != 0)
748 if (mono_test_asany ("ABC", 2) != 0)
751 SimpleStruct2 ss2 = new SimpleStruct2 ();
759 if (mono_test_asany (ss2, 3) != 0)
762 if (mono_test_asany_unicode ("ABC", 4) != 0)
768 mono_test_asany (c, 0);
771 catch (ArgumentException) {
775 mono_test_asany (new Object (), 0);
778 catch (ArgumentException) {
784 /* AsAny marshalling + [In, Out] */
786 public static int test_0_asany_in () {
788 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
789 mono_test_asany_in (str);
792 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
793 mono_test_asany_in (cls);
794 if ((cls.i != 1) || (cls.j != 2) || (cls.k != 3))
798 object obj = new AsAnyStruct(1,2,3, "ABC");
799 mono_test_asany_in (obj);
800 str = (AsAnyStruct)obj;
801 if ((str.i != 1) || (str.j != 2) || (str.k != 3))
807 public static int test_0_asany_out () {
809 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
810 mono_test_asany_out (str);
813 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
814 mono_test_asany_out (cls);
815 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
819 object obj = new AsAnyStruct(1,2,3, "ABC");
820 mono_test_asany_out (obj);
821 str = (AsAnyStruct)obj;
822 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
828 public static int test_0_asany_inout () {
830 AsAnyStruct str = new AsAnyStruct(1,2,3, "ABC");
831 mono_test_asany_inout (str);
834 AsAnyClass cls = new AsAnyClass(1,2,3, "ABC");
835 mono_test_asany_inout (cls);
836 if ((cls.i != 10) || (cls.j != 20) || (cls.k != 30))
840 object obj = new AsAnyStruct(1,2,3, "ABC");
841 mono_test_asany_inout (obj);
842 str = (AsAnyStruct)obj;
843 if ((str.i != 10) || (str.j != 20) || (str.k != 30))
849 /* Byref String Array */
851 [DllImport ("libtest", EntryPoint="mono_test_marshal_byref_string_array")]
852 public static extern int mono_test_marshal_byref_string_array (ref string[] data);
854 public static int test_0_byref_string_array () {
858 if (mono_test_marshal_byref_string_array (ref arr) != 0)
861 arr = new string[] { "Alpha", "Beta", "Gamma" };
863 if (mono_test_marshal_byref_string_array (ref arr) != 1)
866 /* FIXME: Test returned array and out case */
872 * AMD64 small structs-by-value tests.
875 /* TEST 1: 16 byte long INTEGER struct */
877 [StructLayout(LayoutKind.Sequential)]
878 public struct Amd64Struct1 {
885 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct1")]
886 public static extern Amd64Struct1 mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s);
888 public static int test_0_amd64_struct1 () {
889 Amd64Struct1 s = new Amd64Struct1 ();
895 Amd64Struct1 s2 = mono_test_marshal_amd64_pass_return_struct1 (s);
897 return ((s2.i == 6) && (s2.j == -4) && (s2.k == 0x1000000) && (s2.l == 0x10000000)) ? 0 : 1;
900 /* TEST 2: 8 byte long INTEGER struct */
902 [StructLayout(LayoutKind.Sequential)]
903 public struct Amd64Struct2 {
908 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct2")]
909 public static extern Amd64Struct2 mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s);
911 public static int test_0_amd64_struct2 () {
912 Amd64Struct2 s = new Amd64Struct2 ();
916 Amd64Struct2 s2 = mono_test_marshal_amd64_pass_return_struct2 (s);
918 return ((s2.i == 6) && (s2.j == -4)) ? 0 : 1;
921 /* TEST 3: 4 byte long INTEGER struct */
923 [StructLayout(LayoutKind.Sequential)]
924 public struct Amd64Struct3 {
928 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct3")]
929 public static extern Amd64Struct3 mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s);
931 public static int test_0_amd64_struct3 () {
932 Amd64Struct3 s = new Amd64Struct3 ();
935 Amd64Struct3 s2 = mono_test_marshal_amd64_pass_return_struct3 (s);
937 return (s2.i == -4) ? 0 : 1;
940 /* Test 4: 16 byte long FLOAT struct */
942 [StructLayout(LayoutKind.Sequential)]
943 public struct Amd64Struct4 {
944 public double d1, d2;
947 [DllImport ("libtest", EntryPoint="mono_test_marshal_amd64_pass_return_struct4")]
948 public static extern Amd64Struct4 mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s);
950 public static int test_0_amd64_struct4 () {
951 Amd64Struct4 s = new Amd64Struct4 ();
955 Amd64Struct4 s2 = mono_test_marshal_amd64_pass_return_struct4 (s);
957 return (s2.d1 == 6.0 && s2.d2 == -4.0) ? 0 : 1;
964 /* Test 5: Float HFA */
966 [StructLayout(LayoutKind.Sequential)]
967 public struct TestStruct5 {
971 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct5")]
972 public static extern TestStruct5 mono_test_marshal_ia64_pass_return_struct5 (double d1, double d2, TestStruct5 s, double f3, double f4);
974 public static int test_0_ia64_struct5 () {
975 TestStruct5 s = new TestStruct5 ();
979 TestStruct5 s2 = mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s, 3.0, 4.0);
981 return (s2.d1 == 8.0 && s2.d2 == 2.0) ? 0 : 1;
984 /* Test 6: Double HFA */
986 [StructLayout(LayoutKind.Sequential)]
987 public struct TestStruct6 {
988 public double d1, d2;
991 [DllImport ("libtest", EntryPoint="mono_test_marshal_ia64_pass_return_struct6")]
992 public static extern TestStruct6 mono_test_marshal_ia64_pass_return_struct6 (double d1, double d2, TestStruct6 s, double f3, double f4);
994 public static int test_0_ia64_struct6 () {
995 TestStruct6 s = new TestStruct6 ();
999 TestStruct6 s2 = mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s, 3.0, 4.0);
1001 return (s2.d1 == 9.0 && s2.d2 == 1.0) ? 0 : 1;
1004 /* Blittable class */
1005 [DllImport("libtest")]
1006 private static extern BlittableClass TestBlittableClass (BlittableClass vl);
1008 public static int test_0_marshal_blittable_class () {
1009 BlittableClass v1 = new BlittableClass ();
1011 /* Since it is blittable, it looks like it is passed as in/out */
1012 BlittableClass v2 = TestBlittableClass (v1);
1014 if (v1.a != 2 || v1.b != 3)
1017 if (v2.a != 2 || v2.b != 3)
1021 BlittableClass v3 = TestBlittableClass (null);
1023 if (v3.a != 42 || v3.b != 43)
1029 public static int test_0_marshal_byval_class () {
1030 SimpleObj obj0 = new SimpleObj ();
1034 if (class_marshal_test0 (obj0) != 0)
1040 public static int test_0_marshal_byval_class_null () {
1041 if (class_marshal_test4 (null) != 0)
1047 public static int test_0_marshal_out_class () {
1050 class_marshal_test1 (out obj1);
1052 if (obj1.str != "ABC")
1061 public static int test_0_marshal_string () {
1062 return string_marshal_test0 ("TEST0");
1065 public static int test_0_marshal_out_string () {
1068 string_marshal_test1 (out res);
1076 public static int test_0_marshal_byref_string () {
1077 string res = "TEST1";
1079 return string_marshal_test2 (ref res);
1082 public static int test_0_marshal_null_string () {
1083 return string_marshal_test3 (null);
1086 [DllImport ("libtest", EntryPoint="mono_test_stdcall_name_mangling", CallingConvention=CallingConvention.StdCall)]
1087 public static extern int mono_test_stdcall_name_mangling (int a, int b, int c);
1089 public static int test_0_stdcall_name_mangling () {
1090 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1094 * Pointers to structures can not be passed
1097 public struct CharInfo {
1098 public char Character;
1099 public short Attributes;
1102 [DllImport ("libtest", EntryPoint="mono_test_marshal_struct")]
1103 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo *ptr);
1105 public static unsafe int test_0_marshal_ptr_to_struct () {
1106 CharInfo [] buffer = new CharInfo [1];
1107 fixed (CharInfo *ptr = &buffer [0]) {
1109 mono_test_marshal_ptr_to_struct (ptr);
1112 catch (MarshalDirectiveException) {
1120 * LPWStr marshalling
1123 [DllImport("libtest", EntryPoint="test_lpwstr_marshal")]
1124 [return: MarshalAs(UnmanagedType.LPWStr)]
1125 private static extern string mono_test_marshal_lpwstr_marshal(
1126 [MarshalAs(UnmanagedType.LPWStr)] string s,
1129 [DllImport("libtest", EntryPoint="test_lpwstr_marshal", CharSet=CharSet.Unicode)]
1130 private static extern string mono_test_marshal_lpwstr_marshal2(
1134 public static int test_0_pass_return_lwpstr () {
1137 string res = mono_test_marshal_lpwstr_marshal (s, s.Length);
1142 string res2 = mono_test_marshal_lpwstr_marshal2 (s, s.Length);
1151 * Byref bool marshalling
1154 [DllImport("libtest")]
1155 extern static int marshal_test_ref_bool
1158 [MarshalAs(UnmanagedType.I1)] ref bool b1,
1159 [MarshalAs(UnmanagedType.VariantBool)] ref bool b2,
1163 public static int test_0_pass_byref_bool () {
1164 for (int i = 0; i < 8; i++)
1166 bool b1 = (i & 4) != 0;
1167 bool b2 = (i & 2) != 0;
1168 bool b3 = (i & 1) != 0;
1169 bool orig_b1 = b1, orig_b2 = b2, orig_b3 = b3;
1170 if (marshal_test_ref_bool(i, ref b1, ref b2, ref b3) != 0)
1184 * Bool struct field marshalling
1190 [MarshalAs(UnmanagedType.I1)] public bool b1;
1191 [MarshalAs(UnmanagedType.VariantBool)] public bool b2;
1195 [DllImport("libtest")]
1196 extern static int marshal_test_bool_struct(ref BoolStruct s);
1198 public static int test_0_pass_bool_in_struct () {
1199 for (int i = 0; i < 8; i++)
1201 BoolStruct s = new BoolStruct();
1203 s.b1 = (i & 4) != 0;
1204 s.b2 = (i & 2) != 0;
1205 s.b3 = (i & 1) != 0;
1206 BoolStruct orig = s;
1207 if (marshal_test_bool_struct(ref s) != 0)
1209 if (s.b1 != !orig.b1)
1211 if (s.b2 != !orig.b2)
1213 if (s.b3 != !orig.b3)
1221 * Invoking pinvoke methods through delegates
1224 delegate int MyDelegate (string name);
1226 [DllImport ("libtest", EntryPoint="mono_test_puts_static")]
1227 public static extern int puts_static (string name);
1229 public static int test_0_invoke_pinvoke_through_delegate () {
1230 puts_static ("A simple Test for PInvoke 1");
1232 MyDelegate d = new MyDelegate (puts_static);
1233 d ("A simple Test for PInvoke 2");
1235 object [] args = {"A simple Test for PInvoke 3"};
1236 d.DynamicInvoke (args);
1242 * Missing virtual pinvoke methods
1247 public virtual object MyClone ()
1253 public class T2 : T {
1254 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1255 public override extern object MyClone ();
1258 public static int test_0_missing_virtual_pinvoke_method () {
1263 } catch (Exception ex) {