3 using System
.Runtime
.InteropServices
;
4 using System
.Runtime
.CompilerServices
;
8 public static int delegate_test (int a
)
16 [StructLayout (LayoutKind
.Sequential
)]
17 public struct SimpleStruct
{
22 [MarshalAs(UnmanagedType
.LPWStr
)]
26 [StructLayout (LayoutKind
.Sequential
)]
27 public struct SimpleStruct2
{
38 [StructLayout (LayoutKind
.Sequential
, Size
=0)]
39 public struct EmptyStruct
{
42 [StructLayout (LayoutKind
.Sequential
)]
43 public struct DelegateStruct
{
45 public SimpleDelegate del
;
46 [MarshalAs(UnmanagedType
.FunctionPtr
)]
47 public SimpleDelegate del2
;
50 /* sparcv9 has complex conventions when passing structs with doubles in them
51 by value, some simple tests for them */
52 [StructLayout (LayoutKind
.Sequential
)]
58 [StructLayout (LayoutKind
.Sequential
)]
59 public struct MixedPoint
{
64 [StructLayout (LayoutKind
.Sequential
)]
65 public class SimpleClass
{
76 [StructLayout (LayoutKind
.Sequential
)]
77 public class EmptyClass
{
80 [StructLayout (LayoutKind
.Sequential
)]
81 public struct LongAlignStruct
{
87 [StructLayout(LayoutKind
.Sequential
)]
88 public class BlittableClass
94 [StructLayout (LayoutKind
.Sequential
)]
101 [StructLayout(LayoutKind
.Sequential
)]
109 public AsAnyStruct(int i
, int j
, int k
, String s
) {
117 [StructLayout(LayoutKind
.Sequential
)]
125 public AsAnyClass(int i
, int j
, int k
, String s
) {
132 [DllImport ("libnot-found", EntryPoint
="not_found")]
133 public static extern int mono_library_not_found ();
135 [DllImport ("libtest", EntryPoint
="not_found")]
136 public static extern int mono_entry_point_not_found ();
138 [DllImport ("libtest.dll", EntryPoint
="mono_test_marshal_char")]
139 public static extern int mono_test_marshal_char_2 (char a1
);
141 [DllImport ("test", EntryPoint
="mono_test_marshal_char")]
142 public static extern int mono_test_marshal_char_3 (char a1
);
144 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char")]
145 public static extern int mono_test_marshal_char (char a1
);
147 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char_array", CharSet
=CharSet
.Unicode
)]
148 public static extern int mono_test_marshal_char_array (char[] a1
);
150 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_byref")]
151 public static extern int mono_test_marshal_bool_byref (int a
, ref bool b
, int c
);
153 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array")]
154 public static extern int mono_test_marshal_array (int [] a1
);
156 [DllImport ("libtest", EntryPoint
="mono_test_marshal_empty_string_array")]
157 public static extern int mono_test_marshal_empty_string_array (string [] a1
);
159 [DllImport ("libtest", EntryPoint
="mono_test_marshal_string_array")]
160 public static extern int mono_test_marshal_string_array (string [] a1
);
162 [DllImport ("libtest", EntryPoint
="mono_test_marshal_unicode_string_array", CharSet
=CharSet
.Unicode
)]
163 public static extern int mono_test_marshal_unicode_string_array (string [] a1
, [MarshalAs(UnmanagedType
.LPArray
, ArraySubType
=UnmanagedType
.LPStr
)]string [] a2
);
165 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_array")]
166 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder
[] a1
);
168 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_array")]
169 public static extern int mono_test_marshal_inout_array ([In
, Out
] int [] a1
);
171 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_array")]
172 public static extern int mono_test_marshal_out_array ([Out
] [MarshalAs(UnmanagedType
.LPArray
, SizeParamIndex
= 1)] int [] a1
, int n
);
174 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_nonblittable_array", CharSet
= CharSet
.Unicode
)]
175 public static extern int mono_test_marshal_inout_nonblittable_array ([In
, Out
] char [] a1
);
177 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
178 public static extern int mono_test_marshal_struct (SimpleStruct ss
);
180 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2")]
181 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss
);
183 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2_2")]
184 public static extern int mono_test_marshal_struct2_2 (int i
, int j
, int k
, SimpleStruct2 ss
);
186 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
187 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
189 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
190 public static extern int mono_test_marshal_byref_struct_in ([In
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
192 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
193 public static extern int mono_test_marshal_byref_struct_inout ([In
, Out
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
195 [DllImport ("libtest", EntryPoint
="mono_test_marshal_point")]
196 public static extern int mono_test_marshal_point (Point p
);
198 [DllImport ("libtest", EntryPoint
="mono_test_marshal_mixed_point")]
199 public static extern int mono_test_marshal_mixed_point (MixedPoint p
);
201 [DllImport ("libtest", EntryPoint
="mono_test_empty_struct")]
202 public static extern int mono_test_empty_struct (int a
, EmptyStruct es
, int b
);
204 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct")]
205 public static extern int mono_test_marshal_lpstruct ([In
, MarshalAs(UnmanagedType
.LPStruct
)] SimpleStruct ss
);
207 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct_blittable")]
208 public static extern int mono_test_marshal_lpstruct_blittable ([In
, MarshalAs(UnmanagedType
.LPStruct
)] Point p
);
210 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct_array")]
211 public static extern int mono_test_marshal_struct_array (SimpleStruct2
[] ss
);
213 [DllImport ("libtest", EntryPoint
="mono_test_marshal_long_align_struct_array")]
214 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct
[] ss
);
216 [DllImport ("libtest", EntryPoint
="mono_test_marshal_class")]
217 public static extern SimpleClass
mono_test_marshal_class (int i
, int j
, int k
, SimpleClass ss
, int l
);
219 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_class")]
220 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss
);
222 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate")]
223 public static extern int mono_test_marshal_delegate (SimpleDelegate d
);
225 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate_struct")]
226 public static extern DelegateStruct
mono_test_marshal_delegate_struct (DelegateStruct d
);
228 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_delegate")]
229 public static extern SimpleDelegate
mono_test_marshal_return_delegate (SimpleDelegate d
);
231 [DllImport ("libtest", EntryPoint
="mono_test_return_vtype")]
232 public static extern SimpleStruct
mono_test_return_vtype (IntPtr i
);
234 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder")]
235 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb
, int len
);
237 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_default")]
238 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb
, int len
);
240 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_unicode", CharSet
=CharSet
.Unicode
)]
241 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb
, int len
);
243 [DllImport ("libtest", EntryPoint
="mono_test_last_error", SetLastError
=true)]
244 public static extern void mono_test_last_error (int err
);
246 [DllImport ("libtest", EntryPoint
="mono_test_asany")]
247 public static extern int mono_test_asany ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
249 [DllImport ("libtest", EntryPoint
="mono_test_asany", CharSet
=CharSet
.Unicode
)]
250 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
252 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_inout")]
253 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType
.AsAny
)][In
] object obj
);
255 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_inout")]
256 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType
.AsAny
)][Out
] object obj
);
257 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_inout")]
258 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType
.AsAny
)][In
, Out
] object obj
);
260 [DllImport ("libtest")]
261 static extern int class_marshal_test0 (SimpleObj obj
);
263 [DllImport ("libtest")]
264 static extern void class_marshal_test1 (out SimpleObj obj
);
266 [DllImport ("libtest")]
267 static extern int class_marshal_test4 (SimpleObj obj
);
269 [DllImport ("libtest")]
270 static extern int string_marshal_test0 (string str
);
272 [DllImport ("libtest")]
273 static extern void string_marshal_test1 (out string str
);
275 [DllImport ("libtest")]
276 static extern int string_marshal_test2 (ref string str
);
278 [DllImport ("libtest")]
279 static extern int string_marshal_test3 (string str
);
281 public delegate int SimpleDelegate (int a
);
283 public static int Main (string[] args
) {
284 return TestDriver
.RunTests (typeof (Tests
), args
);
287 public static int test_0_marshal_char () {
288 return mono_test_marshal_char ('a');
291 public static int test_0_marshal_char_array () {
292 // a unicode char[] is implicitly marshalled as [Out]
293 char[] buf
= new char [32];
294 mono_test_marshal_char_array (buf
);
295 string s
= new string (buf
);
296 if (s
.StartsWith ("abcdef"))
302 public static int test_1225_marshal_array () {
303 int [] a1
= new int [50];
304 for (int i
= 0; i
< 50; i
++)
307 return mono_test_marshal_array (a1
);
310 public static int test_1225_marshal_inout_array () {
311 int [] a1
= new int [50];
312 for (int i
= 0; i
< 50; i
++)
315 int res
= mono_test_marshal_inout_array (a1
);
317 for (int i
= 0; i
< 50; i
++)
318 if (a1
[i
] != 50 - i
) {
319 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
326 public static int test_0_marshal_out_array () {
327 int [] a1
= new int [50];
329 int res
= mono_test_marshal_out_array (a1
, 0);
331 for (int i
= 0; i
< 50; i
++)
333 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
340 public static int test_0_marshal_inout_nonblittable_array () {
341 char [] a1
= new char [10];
342 for (int i
= 0; i
< 10; i
++)
343 a1
[i
] = "Hello, World" [i
];
345 int res
= mono_test_marshal_inout_nonblittable_array (a1
);
347 for (int i
= 0; i
< 10; i
++)
354 public static int test_0_marshal_struct () {
355 SimpleStruct ss
= new SimpleStruct ();
359 return mono_test_marshal_struct (ss
);
362 public static int test_0_marshal_struct2 () {
363 SimpleStruct2 ss2
= new SimpleStruct2 ();
371 return mono_test_marshal_struct2 (ss2
);
374 public static int test_0_marshal_struct3 () {
375 SimpleStruct2 ss2
= new SimpleStruct2 ();
383 return mono_test_marshal_struct2_2 (10, 11, 12, ss2
);
386 public static int test_0_marshal_empty_struct () {
387 EmptyStruct es
= new EmptyStruct ();
389 if (mono_test_empty_struct (1, es
, 2) != 0)
395 public static int test_0_marshal_lpstruct () {
396 SimpleStruct ss
= new SimpleStruct ();
400 return mono_test_marshal_lpstruct (ss
);
403 public static int test_0_marshal_lpstruct_blittable () {
404 Point p
= new Point ();
408 return mono_test_marshal_lpstruct_blittable (p
);
411 public static int test_0_marshal_struct_array () {
412 SimpleStruct2
[] ss_arr
= new SimpleStruct2
[2];
414 SimpleStruct2 ss2
= new SimpleStruct2 ();
433 return mono_test_marshal_struct_array (ss_arr
);
436 public static int test_105_marshal_long_align_struct_array () {
437 LongAlignStruct
[] ss_arr
= new LongAlignStruct
[2];
439 LongAlignStruct ss
= new LongAlignStruct ();
452 return mono_test_marshal_long_align_struct_array (ss_arr
);
455 /* Test classes as arguments and return values */
456 public static int test_0_marshal_class () {
457 SimpleClass ss
= new SimpleClass ();
465 SimpleClass res
= mono_test_marshal_class (10, 11, 12, ss
, 14);
468 if (! (res
.a
== ss
.a
&& res
.b
== ss
.b
&& res
.c
== ss
.c
&&
469 res
.d
== ss
.d
&& res
.e
== ss
.e
&& res
.f
== ss
.f
&&
470 res
.g
== ss
.g
&& res
.h
== ss
.h
))
473 /* Test null arguments and results */
474 res
= mono_test_marshal_class (10, 11, 12, null, 14);
481 public static int test_0_marshal_byref_class () {
482 SimpleClass ss
= new SimpleClass ();
490 int res
= mono_test_marshal_byref_class (ref ss
);
491 if (ss
.d
!= "TEST-RES")
497 public static int test_0_marshal_delegate () {
498 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
500 return mono_test_marshal_delegate (d
);
503 public static int test_0_marshal_return_delegate () {
504 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
506 SimpleDelegate d2
= mono_test_marshal_return_delegate (d
);
511 public static int test_0_marshal_delegate_struct () {
512 DelegateStruct s
= new DelegateStruct ();
515 s
.del
= new SimpleDelegate (delegate_test
);
516 s
.del2
= new SimpleDelegate (delegate_test
);
518 DelegateStruct res
= mono_test_marshal_delegate_struct (s
);
522 if (res
.del (2) != 0)
524 if (res
.del2 (2) != 0)
530 public static int test_0_marshal_byref_struct () {
531 SimpleStruct s
= new SimpleStruct ();
538 int res
= mono_test_marshal_byref_struct (ref s
, true, false, true, "ABC");
541 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
546 public static int test_0_marshal_byref_struct_in () {
547 SimpleStruct s
= new SimpleStruct ();
554 int res
= mono_test_marshal_byref_struct_in (ref s
, true, false, true, "ABC");
557 if (s
.a
!= true || s
.b
!= false || s
.c
!= true || s
.d
!= "ABC")
562 public static int test_0_marshal_byref_struct_inout () {
563 SimpleStruct s
= new SimpleStruct ();
570 int res
= mono_test_marshal_byref_struct_inout (ref s
, true, false, true, "ABC");
573 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
578 public static int test_0_marshal_point () {
579 Point pt
= new Point();
583 return mono_test_marshal_point(pt
);
586 public static int test_0_marshal_mixed_point () {
587 MixedPoint mpt
= new MixedPoint();
591 return mono_test_marshal_mixed_point(mpt
);
594 public static int test_0_marshal_bool_byref () {
596 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 1)
599 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 0)
607 public static int test_0_return_vtype () {
608 SimpleStruct ss
= mono_test_return_vtype (new IntPtr (5));
610 if (!ss
.a
&& ss
.b
&& !ss
.c
&& ss
.d
== "TEST" && ss
.d2
== "TEST2")
616 public static int test_0_marshal_stringbuilder () {
617 StringBuilder sb
= new StringBuilder(255);
619 mono_test_marshal_stringbuilder (sb
, sb
.Capacity
);
620 String res
= sb
.ToString();
622 if (res
!= "This is my message. Isn't it nice?")
625 // Test StringBuilder with default capacity (16)
626 StringBuilder sb2
= new StringBuilder();
627 mono_test_marshal_stringbuilder_default (sb2
, sb2
.Capacity
);
628 if (sb2
.ToString () != "This is my messa")
634 public static int test_0_marshal_stringbuilder_unicode () {
635 StringBuilder sb
= new StringBuilder(255);
636 mono_test_marshal_stringbuilder_unicode (sb
, sb
.Capacity
);
637 String res
= sb
.ToString();
639 if (res
!= "This is my message. Isn't it nice?")
642 // Test StringBuilder with default capacity (16)
643 StringBuilder sb2
= new StringBuilder();
644 mono_test_marshal_stringbuilder_unicode (sb2
, sb2
.Capacity
);
645 if (sb2
.ToString () != "This is my messa")
651 public static int test_0_marshal_empty_string_array () {
652 return mono_test_marshal_empty_string_array (null);
655 public static int test_0_marshal_string_array () {
656 return mono_test_marshal_string_array (new String
[] { "ABC", "DEF" }
);
659 public static int test_0_marshal_unicode_string_array () {
660 return mono_test_marshal_unicode_string_array (new String
[] { "ABC", "DEF" }
, new String
[] { "ABC", "DEF" }
);
663 public static int test_0_marshal_stringbuilder_array () {
664 StringBuilder sb1
= new StringBuilder ("ABC");
665 StringBuilder sb2
= new StringBuilder ("DEF");
667 int res
= mono_test_marshal_stringbuilder_array (new StringBuilder
[] { sb1, sb2 }
);
670 if (sb1
.ToString () != "DEF")
672 if (sb2
.ToString () != "ABC")
677 public static int test_0_last_error () {
678 mono_test_last_error (5);
679 if (Marshal
.GetLastWin32Error () == 5)
685 public static int test_0_library_not_found () {
688 mono_entry_point_not_found ();
691 catch (EntryPointNotFoundException
) {
697 public static int test_0_entry_point_not_found () {
700 mono_library_not_found ();
703 catch (DllNotFoundException
) {
709 /* Check that the runtime trims .dll from the library name */
710 public static int test_0_trim_dll_from_name () {
712 mono_test_marshal_char_2 ('A');
717 /* Check that the runtime adds lib to to the library name */
718 public static int test_0_add_lib_to_name () {
720 mono_test_marshal_char_3 ('A');
729 public static int test_0_asany () {
730 if (mono_test_asany (5, 1) != 0)
733 if (mono_test_asany ("ABC", 2) != 0)
736 SimpleStruct2 ss2
= new SimpleStruct2 ();
744 if (mono_test_asany (ss2
, 3) != 0)
747 if (mono_test_asany_unicode ("ABC", 4) != 0)
753 mono_test_asany (c
, 0);
756 catch (ArgumentException
) {
760 mono_test_asany (new Object (), 0);
763 catch (ArgumentException
) {
769 /* AsAny marshalling + [In, Out] */
771 public static int test_0_asany_in () {
773 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
774 mono_test_asany_in (str
);
777 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
778 mono_test_asany_in (cls
);
779 if ((cls
.i
!= 1) || (cls
.j
!= 2) || (cls
.k
!= 3))
783 object obj
= new AsAnyStruct(1,2,3, "ABC");
784 mono_test_asany_in (obj
);
785 str
= (AsAnyStruct
)obj
;
786 if ((str
.i
!= 1) || (str
.j
!= 2) || (str
.k
!= 3))
792 public static int test_0_asany_out () {
794 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
795 mono_test_asany_out (str
);
798 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
799 mono_test_asany_out (cls
);
800 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
804 object obj
= new AsAnyStruct(1,2,3, "ABC");
805 mono_test_asany_out (obj
);
806 str
= (AsAnyStruct
)obj
;
807 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
813 public static int test_0_asany_inout () {
815 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
816 mono_test_asany_inout (str
);
819 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
820 mono_test_asany_inout (cls
);
821 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
825 object obj
= new AsAnyStruct(1,2,3, "ABC");
826 mono_test_asany_inout (obj
);
827 str
= (AsAnyStruct
)obj
;
828 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
834 /* Byref String Array */
836 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_string_array")]
837 public static extern int mono_test_marshal_byref_string_array (ref string[] data
);
839 public static int test_0_byref_string_array () {
843 if (mono_test_marshal_byref_string_array (ref arr
) != 0)
846 arr
= new string[] { "Alpha", "Beta", "Gamma" }
;
848 if (mono_test_marshal_byref_string_array (ref arr
) != 1)
851 /* FIXME: Test returned array and out case */
857 * AMD64 small structs-by-value tests.
860 /* TEST 1: 16 byte long INTEGER struct */
862 [StructLayout(LayoutKind
.Sequential
)]
863 public struct Amd64Struct1
{
870 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1")]
871 public static extern Amd64Struct1
mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s
);
873 public static int test_0_amd64_struct1 () {
874 Amd64Struct1 s
= new Amd64Struct1 ();
880 Amd64Struct1 s2
= mono_test_marshal_amd64_pass_return_struct1 (s
);
882 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000)) ? 0 : 1;
885 /* TEST 2: 8 byte long INTEGER struct */
887 [StructLayout(LayoutKind
.Sequential
)]
888 public struct Amd64Struct2
{
893 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct2")]
894 public static extern Amd64Struct2
mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s
);
896 public static int test_0_amd64_struct2 () {
897 Amd64Struct2 s
= new Amd64Struct2 ();
901 Amd64Struct2 s2
= mono_test_marshal_amd64_pass_return_struct2 (s
);
903 return ((s2
.i
== 6) && (s2
.j
== -4)) ? 0 : 1;
906 /* TEST 3: 4 byte long INTEGER struct */
908 [StructLayout(LayoutKind
.Sequential
)]
909 public struct Amd64Struct3
{
913 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct3")]
914 public static extern Amd64Struct3
mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s
);
916 public static int test_0_amd64_struct3 () {
917 Amd64Struct3 s
= new Amd64Struct3 ();
920 Amd64Struct3 s2
= mono_test_marshal_amd64_pass_return_struct3 (s
);
922 return (s2
.i
== -4) ? 0 : 1;
925 /* Test 4: 16 byte long FLOAT struct */
927 [StructLayout(LayoutKind
.Sequential
)]
928 public struct Amd64Struct4
{
929 public double d1
, d2
;
932 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct4")]
933 public static extern Amd64Struct4
mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s
);
935 public static int test_0_amd64_struct4 () {
936 Amd64Struct4 s
= new Amd64Struct4 ();
940 Amd64Struct4 s2
= mono_test_marshal_amd64_pass_return_struct4 (s
);
942 return (s2
.d1
== 6.0 && s2
.d2
== -4.0) ? 0 : 1;
949 /* Test 5: Float HFA */
951 [StructLayout(LayoutKind
.Sequential
)]
952 public struct TestStruct5
{
956 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct5")]
957 public static extern TestStruct5
mono_test_marshal_ia64_pass_return_struct5 (double d1
, double d2
, TestStruct5 s
, double f3
, double f4
);
959 public static int test_0_ia64_struct5 () {
960 TestStruct5 s
= new TestStruct5 ();
964 TestStruct5 s2
= mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s
, 3.0, 4.0);
966 return (s2
.d1
== 8.0 && s2
.d2
== 2.0) ? 0 : 1;
969 /* Test 6: Double HFA */
971 [StructLayout(LayoutKind
.Sequential
)]
972 public struct TestStruct6
{
973 public double d1
, d2
;
976 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct6")]
977 public static extern TestStruct6
mono_test_marshal_ia64_pass_return_struct6 (double d1
, double d2
, TestStruct6 s
, double f3
, double f4
);
979 public static int test_0_ia64_struct6 () {
980 TestStruct6 s
= new TestStruct6 ();
984 TestStruct6 s2
= mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s
, 3.0, 4.0);
986 return (s2
.d1
== 9.0 && s2
.d2
== 1.0) ? 0 : 1;
989 /* Blittable class */
990 [DllImport("libtest")]
991 private static extern BlittableClass
TestBlittableClass (BlittableClass vl
);
993 public static int test_0_marshal_blittable_class () {
994 BlittableClass v1
= new BlittableClass ();
996 /* Since it is blittable, it looks like it is passed as in/out */
997 BlittableClass v2
= TestBlittableClass (v1
);
999 if (v1
.a
!= 2 || v1
.b
!= 3)
1002 if (v2
.a
!= 2 || v2
.b
!= 3)
1006 BlittableClass v3
= TestBlittableClass (null);
1008 if (v3
.a
!= 42 || v3
.b
!= 43)
1014 public static int test_0_marshal_byval_class () {
1015 SimpleObj obj0
= new SimpleObj ();
1019 if (class_marshal_test0 (obj0
) != 0)
1025 public static int test_0_marshal_byval_class_null () {
1026 if (class_marshal_test4 (null) != 0)
1032 public static int test_0_marshal_out_class () {
1035 class_marshal_test1 (out obj1
);
1037 if (obj1
.str
!= "ABC")
1046 public static int test_0_marshal_string () {
1047 return string_marshal_test0 ("TEST0");
1050 public static int test_0_marshal_out_string () {
1053 string_marshal_test1 (out res
);
1061 public static int test_0_marshal_byref_string () {
1062 string res
= "TEST1";
1064 return string_marshal_test2 (ref res
);
1067 public static int test_0_marshal_null_string () {
1068 return string_marshal_test3 (null);
1071 [DllImport ("libtest", EntryPoint
="mono_test_stdcall_name_mangling", CallingConvention
=CallingConvention
.StdCall
)]
1072 public static extern int mono_test_stdcall_name_mangling (int a
, int b
, int c
);
1074 public static int test_0_stdcall_name_mangling () {
1075 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1079 * Pointers to structures can not be passed
1082 public struct CharInfo
{
1083 public char Character
;
1084 public short Attributes
;
1087 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
1088 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo
*ptr
);
1090 public static unsafe int test_0_marshal_ptr_to_struct () {
1091 CharInfo
[] buffer
= new CharInfo
[1];
1092 fixed (CharInfo
*ptr
= &buffer
[0]) {
1094 mono_test_marshal_ptr_to_struct (ptr
);
1097 catch (MarshalDirectiveException
) {
1105 * LPWStr marshalling
1108 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal")]
1109 [return: MarshalAs(UnmanagedType
.LPWStr
)]
1110 private static extern string mono_test_marshal_lpwstr_marshal(
1111 [MarshalAs(UnmanagedType
.LPWStr
)] string s
,
1114 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal", CharSet
=CharSet
.Unicode
)]
1115 private static extern string mono_test_marshal_lpwstr_marshal2(
1119 public static int test_0_pass_return_lwpstr () {
1122 string res
= mono_test_marshal_lpwstr_marshal (s
, s
.Length
);
1127 string res2
= mono_test_marshal_lpwstr_marshal2 (s
, s
.Length
);
1136 * Byref bool marshalling
1139 [DllImport("libtest")]
1140 extern static int marshal_test_ref_bool
1143 [MarshalAs(UnmanagedType
.I1
)] ref bool b1
,
1144 [MarshalAs(UnmanagedType
.VariantBool
)] ref bool b2
,
1148 public static int test_0_pass_byref_bool () {
1149 for (int i
= 0; i
< 8; i
++)
1151 bool b1
= (i
& 4) != 0;
1152 bool b2
= (i
& 2) != 0;
1153 bool b3
= (i
& 1) != 0;
1154 bool orig_b1
= b1
, orig_b2
= b2
, orig_b3
= b3
;
1155 if (marshal_test_ref_bool(i
, ref b1
, ref b2
, ref b3
) != 0)
1169 * Bool struct field marshalling
1175 [MarshalAs(UnmanagedType
.I1
)] public bool b1
;
1176 [MarshalAs(UnmanagedType
.VariantBool
)] public bool b2
;
1180 [DllImport("libtest")]
1181 extern static int marshal_test_bool_struct(ref BoolStruct s
);
1183 public static int test_0_pass_bool_in_struct () {
1184 for (int i
= 0; i
< 8; i
++)
1186 BoolStruct s
= new BoolStruct();
1188 s
.b1
= (i
& 4) != 0;
1189 s
.b2
= (i
& 2) != 0;
1190 s
.b3
= (i
& 1) != 0;
1191 BoolStruct orig
= s
;
1192 if (marshal_test_bool_struct(ref s
) != 0)
1194 if (s
.b1
!= !orig
.b1
)
1196 if (s
.b2
!= !orig
.b2
)
1198 if (s
.b3
!= !orig
.b3
)
1206 * Invoking pinvoke methods through delegates
1209 delegate int MyDelegate (string name
);
1211 [DllImport ("libtest", EntryPoint
="mono_test_puts_static")]
1212 public static extern int puts_static (string name
);
1214 public static int test_0_invoke_pinvoke_through_delegate () {
1215 puts_static ("A simple Test for PInvoke 1");
1217 MyDelegate d
= new MyDelegate (puts_static
);
1218 d ("A simple Test for PInvoke 2");
1220 object [] args
= {"A simple Test for PInvoke 3"}
;
1221 d
.DynamicInvoke (args
);
1227 * Missing virtual pinvoke methods
1232 public virtual object MyClone ()
1238 public class T2
: T
{
1239 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
1240 public override extern object MyClone ();
1243 public static int test_0_missing_virtual_pinvoke_method () {
1248 } catch (Exception ex
) {