3 using System
.Runtime
.InteropServices
;
4 using System
.Runtime
.CompilerServices
;
5 using System
.Reflection
.Emit
;
11 public static int delegate_test (int a
)
19 public int delegate_test_instance (int a
)
24 [StructLayout (LayoutKind
.Sequential
)]
25 public struct SimpleStruct
{
30 [MarshalAs(UnmanagedType
.LPWStr
)]
34 [StructLayout (LayoutKind
.Sequential
)]
35 public struct SimpleStructGen
<T
> {
40 [MarshalAs(UnmanagedType
.LPWStr
)]
44 [StructLayout (LayoutKind
.Sequential
)]
45 public struct SimpleStruct2
{
56 [StructLayout (LayoutKind
.Sequential
, Size
=0)]
57 public struct EmptyStruct
{
60 [StructLayout (LayoutKind
.Sequential
)]
61 public struct DelegateStruct
{
63 public SimpleDelegate del
;
64 [MarshalAs(UnmanagedType
.FunctionPtr
)]
65 public SimpleDelegate del2
;
66 [MarshalAs(UnmanagedType
.FunctionPtr
)]
67 public SimpleDelegate del3
;
70 /* sparcv9 has complex conventions when passing structs with doubles in them
71 by value, some simple tests for them */
72 [StructLayout (LayoutKind
.Sequential
)]
78 [StructLayout (LayoutKind
.Sequential
)]
79 public struct MixedPoint
{
84 [StructLayout (LayoutKind
.Sequential
)]
85 public class SimpleClass
{
96 [StructLayout (LayoutKind
.Sequential
)]
97 public class EmptyClass
{
100 [StructLayout (LayoutKind
.Sequential
)]
101 public struct LongAlignStruct
{
107 [StructLayout(LayoutKind
.Sequential
)]
108 public class BlittableClass
114 [StructLayout (LayoutKind
.Sequential
)]
121 [StructLayout(LayoutKind
.Sequential
)]
129 public AsAnyStruct(int i
, int j
, int k
, String s
) {
137 [StructLayout(LayoutKind
.Sequential
)]
145 public AsAnyClass(int i
, int j
, int k
, String s
) {
152 [DllImport ("libnot-found", EntryPoint
="not_found")]
153 public static extern int mono_library_not_found ();
155 [DllImport ("libtest", EntryPoint
="not_found")]
156 public static extern int mono_entry_point_not_found ();
158 [DllImport ("libtest.dll", EntryPoint
="mono_test_marshal_char")]
159 public static extern int mono_test_marshal_char_2 (char a1
);
161 [DllImport ("test", EntryPoint
="mono_test_marshal_char")]
162 public static extern int mono_test_marshal_char_3 (char a1
);
164 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char")]
165 public static extern int mono_test_marshal_char (char a1
);
167 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char_array", CharSet
=CharSet
.Unicode
)]
168 public static extern int mono_test_marshal_char_array (char[] a1
);
170 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_byref")]
171 public static extern int mono_test_marshal_bool_byref (int a
, ref bool b
, int c
);
173 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_in_as_I1_U1")]
174 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType
.I1
)] bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] bool bFalse
);
176 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_in_as_I1_U1")]
177 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType
.U1
)] bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] bool bFalse
);
179 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_out_as_I1_U1")]
180 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType
.I1
)] out bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] out bool bFalse
);
182 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_out_as_I1_U1")]
183 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType
.U1
)] out bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] out bool bFalse
);
185 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_ref_as_I1_U1")]
186 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType
.I1
)] ref bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] ref bool bFalse
);
188 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_ref_as_I1_U1")]
189 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType
.U1
)] ref bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] ref bool bFalse
);
191 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array")]
192 public static extern int mono_test_marshal_array (int [] a1
);
194 [DllImport ("libtest", EntryPoint
="mono_test_marshal_empty_string_array")]
195 public static extern int mono_test_marshal_empty_string_array (string [] a1
);
197 [DllImport ("libtest", EntryPoint
="mono_test_marshal_string_array")]
198 public static extern int mono_test_marshal_string_array (string [] a1
);
200 [DllImport ("libtest", EntryPoint
="mono_test_marshal_unicode_string_array", CharSet
=CharSet
.Unicode
)]
201 public static extern int mono_test_marshal_unicode_string_array (string [] a1
, [MarshalAs(UnmanagedType
.LPArray
, ArraySubType
=UnmanagedType
.LPStr
)]string [] a2
);
203 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_array")]
204 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder
[] a1
);
206 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_array")]
207 public static extern int mono_test_marshal_inout_array ([In
, Out
] int [] a1
);
209 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_array")]
210 public static extern int mono_test_marshal_out_array ([Out
] [MarshalAs(UnmanagedType
.LPArray
, SizeParamIndex
= 1)] int [] a1
, int n
);
212 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_nonblittable_array", CharSet
= CharSet
.Unicode
)]
213 public static extern int mono_test_marshal_inout_nonblittable_array ([In
, Out
] char [] a1
);
215 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
216 public static extern int mono_test_marshal_struct (SimpleStruct ss
);
218 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
219 public static extern int mono_test_marshal_struct_gen (SimpleStructGen
<string> ss
);
221 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2")]
222 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss
);
224 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2_2")]
225 public static extern int mono_test_marshal_struct2_2 (int i
, int j
, int k
, SimpleStruct2 ss
);
227 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
228 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
230 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
231 public static extern int mono_test_marshal_byref_struct_in ([In
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
233 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
234 public static extern int mono_test_marshal_byref_struct_inout ([In
, Out
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
236 [DllImport ("libtest", EntryPoint
="mono_test_marshal_point")]
237 public static extern int mono_test_marshal_point (Point p
);
239 [DllImport ("libtest", EntryPoint
="mono_test_marshal_mixed_point")]
240 public static extern int mono_test_marshal_mixed_point (MixedPoint p
);
242 [DllImport ("libtest", EntryPoint
="mono_test_empty_struct")]
243 public static extern int mono_test_empty_struct (int a
, EmptyStruct es
, int b
);
245 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct")]
246 public static extern int mono_test_marshal_lpstruct ([In
, MarshalAs(UnmanagedType
.LPStruct
)] SimpleStruct ss
);
248 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct_blittable")]
249 public static extern int mono_test_marshal_lpstruct_blittable ([In
, MarshalAs(UnmanagedType
.LPStruct
)] Point p
);
251 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct_array")]
252 public static extern int mono_test_marshal_struct_array (SimpleStruct2
[] ss
);
254 [DllImport ("libtest", EntryPoint
="mono_test_marshal_long_align_struct_array")]
255 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct
[] ss
);
257 [DllImport ("libtest", EntryPoint
="mono_test_marshal_class")]
258 public static extern SimpleClass
mono_test_marshal_class (int i
, int j
, int k
, SimpleClass ss
, int l
);
260 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_class")]
261 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss
);
263 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate")]
264 public static extern int mono_test_marshal_delegate (SimpleDelegate d
);
266 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate_struct")]
267 public static extern DelegateStruct
mono_test_marshal_delegate_struct (DelegateStruct d
);
269 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_delegate")]
270 public static extern SimpleDelegate
mono_test_marshal_return_delegate (SimpleDelegate d
);
272 [DllImport ("libtest", EntryPoint
="mono_test_return_vtype")]
273 public static extern SimpleStruct
mono_test_return_vtype (IntPtr i
);
275 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder")]
276 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb
, int len
);
278 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_default")]
279 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb
, int len
);
281 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_unicode", CharSet
=CharSet
.Unicode
)]
282 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb
, int len
);
284 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_out")]
285 public static extern void mono_test_marshal_stringbuilder_out (out StringBuilder sb
);
287 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_out_unicode", CharSet
=CharSet
.Unicode
)]
288 public static extern void mono_test_marshal_stringbuilder_out_unicode (out StringBuilder sb
);
290 [DllImport ("libtest", EntryPoint
="mono_test_last_error", SetLastError
=true)]
291 public static extern void mono_test_last_error (int err
);
293 [DllImport ("libtest", EntryPoint
="mono_test_asany")]
294 public static extern int mono_test_asany ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
296 [DllImport ("libtest", EntryPoint
="mono_test_asany", CharSet
=CharSet
.Unicode
)]
297 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
299 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_in")]
300 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType
.AsAny
)][In
] object obj
);
302 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_out")]
303 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType
.AsAny
)][Out
] object obj
);
304 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_inout")]
305 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType
.AsAny
)][In
, Out
] object obj
);
307 [DllImport ("libtest")]
308 static extern int class_marshal_test0 (SimpleObj obj
);
310 [DllImport ("libtest")]
311 static extern void class_marshal_test1 (out SimpleObj obj
);
313 [DllImport ("libtest")]
314 static extern int class_marshal_test4 (SimpleObj obj
);
316 [DllImport ("libtest")]
317 static extern int string_marshal_test0 (string str
);
319 [DllImport ("libtest")]
320 static extern void string_marshal_test1 (out string str
);
322 [DllImport ("libtest")]
323 static extern int string_marshal_test2 (ref string str
);
325 [DllImport ("libtest")]
326 static extern int string_marshal_test3 (string str
);
328 public delegate int SimpleDelegate (int a
);
330 public static int Main (string[] args
) {
331 return TestDriver
.RunTests (typeof (Tests
), args
);
334 public static int test_0_marshal_char () {
335 return mono_test_marshal_char ('a');
338 public static int test_0_marshal_char_array () {
339 // a unicode char[] is implicitly marshalled as [Out]
340 char[] buf
= new char [32];
341 mono_test_marshal_char_array (buf
);
342 string s
= new string (buf
);
343 if (s
.StartsWith ("abcdef"))
349 public static int test_1225_marshal_array () {
350 int [] a1
= new int [50];
351 for (int i
= 0; i
< 50; i
++)
354 return mono_test_marshal_array (a1
);
357 public static int test_1225_marshal_inout_array () {
358 int [] a1
= new int [50];
359 for (int i
= 0; i
< 50; i
++)
362 int res
= mono_test_marshal_inout_array (a1
);
364 for (int i
= 0; i
< 50; i
++)
365 if (a1
[i
] != 50 - i
) {
366 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
373 public static int test_0_marshal_out_array () {
374 int [] a1
= new int [50];
376 int res
= mono_test_marshal_out_array (a1
, 0);
378 for (int i
= 0; i
< 50; i
++)
380 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
387 public static int test_0_marshal_inout_nonblittable_array () {
388 char [] a1
= new char [10];
389 for (int i
= 0; i
< 10; i
++)
390 a1
[i
] = "Hello, World" [i
];
392 int res
= mono_test_marshal_inout_nonblittable_array (a1
);
394 for (int i
= 0; i
< 10; i
++)
401 public static int test_0_marshal_struct () {
402 SimpleStruct ss
= new SimpleStruct ();
406 return mono_test_marshal_struct (ss
);
409 public static int test_0_marshal_struct_gen () {
410 SimpleStructGen
<string> ss
= new SimpleStructGen
<string> ();
414 return mono_test_marshal_struct_gen (ss
);
417 public static int test_0_marshal_struct2 () {
418 SimpleStruct2 ss2
= new SimpleStruct2 ();
426 return mono_test_marshal_struct2 (ss2
);
429 public static int test_0_marshal_struct3 () {
430 SimpleStruct2 ss2
= new SimpleStruct2 ();
438 return mono_test_marshal_struct2_2 (10, 11, 12, ss2
);
441 public static int test_0_marshal_empty_struct () {
442 EmptyStruct es
= new EmptyStruct ();
444 if (mono_test_empty_struct (1, es
, 2) != 0)
450 public static int test_0_marshal_lpstruct () {
451 SimpleStruct ss
= new SimpleStruct ();
455 return mono_test_marshal_lpstruct (ss
);
458 public static int test_0_marshal_lpstruct_blittable () {
459 Point p
= new Point ();
463 return mono_test_marshal_lpstruct_blittable (p
);
466 public static int test_0_marshal_struct_array () {
467 SimpleStruct2
[] ss_arr
= new SimpleStruct2
[2];
469 SimpleStruct2 ss2
= new SimpleStruct2 ();
488 return mono_test_marshal_struct_array (ss_arr
);
491 public static int test_105_marshal_long_align_struct_array () {
492 LongAlignStruct
[] ss_arr
= new LongAlignStruct
[2];
494 LongAlignStruct ss
= new LongAlignStruct ();
507 return mono_test_marshal_long_align_struct_array (ss_arr
);
510 /* Test classes as arguments and return values */
511 public static int test_0_marshal_class () {
512 SimpleClass ss
= new SimpleClass ();
520 SimpleClass res
= mono_test_marshal_class (10, 11, 12, ss
, 14);
523 if (! (res
.a
== ss
.a
&& res
.b
== ss
.b
&& res
.c
== ss
.c
&&
524 res
.d
== ss
.d
&& res
.e
== ss
.e
&& res
.f
== ss
.f
&&
525 res
.g
== ss
.g
&& res
.h
== ss
.h
))
528 /* Test null arguments and results */
529 res
= mono_test_marshal_class (10, 11, 12, null, 14);
536 public static int test_0_marshal_byref_class () {
537 SimpleClass ss
= new SimpleClass ();
545 int res
= mono_test_marshal_byref_class (ref ss
);
546 if (ss
.d
!= "TEST-RES")
552 public static int test_0_marshal_delegate () {
553 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
555 return mono_test_marshal_delegate (d
);
558 public static int test_34_marshal_instance_delegate () {
559 Tests t
= new Tests ();
561 SimpleDelegate d
= new SimpleDelegate (t
.delegate_test_instance
);
563 return mono_test_marshal_delegate (d
);
566 /* Static delegates closed over their first argument */
567 public static int closed_delegate (Tests t
, int a
) {
568 return t
.int_field
+ a
;
571 public static int test_34_marshal_closed_static_delegate () {
572 Tests t
= new Tests ();
574 SimpleDelegate d
= (SimpleDelegate
)Delegate
.CreateDelegate (typeof (SimpleDelegate
), t
, typeof (Tests
).GetMethod ("closed_delegate"));
576 return mono_test_marshal_delegate (d
);
579 public static int test_0_marshal_return_delegate () {
580 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
582 SimpleDelegate d2
= mono_test_marshal_return_delegate (d
);
587 public static int test_0_marshal_delegate_struct () {
588 DelegateStruct s
= new DelegateStruct ();
591 s
.del
= new SimpleDelegate (delegate_test
);
592 s
.del2
= new SimpleDelegate (delegate_test
);
595 DelegateStruct res
= mono_test_marshal_delegate_struct (s
);
599 if (res
.del (2) != 0)
601 if (res
.del2 (2) != 0)
603 if (res
.del3
!= null)
609 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_delegate")]
610 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d
);
612 public static int test_3_marshal_out_delegate () {
613 SimpleDelegate d
= null;
615 mono_test_marshal_out_delegate (out d
);
620 public static int test_0_marshal_byref_struct () {
621 SimpleStruct s
= new SimpleStruct ();
628 int res
= mono_test_marshal_byref_struct (ref s
, true, false, true, "ABC");
631 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
636 public static int test_0_marshal_byref_struct_in () {
637 SimpleStruct s
= new SimpleStruct ();
644 int res
= mono_test_marshal_byref_struct_in (ref s
, true, false, true, "ABC");
647 if (s
.a
!= true || s
.b
!= false || s
.c
!= true || s
.d
!= "ABC")
652 public static int test_0_marshal_byref_struct_inout () {
653 SimpleStruct s
= new SimpleStruct ();
660 int res
= mono_test_marshal_byref_struct_inout (ref s
, true, false, true, "ABC");
663 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
668 public static int test_0_marshal_point () {
669 Point pt
= new Point();
673 return mono_test_marshal_point(pt
);
676 public static int test_0_marshal_mixed_point () {
677 MixedPoint mpt
= new MixedPoint();
681 return mono_test_marshal_mixed_point(mpt
);
684 public static int test_0_marshal_bool_byref () {
686 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 1)
689 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 0)
697 public static int test_0_marshal_bool_as_I1 () {
701 if ((ret
= mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
704 if ((ret
= mono_test_marshal_bool_out_as_I1 (out bTrue
, out bFalse
)) != 0)
713 if ((ret
= mono_test_marshal_bool_ref_as_I1 (ref bTrue
, ref bFalse
)) != 0)
725 public static int test_0_marshal_bool_as_U1 () {
729 if ((ret
= mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
732 if ((ret
= mono_test_marshal_bool_out_as_U1 (out bTrue
, out bFalse
)) != 0)
741 if ((ret
= mono_test_marshal_bool_ref_as_U1 (ref bTrue
, ref bFalse
)) != 0)
753 public static int test_0_return_vtype () {
754 SimpleStruct ss
= mono_test_return_vtype (new IntPtr (5));
756 if (!ss
.a
&& ss
.b
&& !ss
.c
&& ss
.d
== "TEST" && ss
.d2
== "TEST2")
762 public static int test_0_marshal_stringbuilder () {
763 StringBuilder sb
= new StringBuilder(255);
765 mono_test_marshal_stringbuilder (sb
, sb
.Capacity
);
766 String res
= sb
.ToString();
768 if (res
!= "This is my message. Isn't it nice?")
771 // Test StringBuilder with default capacity (16)
772 StringBuilder sb2
= new StringBuilder();
773 mono_test_marshal_stringbuilder_default (sb2
, sb2
.Capacity
);
774 if (sb2
.ToString () != "This is my messa")
780 public static int test_0_marshal_stringbuilder_unicode () {
781 StringBuilder sb
= new StringBuilder(255);
782 mono_test_marshal_stringbuilder_unicode (sb
, sb
.Capacity
);
783 String res
= sb
.ToString();
785 if (res
!= "This is my message. Isn't it nice?")
788 // Test StringBuilder with default capacity (16)
789 StringBuilder sb2
= new StringBuilder();
790 mono_test_marshal_stringbuilder_unicode (sb2
, sb2
.Capacity
);
791 if (sb2
.ToString () != "This is my messa")
797 public static int test_0_marshal_stringbuilder_out () {
799 mono_test_marshal_stringbuilder_out (out sb
);
801 if (sb
.ToString () != "This is my message. Isn't it nice?")
806 public static int test_0_marshal_stringbuilder_out_unicode () {
808 mono_test_marshal_stringbuilder_out_unicode (out sb
);
810 if (sb
.ToString () != "This is my message. Isn't it nice?")
815 public static int test_0_marshal_empty_string_array () {
816 return mono_test_marshal_empty_string_array (null);
819 public static int test_0_marshal_string_array () {
820 return mono_test_marshal_string_array (new String
[] { "ABC", "DEF" }
);
823 public static int test_0_marshal_unicode_string_array () {
824 return mono_test_marshal_unicode_string_array (new String
[] { "ABC", "DEF" }
, new String
[] { "ABC", "DEF" }
);
827 public static int test_0_marshal_stringbuilder_array () {
828 StringBuilder sb1
= new StringBuilder ("ABC");
829 StringBuilder sb2
= new StringBuilder ("DEF");
831 int res
= mono_test_marshal_stringbuilder_array (new StringBuilder
[] { sb1, sb2 }
);
834 if (sb1
.ToString () != "DEF")
836 if (sb2
.ToString () != "ABC")
841 public static int test_0_last_error () {
842 mono_test_last_error (5);
843 if (Marshal
.GetLastWin32Error () == 5)
849 public static int test_0_entry_point_not_found () {
852 mono_entry_point_not_found ();
855 catch (EntryPointNotFoundException
) {
861 public static int test_0_library_not_found () {
864 mono_library_not_found ();
867 catch (DllNotFoundException
) {
873 /* Check that the runtime trims .dll from the library name */
874 public static int test_0_trim_dll_from_name () {
876 mono_test_marshal_char_2 ('A');
881 /* Check that the runtime adds lib to to the library name */
882 public static int test_0_add_lib_to_name () {
884 mono_test_marshal_char_3 ('A');
893 public static int test_0_asany () {
894 if (mono_test_asany (5, 1) != 0)
897 if (mono_test_asany ("ABC", 2) != 0)
900 SimpleStruct2 ss2
= new SimpleStruct2 ();
908 if (mono_test_asany (ss2
, 3) != 0)
911 if (mono_test_asany_unicode ("ABC", 4) != 0)
917 mono_test_asany (c
, 0);
920 catch (ArgumentException
) {
924 mono_test_asany (new Object (), 0);
927 catch (ArgumentException
) {
933 /* AsAny marshalling + [In, Out] */
935 public static int test_0_asany_in () {
937 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
938 mono_test_asany_in (str
);
941 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
942 mono_test_asany_in (cls
);
943 if ((cls
.i
!= 1) || (cls
.j
!= 2) || (cls
.k
!= 3))
947 object obj
= new AsAnyStruct(1,2,3, "ABC");
948 mono_test_asany_in (obj
);
949 str
= (AsAnyStruct
)obj
;
950 if ((str
.i
!= 1) || (str
.j
!= 2) || (str
.k
!= 3))
956 public static int test_0_asany_out () {
958 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
959 mono_test_asany_out (str
);
962 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
963 mono_test_asany_out (cls
);
964 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
968 object obj
= new AsAnyStruct(1,2,3, "ABC");
969 mono_test_asany_out (obj
);
970 str
= (AsAnyStruct
)obj
;
971 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
977 public static int test_0_asany_inout () {
979 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
980 mono_test_asany_inout (str
);
983 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
984 mono_test_asany_inout (cls
);
985 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
989 object obj
= new AsAnyStruct(1,2,3, "ABC");
990 mono_test_asany_inout (obj
);
991 str
= (AsAnyStruct
)obj
;
992 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
998 /* Byref String Array */
1000 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_string_array")]
1001 public static extern int mono_test_marshal_byref_string_array (ref string[] data
);
1003 public static int test_0_byref_string_array () {
1005 string[] arr
= null;
1007 if (mono_test_marshal_byref_string_array (ref arr
) != 0)
1010 arr
= new string[] { "Alpha", "Beta", "Gamma" }
;
1012 if (mono_test_marshal_byref_string_array (ref arr
) != 1)
1015 /* FIXME: Test returned array and out case */
1021 * AMD64 small structs-by-value tests.
1024 /* TEST 1: 16 byte long INTEGER struct */
1026 [StructLayout(LayoutKind
.Sequential
)]
1027 public struct Amd64Struct1
{
1034 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1")]
1035 public static extern Amd64Struct1
mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s
);
1037 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1038 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
);
1040 public static int test_0_amd64_struct1 () {
1041 Amd64Struct1 s
= new Amd64Struct1 ();
1047 Amd64Struct1 s2
= mono_test_marshal_amd64_pass_return_struct1 (s
);
1049 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000)) ? 0 : 1;
1052 public static int test_0_amd64_struct1_many_args () {
1053 Amd64Struct1 s
= new Amd64Struct1 ();
1059 Amd64Struct1 s2
= mono_test_marshal_amd64_pass_return_struct1_many_args (s
, 1, 2, 3, 4, 5, 6, 7, 8);
1061 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1064 /* TEST 2: 8 byte long INTEGER struct */
1066 [StructLayout(LayoutKind
.Sequential
)]
1067 public struct Amd64Struct2
{
1072 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct2")]
1073 public static extern Amd64Struct2
mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s
);
1075 public static int test_0_amd64_struct2 () {
1076 Amd64Struct2 s
= new Amd64Struct2 ();
1080 Amd64Struct2 s2
= mono_test_marshal_amd64_pass_return_struct2 (s
);
1082 return ((s2
.i
== 6) && (s2
.j
== -4)) ? 0 : 1;
1085 /* TEST 3: 4 byte long INTEGER struct */
1087 [StructLayout(LayoutKind
.Sequential
)]
1088 public struct Amd64Struct3
{
1092 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct3")]
1093 public static extern Amd64Struct3
mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s
);
1095 public static int test_0_amd64_struct3 () {
1096 Amd64Struct3 s
= new Amd64Struct3 ();
1099 Amd64Struct3 s2
= mono_test_marshal_amd64_pass_return_struct3 (s
);
1101 return (s2
.i
== -4) ? 0 : 1;
1104 /* Test 4: 16 byte long FLOAT struct */
1106 [StructLayout(LayoutKind
.Sequential
)]
1107 public struct Amd64Struct4
{
1108 public double d1
, d2
;
1111 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct4")]
1112 public static extern Amd64Struct4
mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s
);
1114 public static int test_0_amd64_struct4 () {
1115 Amd64Struct4 s
= new Amd64Struct4 ();
1119 Amd64Struct4 s2
= mono_test_marshal_amd64_pass_return_struct4 (s
);
1121 return (s2
.d1
== 6.0 && s2
.d2
== -4.0) ? 0 : 1;
1128 /* Test 5: Float HFA */
1130 [StructLayout(LayoutKind
.Sequential
)]
1131 public struct TestStruct5
{
1132 public float d1
, d2
;
1135 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct5")]
1136 public static extern TestStruct5
mono_test_marshal_ia64_pass_return_struct5 (double d1
, double d2
, TestStruct5 s
, int i
, double f3
, double f4
);
1138 public static int test_0_ia64_struct5 () {
1139 TestStruct5 s
= new TestStruct5 ();
1143 TestStruct5 s2
= mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s
, 5, 3.0, 4.0);
1145 return (s2
.d1
== 13.0 && s2
.d2
== 7.0) ? 0 : 1;
1148 /* Test 6: Double HFA */
1150 [StructLayout(LayoutKind
.Sequential
)]
1151 public struct TestStruct6
{
1152 public double d1
, d2
;
1155 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct6")]
1156 public static extern TestStruct6
mono_test_marshal_ia64_pass_return_struct6 (double d1
, double d2
, TestStruct6 s
, int i
, double f3
, double f4
);
1158 public static int test_0_ia64_struct6 () {
1159 TestStruct6 s
= new TestStruct6 ();
1163 TestStruct6 s2
= mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s
, 3, 4.0, 5.0);
1165 return (s2
.d1
== 12.0 && s2
.d2
== 3.0) ? 0 : 1;
1168 /* Blittable class */
1169 [DllImport("libtest")]
1170 private static extern BlittableClass
TestBlittableClass (BlittableClass vl
);
1172 public static int test_0_marshal_blittable_class () {
1173 BlittableClass v1
= new BlittableClass ();
1175 /* Since it is blittable, it looks like it is passed as in/out */
1176 BlittableClass v2
= TestBlittableClass (v1
);
1178 if (v1
.a
!= 2 || v1
.b
!= 3)
1181 if (v2
.a
!= 2 || v2
.b
!= 3)
1185 BlittableClass v3
= TestBlittableClass (null);
1187 if (v3
.a
!= 42 || v3
.b
!= 43)
1194 * Generic structures
1197 [StructLayout(LayoutKind
.Sequential
)]
1198 public struct Amd64Struct1Gen
<T
> {
1205 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1")]
1206 public static extern Amd64Struct1Gen
<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen
<int> s
);
1208 public static int test_0_amd64_struct1_gen () {
1209 Amd64Struct1Gen
<int> s
= new Amd64Struct1Gen
<int> ();
1215 Amd64Struct1Gen
<int> s2
= mono_test_marshal_amd64_pass_return_struct1_gen (s
);
1217 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000)) ? 0 : 1;
1224 public static int test_0_marshal_byval_class () {
1225 SimpleObj obj0
= new SimpleObj ();
1229 if (class_marshal_test0 (obj0
) != 0)
1235 public static int test_0_marshal_byval_class_null () {
1236 if (class_marshal_test4 (null) != 0)
1242 public static int test_0_marshal_out_class () {
1245 class_marshal_test1 (out obj1
);
1247 if (obj1
.str
!= "ABC")
1256 public static int test_0_marshal_string () {
1257 return string_marshal_test0 ("TEST0");
1260 public static int test_0_marshal_out_string () {
1263 string_marshal_test1 (out res
);
1271 public static int test_0_marshal_byref_string () {
1272 string res
= "TEST1";
1274 return string_marshal_test2 (ref res
);
1277 public static int test_0_marshal_null_string () {
1278 return string_marshal_test3 (null);
1281 [DllImport ("libtest", EntryPoint
="mono_test_stdcall_name_mangling", CallingConvention
=CallingConvention
.StdCall
)]
1282 public static extern int mono_test_stdcall_name_mangling (int a
, int b
, int c
);
1284 public static int test_0_stdcall_name_mangling () {
1285 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1290 [DllImport ("libtest", EntryPoint
="mono_test_marshal_pass_return_float")]
1291 public static extern float mono_test_marshal_pass_return_float (float f
);
1293 public static int test_0_pass_return_float () {
1294 float f
= mono_test_marshal_pass_return_float (1.5f
);
1296 return (f
== 2.5f
) ? 0 : 1;
1300 * Pointers to structures can not be passed
1303 public struct CharInfo
{
1304 public char Character
;
1305 public short Attributes
;
1308 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
1309 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo
*ptr
);
1311 public static unsafe int test_0_marshal_ptr_to_struct () {
1312 CharInfo
[] buffer
= new CharInfo
[1];
1313 fixed (CharInfo
*ptr
= &buffer
[0]) {
1315 mono_test_marshal_ptr_to_struct (ptr
);
1318 catch (MarshalDirectiveException
) {
1326 * LPWStr marshalling
1329 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal")]
1330 [return: MarshalAs(UnmanagedType
.LPWStr
)]
1331 private static extern string mono_test_marshal_lpwstr_marshal(
1332 [MarshalAs(UnmanagedType
.LPWStr
)] string s
,
1335 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal", CharSet
=CharSet
.Unicode
)]
1336 private static extern string mono_test_marshal_lpwstr_marshal2(
1340 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal_out")]
1341 private static extern void mono_test_marshal_lpwstr_out_marshal(
1342 [MarshalAs(UnmanagedType
.LPWStr
)] out string s
);
1344 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal_out", CharSet
=CharSet
.Unicode
)]
1345 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1348 public static int test_0_pass_return_lwpstr () {
1351 mono_test_marshal_lpwstr_out_marshal (out s
);
1357 mono_test_marshal_lpwstr_out_marshal2 (out s
);
1365 public static int test_0_out_lwpstr () {
1368 string res
= mono_test_marshal_lpwstr_marshal (s
, s
.Length
);
1373 string res2
= mono_test_marshal_lpwstr_marshal2 (s
, s
.Length
);
1382 * Byref bool marshalling
1385 [DllImport("libtest")]
1386 extern static int marshal_test_ref_bool
1389 [MarshalAs(UnmanagedType
.I1
)] ref bool b1
,
1390 [MarshalAs(UnmanagedType
.VariantBool
)] ref bool b2
,
1394 public static int test_0_pass_byref_bool () {
1395 for (int i
= 0; i
< 8; i
++)
1397 bool b1
= (i
& 4) != 0;
1398 bool b2
= (i
& 2) != 0;
1399 bool b3
= (i
& 1) != 0;
1400 bool orig_b1
= b1
, orig_b2
= b2
, orig_b3
= b3
;
1401 if (marshal_test_ref_bool(i
, ref b1
, ref b2
, ref b3
) != 0)
1415 * Bool struct field marshalling
1421 [MarshalAs(UnmanagedType
.I1
)] public bool b1
;
1422 [MarshalAs(UnmanagedType
.VariantBool
)] public bool b2
;
1426 [DllImport("libtest")]
1427 extern static int marshal_test_bool_struct(ref BoolStruct s
);
1429 public static int test_0_pass_bool_in_struct () {
1430 for (int i
= 0; i
< 8; i
++)
1432 BoolStruct s
= new BoolStruct();
1434 s
.b1
= (i
& 4) != 0;
1435 s
.b2
= (i
& 2) != 0;
1436 s
.b3
= (i
& 1) != 0;
1437 BoolStruct orig
= s
;
1438 if (marshal_test_bool_struct(ref s
) != 0)
1440 if (s
.b1
!= !orig
.b1
)
1442 if (s
.b2
!= !orig
.b2
)
1444 if (s
.b3
!= !orig
.b3
)
1452 * Alignment of structs containing longs
1455 struct LongStruct2
{
1461 public LongStruct2 l
;
1464 [DllImport("libtest")]
1465 extern static int mono_test_marshal_long_struct (ref LongStruct s
);
1467 public static int test_47_pass_long_struct () {
1468 LongStruct s
= new LongStruct ();
1470 s
.l
= new LongStruct2 ();
1473 return mono_test_marshal_long_struct (ref s
);
1477 * Invoking pinvoke methods through delegates
1480 delegate int MyDelegate (string name
);
1482 [DllImport ("libtest", EntryPoint
="mono_test_puts_static")]
1483 public static extern int puts_static (string name
);
1485 public static int test_0_invoke_pinvoke_through_delegate () {
1486 puts_static ("A simple Test for PInvoke 1");
1488 MyDelegate d
= new MyDelegate (puts_static
);
1489 d ("A simple Test for PInvoke 2");
1491 object [] args
= {"A simple Test for PInvoke 3"}
;
1492 d
.DynamicInvoke (args
);
1498 * Missing virtual pinvoke methods
1503 public virtual object MyClone ()
1509 public class T2
: T
{
1510 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
1511 public override extern object MyClone ();
1514 public static int test_0_missing_virtual_pinvoke_method () {
1519 } catch (Exception ex
) {
1527 * Marshalling of type 'object'
1530 [DllImport ("libtest", EntryPoint
="mono_test_marshal_class")]
1531 public static extern SimpleClass
mono_test_marshal_object (int i
, int j
, int k
, object ss
, int l
);
1533 public static int test_0_marshal_object () {
1535 mono_test_marshal_object (0, 0, 0, null, 0);
1537 } catch (Exception
) {
1543 * Marshalling of DateTime to OLE DATE (double)
1545 [DllImport ("libtest", EntryPoint
="mono_test_marshal_date_time")]
1546 public static extern double mono_test_marshal_date_time (DateTime d
, out DateTime d2
);
1548 public static int test_0_marshal_date_time () {
1549 DateTime d
= new DateTime (2009, 12, 6);
1551 double d3
= mono_test_marshal_date_time (d
, out d2
);
1560 * Calling pinvoke functions dynamically using calli
1563 [DllImport("libtest")]
1564 private static extern IntPtr
mono_test_marshal_lookup_symbol (string fileName
);
1566 delegate void CalliDel (IntPtr a
, int[] f
);
1568 public static int test_0_calli_dynamic () {
1569 /* we need the cdecl version because the icall convention demands it under Windows */
1570 IntPtr func
= mono_test_marshal_lookup_symbol ("mono_test_marshal_inout_array_cdecl");
1572 DynamicMethod dm
= new DynamicMethod ("calli", typeof (void), new Type
[] { typeof (IntPtr), typeof (int[]) }
);
1574 var il
= dm
.GetILGenerator ();
1575 var signature
= SignatureHelper
.GetMethodSigHelper (CallingConvention
.Cdecl
, typeof (void));
1577 il
.Emit (OpCodes
.Ldarg
, 1);
1578 signature
.AddArgument (typeof (byte[]));
1580 il
.Emit (OpCodes
.Ldarg_0
);
1582 il
.Emit (OpCodes
.Calli
, signature
);
1583 il
.Emit (OpCodes
.Ret
);
1585 var f
= (CalliDel
)dm
.CreateDelegate (typeof (CalliDel
));
1587 int[] arr
= new int [1000];
1588 for (int i
= 0; i
< 50; ++i
)
1591 if (arr
.Length
!= 1000)
1593 for (int i
= 0; i
< 50; ++i
)
1594 if (arr
[i
] != 50 - i
)
1601 /*char array marshaling */
1602 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ansi_char_array", CharSet
=CharSet
.Ansi
)]
1603 public static extern int mono_test_marshal_ansi_char_array (char[] a1
);
1605 public static int test_0_marshal_ansi_char_array () {
1606 char[] buf
= new char [32];
1612 if (mono_test_marshal_ansi_char_array (buf
) != 0)
1615 string s
= new string (buf
);
1616 if (s
.StartsWith ("qwer"))
1622 /*char array marshaling */
1623 [DllImport ("libtest", EntryPoint
="mono_test_marshal_unicode_char_array", CharSet
=CharSet
.Unicode
)]
1624 public static extern int mono_test_marshal_unicode_char_array (char[] a1
);
1626 public static int test_0_marshal_unicode_char_array () {
1627 char[] buf
= new char [32];
1633 if (mono_test_marshal_unicode_char_array (buf
) != 0)
1636 string s
= new string (buf
);
1637 if (s
.StartsWith ("abcdef"))