3 using System
.Runtime
.InteropServices
;
4 using System
.Runtime
.CompilerServices
;
10 public static int delegate_test (int a
)
18 public int delegate_test_instance (int a
)
23 [StructLayout (LayoutKind
.Sequential
)]
24 public struct SimpleStruct
{
29 [MarshalAs(UnmanagedType
.LPWStr
)]
33 [StructLayout (LayoutKind
.Sequential
)]
34 public struct SimpleStructGen
<T
> {
39 [MarshalAs(UnmanagedType
.LPWStr
)]
43 [StructLayout (LayoutKind
.Sequential
)]
44 public struct SimpleStruct2
{
55 [StructLayout (LayoutKind
.Sequential
, Size
=0)]
56 public struct EmptyStruct
{
59 [StructLayout (LayoutKind
.Sequential
)]
60 public struct DelegateStruct
{
62 public SimpleDelegate del
;
63 [MarshalAs(UnmanagedType
.FunctionPtr
)]
64 public SimpleDelegate del2
;
65 [MarshalAs(UnmanagedType
.FunctionPtr
)]
66 public SimpleDelegate del3
;
69 /* sparcv9 has complex conventions when passing structs with doubles in them
70 by value, some simple tests for them */
71 [StructLayout (LayoutKind
.Sequential
)]
77 [StructLayout (LayoutKind
.Sequential
)]
78 public struct MixedPoint
{
83 [StructLayout (LayoutKind
.Sequential
)]
84 public class SimpleClass
{
95 [StructLayout (LayoutKind
.Sequential
)]
96 public class EmptyClass
{
99 [StructLayout (LayoutKind
.Sequential
)]
100 public struct LongAlignStruct
{
106 [StructLayout(LayoutKind
.Sequential
)]
107 public class BlittableClass
113 [StructLayout (LayoutKind
.Sequential
)]
120 [StructLayout(LayoutKind
.Sequential
)]
128 public AsAnyStruct(int i
, int j
, int k
, String s
) {
136 [StructLayout(LayoutKind
.Sequential
)]
144 public AsAnyClass(int i
, int j
, int k
, String s
) {
151 [DllImport ("libnot-found", EntryPoint
="not_found")]
152 public static extern int mono_library_not_found ();
154 [DllImport ("libtest", EntryPoint
="not_found")]
155 public static extern int mono_entry_point_not_found ();
157 [DllImport ("libtest.dll", EntryPoint
="mono_test_marshal_char")]
158 public static extern int mono_test_marshal_char_2 (char a1
);
160 [DllImport ("test", EntryPoint
="mono_test_marshal_char")]
161 public static extern int mono_test_marshal_char_3 (char a1
);
163 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char")]
164 public static extern int mono_test_marshal_char (char a1
);
166 [DllImport ("libtest", EntryPoint
="mono_test_marshal_char_array", CharSet
=CharSet
.Unicode
)]
167 public static extern int mono_test_marshal_char_array (char[] a1
);
169 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_byref")]
170 public static extern int mono_test_marshal_bool_byref (int a
, ref bool b
, int c
);
172 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_in_as_I1_U1")]
173 public static extern int mono_test_marshal_bool_in_as_I1 ([MarshalAs (UnmanagedType
.I1
)] bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] bool bFalse
);
175 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_in_as_I1_U1")]
176 public static extern int mono_test_marshal_bool_in_as_U1 ([MarshalAs (UnmanagedType
.U1
)] bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] bool bFalse
);
178 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_out_as_I1_U1")]
179 public static extern int mono_test_marshal_bool_out_as_I1 ([MarshalAs (UnmanagedType
.I1
)] out bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] out bool bFalse
);
181 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_out_as_I1_U1")]
182 public static extern int mono_test_marshal_bool_out_as_U1 ([MarshalAs (UnmanagedType
.U1
)] out bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] out bool bFalse
);
184 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_ref_as_I1_U1")]
185 public static extern int mono_test_marshal_bool_ref_as_I1 ([MarshalAs (UnmanagedType
.I1
)] ref bool bTrue
, [MarshalAs (UnmanagedType
.I1
)] ref bool bFalse
);
187 [DllImport ("libtest", EntryPoint
="mono_test_marshal_bool_ref_as_I1_U1")]
188 public static extern int mono_test_marshal_bool_ref_as_U1 ([MarshalAs (UnmanagedType
.U1
)] ref bool bTrue
, [MarshalAs (UnmanagedType
.U1
)] ref bool bFalse
);
190 [DllImport ("libtest", EntryPoint
="mono_test_marshal_array")]
191 public static extern int mono_test_marshal_array (int [] a1
);
193 [DllImport ("libtest", EntryPoint
="mono_test_marshal_empty_string_array")]
194 public static extern int mono_test_marshal_empty_string_array (string [] a1
);
196 [DllImport ("libtest", EntryPoint
="mono_test_marshal_string_array")]
197 public static extern int mono_test_marshal_string_array (string [] a1
);
199 [DllImport ("libtest", EntryPoint
="mono_test_marshal_unicode_string_array", CharSet
=CharSet
.Unicode
)]
200 public static extern int mono_test_marshal_unicode_string_array (string [] a1
, [MarshalAs(UnmanagedType
.LPArray
, ArraySubType
=UnmanagedType
.LPStr
)]string [] a2
);
202 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_array")]
203 public static extern int mono_test_marshal_stringbuilder_array (StringBuilder
[] a1
);
205 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_array")]
206 public static extern int mono_test_marshal_inout_array ([In
, Out
] int [] a1
);
208 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_array")]
209 public static extern int mono_test_marshal_out_array ([Out
] [MarshalAs(UnmanagedType
.LPArray
, SizeParamIndex
= 1)] int [] a1
, int n
);
211 [DllImport ("libtest", EntryPoint
="mono_test_marshal_inout_nonblittable_array", CharSet
= CharSet
.Unicode
)]
212 public static extern int mono_test_marshal_inout_nonblittable_array ([In
, Out
] char [] a1
);
214 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
215 public static extern int mono_test_marshal_struct (SimpleStruct ss
);
217 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
218 public static extern int mono_test_marshal_struct_gen (SimpleStructGen
<string> ss
);
220 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2")]
221 public static extern int mono_test_marshal_struct2 (SimpleStruct2 ss
);
223 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct2_2")]
224 public static extern int mono_test_marshal_struct2_2 (int i
, int j
, int k
, SimpleStruct2 ss
);
226 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
227 public static extern int mono_test_marshal_byref_struct (ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
229 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
230 public static extern int mono_test_marshal_byref_struct_in ([In
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
232 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_struct")]
233 public static extern int mono_test_marshal_byref_struct_inout ([In
, Out
] ref SimpleStruct ss
, bool a
, bool b
, bool c
, String d
);
235 [DllImport ("libtest", EntryPoint
="mono_test_marshal_point")]
236 public static extern int mono_test_marshal_point (Point p
);
238 [DllImport ("libtest", EntryPoint
="mono_test_marshal_mixed_point")]
239 public static extern int mono_test_marshal_mixed_point (MixedPoint p
);
241 [DllImport ("libtest", EntryPoint
="mono_test_empty_struct")]
242 public static extern int mono_test_empty_struct (int a
, EmptyStruct es
, int b
);
244 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct")]
245 public static extern int mono_test_marshal_lpstruct ([In
, MarshalAs(UnmanagedType
.LPStruct
)] SimpleStruct ss
);
247 [DllImport ("libtest", EntryPoint
="mono_test_marshal_lpstruct_blittable")]
248 public static extern int mono_test_marshal_lpstruct_blittable ([In
, MarshalAs(UnmanagedType
.LPStruct
)] Point p
);
250 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct_array")]
251 public static extern int mono_test_marshal_struct_array (SimpleStruct2
[] ss
);
253 [DllImport ("libtest", EntryPoint
="mono_test_marshal_long_align_struct_array")]
254 public static extern int mono_test_marshal_long_align_struct_array (LongAlignStruct
[] ss
);
256 [DllImport ("libtest", EntryPoint
="mono_test_marshal_class")]
257 public static extern SimpleClass
mono_test_marshal_class (int i
, int j
, int k
, SimpleClass ss
, int l
);
259 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_class")]
260 public static extern int mono_test_marshal_byref_class (ref SimpleClass ss
);
262 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate")]
263 public static extern int mono_test_marshal_delegate (SimpleDelegate d
);
265 [DllImport ("libtest", EntryPoint
="mono_test_marshal_delegate_struct")]
266 public static extern DelegateStruct
mono_test_marshal_delegate_struct (DelegateStruct d
);
268 [DllImport ("libtest", EntryPoint
="mono_test_marshal_return_delegate")]
269 public static extern SimpleDelegate
mono_test_marshal_return_delegate (SimpleDelegate d
);
271 [DllImport ("libtest", EntryPoint
="mono_test_return_vtype")]
272 public static extern SimpleStruct
mono_test_return_vtype (IntPtr i
);
274 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder")]
275 public static extern void mono_test_marshal_stringbuilder (StringBuilder sb
, int len
);
277 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_default")]
278 public static extern void mono_test_marshal_stringbuilder_default (StringBuilder sb
, int len
);
280 [DllImport ("libtest", EntryPoint
="mono_test_marshal_stringbuilder_unicode", CharSet
=CharSet
.Unicode
)]
281 public static extern void mono_test_marshal_stringbuilder_unicode (StringBuilder sb
, int len
);
283 [DllImport ("libtest", EntryPoint
="mono_test_last_error", SetLastError
=true)]
284 public static extern void mono_test_last_error (int err
);
286 [DllImport ("libtest", EntryPoint
="mono_test_asany")]
287 public static extern int mono_test_asany ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
289 [DllImport ("libtest", EntryPoint
="mono_test_asany", CharSet
=CharSet
.Unicode
)]
290 public static extern int mono_test_asany_unicode ([MarshalAs (UnmanagedType
.AsAny
)] object o
, int what
);
292 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_in")]
293 static extern void mono_test_asany_in ([MarshalAs(UnmanagedType
.AsAny
)][In
] object obj
);
295 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_out")]
296 static extern void mono_test_asany_out ([MarshalAs(UnmanagedType
.AsAny
)][Out
] object obj
);
297 [DllImport("libtest", EntryPoint
="mono_test_marshal_asany_inout")]
298 static extern void mono_test_asany_inout ([MarshalAs(UnmanagedType
.AsAny
)][In
, Out
] object obj
);
300 [DllImport ("libtest")]
301 static extern int class_marshal_test0 (SimpleObj obj
);
303 [DllImport ("libtest")]
304 static extern void class_marshal_test1 (out SimpleObj obj
);
306 [DllImport ("libtest")]
307 static extern int class_marshal_test4 (SimpleObj obj
);
309 [DllImport ("libtest")]
310 static extern int string_marshal_test0 (string str
);
312 [DllImport ("libtest")]
313 static extern void string_marshal_test1 (out string str
);
315 [DllImport ("libtest")]
316 static extern int string_marshal_test2 (ref string str
);
318 [DllImport ("libtest")]
319 static extern int string_marshal_test3 (string str
);
321 public delegate int SimpleDelegate (int a
);
323 public static int Main (string[] args
) {
324 return TestDriver
.RunTests (typeof (Tests
), args
);
327 public static int test_0_marshal_char () {
328 return mono_test_marshal_char ('a');
331 public static int test_0_marshal_char_array () {
332 // a unicode char[] is implicitly marshalled as [Out]
333 char[] buf
= new char [32];
334 mono_test_marshal_char_array (buf
);
335 string s
= new string (buf
);
336 if (s
.StartsWith ("abcdef"))
342 public static int test_1225_marshal_array () {
343 int [] a1
= new int [50];
344 for (int i
= 0; i
< 50; i
++)
347 return mono_test_marshal_array (a1
);
350 public static int test_1225_marshal_inout_array () {
351 int [] a1
= new int [50];
352 for (int i
= 0; i
< 50; i
++)
355 int res
= mono_test_marshal_inout_array (a1
);
357 for (int i
= 0; i
< 50; i
++)
358 if (a1
[i
] != 50 - i
) {
359 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
366 public static int test_0_marshal_out_array () {
367 int [] a1
= new int [50];
369 int res
= mono_test_marshal_out_array (a1
, 0);
371 for (int i
= 0; i
< 50; i
++)
373 Console
.WriteLine ("X: " + i
+ " " + a1
[i
]);
380 public static int test_0_marshal_inout_nonblittable_array () {
381 char [] a1
= new char [10];
382 for (int i
= 0; i
< 10; i
++)
383 a1
[i
] = "Hello, World" [i
];
385 int res
= mono_test_marshal_inout_nonblittable_array (a1
);
387 for (int i
= 0; i
< 10; i
++)
394 public static int test_0_marshal_struct () {
395 SimpleStruct ss
= new SimpleStruct ();
399 return mono_test_marshal_struct (ss
);
402 public static int test_0_marshal_struct_gen () {
403 SimpleStructGen
<string> ss
= new SimpleStructGen
<string> ();
407 return mono_test_marshal_struct_gen (ss
);
410 public static int test_0_marshal_struct2 () {
411 SimpleStruct2 ss2
= new SimpleStruct2 ();
419 return mono_test_marshal_struct2 (ss2
);
422 public static int test_0_marshal_struct3 () {
423 SimpleStruct2 ss2
= new SimpleStruct2 ();
431 return mono_test_marshal_struct2_2 (10, 11, 12, ss2
);
434 public static int test_0_marshal_empty_struct () {
435 EmptyStruct es
= new EmptyStruct ();
437 if (mono_test_empty_struct (1, es
, 2) != 0)
443 public static int test_0_marshal_lpstruct () {
444 SimpleStruct ss
= new SimpleStruct ();
448 return mono_test_marshal_lpstruct (ss
);
451 public static int test_0_marshal_lpstruct_blittable () {
452 Point p
= new Point ();
456 return mono_test_marshal_lpstruct_blittable (p
);
459 public static int test_0_marshal_struct_array () {
460 SimpleStruct2
[] ss_arr
= new SimpleStruct2
[2];
462 SimpleStruct2 ss2
= new SimpleStruct2 ();
481 return mono_test_marshal_struct_array (ss_arr
);
484 public static int test_105_marshal_long_align_struct_array () {
485 LongAlignStruct
[] ss_arr
= new LongAlignStruct
[2];
487 LongAlignStruct ss
= new LongAlignStruct ();
500 return mono_test_marshal_long_align_struct_array (ss_arr
);
503 /* Test classes as arguments and return values */
504 public static int test_0_marshal_class () {
505 SimpleClass ss
= new SimpleClass ();
513 SimpleClass res
= mono_test_marshal_class (10, 11, 12, ss
, 14);
516 if (! (res
.a
== ss
.a
&& res
.b
== ss
.b
&& res
.c
== ss
.c
&&
517 res
.d
== ss
.d
&& res
.e
== ss
.e
&& res
.f
== ss
.f
&&
518 res
.g
== ss
.g
&& res
.h
== ss
.h
))
521 /* Test null arguments and results */
522 res
= mono_test_marshal_class (10, 11, 12, null, 14);
529 public static int test_0_marshal_byref_class () {
530 SimpleClass ss
= new SimpleClass ();
538 int res
= mono_test_marshal_byref_class (ref ss
);
539 if (ss
.d
!= "TEST-RES")
545 public static int test_0_marshal_delegate () {
546 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
548 return mono_test_marshal_delegate (d
);
551 public static int test_34_marshal_instance_delegate () {
552 Tests t
= new Tests ();
554 SimpleDelegate d
= new SimpleDelegate (t
.delegate_test_instance
);
556 return mono_test_marshal_delegate (d
);
559 public static int test_0_marshal_return_delegate () {
560 SimpleDelegate d
= new SimpleDelegate (delegate_test
);
562 SimpleDelegate d2
= mono_test_marshal_return_delegate (d
);
567 public static int test_0_marshal_delegate_struct () {
568 DelegateStruct s
= new DelegateStruct ();
571 s
.del
= new SimpleDelegate (delegate_test
);
572 s
.del2
= new SimpleDelegate (delegate_test
);
575 DelegateStruct res
= mono_test_marshal_delegate_struct (s
);
579 if (res
.del (2) != 0)
581 if (res
.del2 (2) != 0)
583 if (res
.del3
!= null)
589 [DllImport ("libtest", EntryPoint
="mono_test_marshal_out_delegate")]
590 public static extern int mono_test_marshal_out_delegate (out SimpleDelegate d
);
592 public static int test_3_marshal_out_delegate () {
593 SimpleDelegate d
= null;
595 mono_test_marshal_out_delegate (out d
);
600 public static int test_0_marshal_byref_struct () {
601 SimpleStruct s
= new SimpleStruct ();
608 int res
= mono_test_marshal_byref_struct (ref s
, true, false, true, "ABC");
611 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
616 public static int test_0_marshal_byref_struct_in () {
617 SimpleStruct s
= new SimpleStruct ();
624 int res
= mono_test_marshal_byref_struct_in (ref s
, true, false, true, "ABC");
627 if (s
.a
!= true || s
.b
!= false || s
.c
!= true || s
.d
!= "ABC")
632 public static int test_0_marshal_byref_struct_inout () {
633 SimpleStruct s
= new SimpleStruct ();
640 int res
= mono_test_marshal_byref_struct_inout (ref s
, true, false, true, "ABC");
643 if (s
.a
!= false || s
.b
!= true || s
.c
!= false || s
.d
!= "DEF")
648 public static int test_0_marshal_point () {
649 Point pt
= new Point();
653 return mono_test_marshal_point(pt
);
656 public static int test_0_marshal_mixed_point () {
657 MixedPoint mpt
= new MixedPoint();
661 return mono_test_marshal_mixed_point(mpt
);
664 public static int test_0_marshal_bool_byref () {
666 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 1)
669 if (mono_test_marshal_bool_byref (99, ref b
, 100) != 0)
677 public static int test_0_marshal_bool_as_I1 () {
681 if ((ret
= mono_test_marshal_bool_in_as_I1 (true, false)) != 0)
684 if ((ret
= mono_test_marshal_bool_out_as_I1 (out bTrue
, out bFalse
)) != 0)
693 if ((ret
= mono_test_marshal_bool_ref_as_I1 (ref bTrue
, ref bFalse
)) != 0)
705 public static int test_0_marshal_bool_as_U1 () {
709 if ((ret
= mono_test_marshal_bool_in_as_U1 (true, false)) != 0)
712 if ((ret
= mono_test_marshal_bool_out_as_U1 (out bTrue
, out bFalse
)) != 0)
721 if ((ret
= mono_test_marshal_bool_ref_as_U1 (ref bTrue
, ref bFalse
)) != 0)
733 public static int test_0_return_vtype () {
734 SimpleStruct ss
= mono_test_return_vtype (new IntPtr (5));
736 if (!ss
.a
&& ss
.b
&& !ss
.c
&& ss
.d
== "TEST" && ss
.d2
== "TEST2")
742 public static int test_0_marshal_stringbuilder () {
743 StringBuilder sb
= new StringBuilder(255);
745 mono_test_marshal_stringbuilder (sb
, sb
.Capacity
);
746 String res
= sb
.ToString();
748 if (res
!= "This is my message. Isn't it nice?")
751 // Test StringBuilder with default capacity (16)
752 StringBuilder sb2
= new StringBuilder();
753 mono_test_marshal_stringbuilder_default (sb2
, sb2
.Capacity
);
754 if (sb2
.ToString () != "This is my messa")
760 public static int test_0_marshal_stringbuilder_unicode () {
761 StringBuilder sb
= new StringBuilder(255);
762 mono_test_marshal_stringbuilder_unicode (sb
, sb
.Capacity
);
763 String res
= sb
.ToString();
765 if (res
!= "This is my message. Isn't it nice?")
768 // Test StringBuilder with default capacity (16)
769 StringBuilder sb2
= new StringBuilder();
770 mono_test_marshal_stringbuilder_unicode (sb2
, sb2
.Capacity
);
771 if (sb2
.ToString () != "This is my messa")
777 public static int test_0_marshal_empty_string_array () {
778 return mono_test_marshal_empty_string_array (null);
781 public static int test_0_marshal_string_array () {
782 return mono_test_marshal_string_array (new String
[] { "ABC", "DEF" }
);
785 public static int test_0_marshal_unicode_string_array () {
786 return mono_test_marshal_unicode_string_array (new String
[] { "ABC", "DEF" }
, new String
[] { "ABC", "DEF" }
);
789 public static int test_0_marshal_stringbuilder_array () {
790 StringBuilder sb1
= new StringBuilder ("ABC");
791 StringBuilder sb2
= new StringBuilder ("DEF");
793 int res
= mono_test_marshal_stringbuilder_array (new StringBuilder
[] { sb1, sb2 }
);
796 if (sb1
.ToString () != "DEF")
798 if (sb2
.ToString () != "ABC")
803 public static int test_0_last_error () {
804 mono_test_last_error (5);
805 if (Marshal
.GetLastWin32Error () == 5)
811 public static int test_0_entry_point_not_found () {
814 mono_entry_point_not_found ();
817 catch (EntryPointNotFoundException
) {
823 public static int test_0_library_not_found () {
826 mono_library_not_found ();
829 catch (DllNotFoundException
) {
835 /* Check that the runtime trims .dll from the library name */
836 public static int test_0_trim_dll_from_name () {
838 mono_test_marshal_char_2 ('A');
843 /* Check that the runtime adds lib to to the library name */
844 public static int test_0_add_lib_to_name () {
846 mono_test_marshal_char_3 ('A');
855 public static int test_0_asany () {
856 if (mono_test_asany (5, 1) != 0)
859 if (mono_test_asany ("ABC", 2) != 0)
862 SimpleStruct2 ss2
= new SimpleStruct2 ();
870 if (mono_test_asany (ss2
, 3) != 0)
873 if (mono_test_asany_unicode ("ABC", 4) != 0)
879 mono_test_asany (c
, 0);
882 catch (ArgumentException
) {
886 mono_test_asany (new Object (), 0);
889 catch (ArgumentException
) {
895 /* AsAny marshalling + [In, Out] */
897 public static int test_0_asany_in () {
899 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
900 mono_test_asany_in (str
);
903 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
904 mono_test_asany_in (cls
);
905 if ((cls
.i
!= 1) || (cls
.j
!= 2) || (cls
.k
!= 3))
909 object obj
= new AsAnyStruct(1,2,3, "ABC");
910 mono_test_asany_in (obj
);
911 str
= (AsAnyStruct
)obj
;
912 if ((str
.i
!= 1) || (str
.j
!= 2) || (str
.k
!= 3))
918 public static int test_0_asany_out () {
920 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
921 mono_test_asany_out (str
);
924 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
925 mono_test_asany_out (cls
);
926 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
930 object obj
= new AsAnyStruct(1,2,3, "ABC");
931 mono_test_asany_out (obj
);
932 str
= (AsAnyStruct
)obj
;
933 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
939 public static int test_0_asany_inout () {
941 AsAnyStruct str
= new AsAnyStruct(1,2,3, "ABC");
942 mono_test_asany_inout (str
);
945 AsAnyClass cls
= new AsAnyClass(1,2,3, "ABC");
946 mono_test_asany_inout (cls
);
947 if ((cls
.i
!= 10) || (cls
.j
!= 20) || (cls
.k
!= 30))
951 object obj
= new AsAnyStruct(1,2,3, "ABC");
952 mono_test_asany_inout (obj
);
953 str
= (AsAnyStruct
)obj
;
954 if ((str
.i
!= 10) || (str
.j
!= 20) || (str
.k
!= 30))
960 /* Byref String Array */
962 [DllImport ("libtest", EntryPoint
="mono_test_marshal_byref_string_array")]
963 public static extern int mono_test_marshal_byref_string_array (ref string[] data
);
965 public static int test_0_byref_string_array () {
969 if (mono_test_marshal_byref_string_array (ref arr
) != 0)
972 arr
= new string[] { "Alpha", "Beta", "Gamma" }
;
974 if (mono_test_marshal_byref_string_array (ref arr
) != 1)
977 /* FIXME: Test returned array and out case */
983 * AMD64 small structs-by-value tests.
986 /* TEST 1: 16 byte long INTEGER struct */
988 [StructLayout(LayoutKind
.Sequential
)]
989 public struct Amd64Struct1
{
996 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1")]
997 public static extern Amd64Struct1
mono_test_marshal_amd64_pass_return_struct1 (Amd64Struct1 s
);
999 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1_many_args")]
1000 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
);
1002 public static int test_0_amd64_struct1 () {
1003 Amd64Struct1 s
= new Amd64Struct1 ();
1009 Amd64Struct1 s2
= mono_test_marshal_amd64_pass_return_struct1 (s
);
1011 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000)) ? 0 : 1;
1014 public static int test_0_amd64_struct1_many_args () {
1015 Amd64Struct1 s
= new Amd64Struct1 ();
1021 Amd64Struct1 s2
= mono_test_marshal_amd64_pass_return_struct1_many_args (s
, 1, 2, 3, 4, 5, 6, 7, 8);
1023 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)) ? 0 : 1;
1026 /* TEST 2: 8 byte long INTEGER struct */
1028 [StructLayout(LayoutKind
.Sequential
)]
1029 public struct Amd64Struct2
{
1034 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct2")]
1035 public static extern Amd64Struct2
mono_test_marshal_amd64_pass_return_struct2 (Amd64Struct2 s
);
1037 public static int test_0_amd64_struct2 () {
1038 Amd64Struct2 s
= new Amd64Struct2 ();
1042 Amd64Struct2 s2
= mono_test_marshal_amd64_pass_return_struct2 (s
);
1044 return ((s2
.i
== 6) && (s2
.j
== -4)) ? 0 : 1;
1047 /* TEST 3: 4 byte long INTEGER struct */
1049 [StructLayout(LayoutKind
.Sequential
)]
1050 public struct Amd64Struct3
{
1054 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct3")]
1055 public static extern Amd64Struct3
mono_test_marshal_amd64_pass_return_struct3 (Amd64Struct3 s
);
1057 public static int test_0_amd64_struct3 () {
1058 Amd64Struct3 s
= new Amd64Struct3 ();
1061 Amd64Struct3 s2
= mono_test_marshal_amd64_pass_return_struct3 (s
);
1063 return (s2
.i
== -4) ? 0 : 1;
1066 /* Test 4: 16 byte long FLOAT struct */
1068 [StructLayout(LayoutKind
.Sequential
)]
1069 public struct Amd64Struct4
{
1070 public double d1
, d2
;
1073 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct4")]
1074 public static extern Amd64Struct4
mono_test_marshal_amd64_pass_return_struct4 (Amd64Struct4 s
);
1076 public static int test_0_amd64_struct4 () {
1077 Amd64Struct4 s
= new Amd64Struct4 ();
1081 Amd64Struct4 s2
= mono_test_marshal_amd64_pass_return_struct4 (s
);
1083 return (s2
.d1
== 6.0 && s2
.d2
== -4.0) ? 0 : 1;
1090 /* Test 5: Float HFA */
1092 [StructLayout(LayoutKind
.Sequential
)]
1093 public struct TestStruct5
{
1094 public float d1
, d2
;
1097 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct5")]
1098 public static extern TestStruct5
mono_test_marshal_ia64_pass_return_struct5 (double d1
, double d2
, TestStruct5 s
, int i
, double f3
, double f4
);
1100 public static int test_0_ia64_struct5 () {
1101 TestStruct5 s
= new TestStruct5 ();
1105 TestStruct5 s2
= mono_test_marshal_ia64_pass_return_struct5 (1.0, 2.0, s
, 5, 3.0, 4.0);
1107 return (s2
.d1
== 13.0 && s2
.d2
== 7.0) ? 0 : 1;
1110 /* Test 6: Double HFA */
1112 [StructLayout(LayoutKind
.Sequential
)]
1113 public struct TestStruct6
{
1114 public double d1
, d2
;
1117 [DllImport ("libtest", EntryPoint
="mono_test_marshal_ia64_pass_return_struct6")]
1118 public static extern TestStruct6
mono_test_marshal_ia64_pass_return_struct6 (double d1
, double d2
, TestStruct6 s
, int i
, double f3
, double f4
);
1120 public static int test_0_ia64_struct6 () {
1121 TestStruct6 s
= new TestStruct6 ();
1125 TestStruct6 s2
= mono_test_marshal_ia64_pass_return_struct6 (1.0, 2.0, s
, 3, 4.0, 5.0);
1127 return (s2
.d1
== 12.0 && s2
.d2
== 3.0) ? 0 : 1;
1130 /* Blittable class */
1131 [DllImport("libtest")]
1132 private static extern BlittableClass
TestBlittableClass (BlittableClass vl
);
1134 public static int test_0_marshal_blittable_class () {
1135 BlittableClass v1
= new BlittableClass ();
1137 /* Since it is blittable, it looks like it is passed as in/out */
1138 BlittableClass v2
= TestBlittableClass (v1
);
1140 if (v1
.a
!= 2 || v1
.b
!= 3)
1143 if (v2
.a
!= 2 || v2
.b
!= 3)
1147 BlittableClass v3
= TestBlittableClass (null);
1149 if (v3
.a
!= 42 || v3
.b
!= 43)
1156 * Generic structures
1159 [StructLayout(LayoutKind
.Sequential
)]
1160 public struct Amd64Struct1Gen
<T
> {
1167 [DllImport ("libtest", EntryPoint
="mono_test_marshal_amd64_pass_return_struct1")]
1168 public static extern Amd64Struct1Gen
<int> mono_test_marshal_amd64_pass_return_struct1_gen (Amd64Struct1Gen
<int> s
);
1170 public static int test_0_amd64_struct1_gen () {
1171 Amd64Struct1Gen
<int> s
= new Amd64Struct1Gen
<int> ();
1177 Amd64Struct1Gen
<int> s2
= mono_test_marshal_amd64_pass_return_struct1_gen (s
);
1179 return ((s2
.i
== 6) && (s2
.j
== -4) && (s2
.k
== 0x1000000) && (s2
.l
== 0x10000000)) ? 0 : 1;
1186 public static int test_0_marshal_byval_class () {
1187 SimpleObj obj0
= new SimpleObj ();
1191 if (class_marshal_test0 (obj0
) != 0)
1197 public static int test_0_marshal_byval_class_null () {
1198 if (class_marshal_test4 (null) != 0)
1204 public static int test_0_marshal_out_class () {
1207 class_marshal_test1 (out obj1
);
1209 if (obj1
.str
!= "ABC")
1218 public static int test_0_marshal_string () {
1219 return string_marshal_test0 ("TEST0");
1222 public static int test_0_marshal_out_string () {
1225 string_marshal_test1 (out res
);
1233 public static int test_0_marshal_byref_string () {
1234 string res
= "TEST1";
1236 return string_marshal_test2 (ref res
);
1239 public static int test_0_marshal_null_string () {
1240 return string_marshal_test3 (null);
1243 [DllImport ("libtest", EntryPoint
="mono_test_stdcall_name_mangling", CallingConvention
=CallingConvention
.StdCall
)]
1244 public static extern int mono_test_stdcall_name_mangling (int a
, int b
, int c
);
1246 public static int test_0_stdcall_name_mangling () {
1247 return mono_test_stdcall_name_mangling (0, 1, 2) == 3 ? 0 : 1;
1252 [DllImport ("libtest", EntryPoint
="mono_test_marshal_pass_return_float")]
1253 public static extern float mono_test_marshal_pass_return_float (float f
);
1255 public static int test_0_pass_return_float () {
1256 float f
= mono_test_marshal_pass_return_float (1.5f
);
1258 return (f
== 2.5f
) ? 0 : 1;
1262 * Pointers to structures can not be passed
1265 public struct CharInfo
{
1266 public char Character
;
1267 public short Attributes
;
1270 [DllImport ("libtest", EntryPoint
="mono_test_marshal_struct")]
1271 public static unsafe extern int mono_test_marshal_ptr_to_struct (CharInfo
*ptr
);
1273 public static unsafe int test_0_marshal_ptr_to_struct () {
1274 CharInfo
[] buffer
= new CharInfo
[1];
1275 fixed (CharInfo
*ptr
= &buffer
[0]) {
1277 mono_test_marshal_ptr_to_struct (ptr
);
1280 catch (MarshalDirectiveException
) {
1288 * LPWStr marshalling
1291 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal")]
1292 [return: MarshalAs(UnmanagedType
.LPWStr
)]
1293 private static extern string mono_test_marshal_lpwstr_marshal(
1294 [MarshalAs(UnmanagedType
.LPWStr
)] string s
,
1297 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal", CharSet
=CharSet
.Unicode
)]
1298 private static extern string mono_test_marshal_lpwstr_marshal2(
1302 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal_out")]
1303 private static extern void mono_test_marshal_lpwstr_out_marshal(
1304 [MarshalAs(UnmanagedType
.LPWStr
)] out string s
);
1306 [DllImport("libtest", EntryPoint
="test_lpwstr_marshal_out", CharSet
=CharSet
.Unicode
)]
1307 private static extern void mono_test_marshal_lpwstr_out_marshal2(
1310 public static int test_0_pass_return_lwpstr () {
1313 mono_test_marshal_lpwstr_out_marshal (out s
);
1319 mono_test_marshal_lpwstr_out_marshal2 (out s
);
1327 public static int test_0_out_lwpstr () {
1330 string res
= mono_test_marshal_lpwstr_marshal (s
, s
.Length
);
1335 string res2
= mono_test_marshal_lpwstr_marshal2 (s
, s
.Length
);
1344 * Byref bool marshalling
1347 [DllImport("libtest")]
1348 extern static int marshal_test_ref_bool
1351 [MarshalAs(UnmanagedType
.I1
)] ref bool b1
,
1352 [MarshalAs(UnmanagedType
.VariantBool
)] ref bool b2
,
1356 public static int test_0_pass_byref_bool () {
1357 for (int i
= 0; i
< 8; i
++)
1359 bool b1
= (i
& 4) != 0;
1360 bool b2
= (i
& 2) != 0;
1361 bool b3
= (i
& 1) != 0;
1362 bool orig_b1
= b1
, orig_b2
= b2
, orig_b3
= b3
;
1363 if (marshal_test_ref_bool(i
, ref b1
, ref b2
, ref b3
) != 0)
1377 * Bool struct field marshalling
1383 [MarshalAs(UnmanagedType
.I1
)] public bool b1
;
1384 [MarshalAs(UnmanagedType
.VariantBool
)] public bool b2
;
1388 [DllImport("libtest")]
1389 extern static int marshal_test_bool_struct(ref BoolStruct s
);
1391 public static int test_0_pass_bool_in_struct () {
1392 for (int i
= 0; i
< 8; i
++)
1394 BoolStruct s
= new BoolStruct();
1396 s
.b1
= (i
& 4) != 0;
1397 s
.b2
= (i
& 2) != 0;
1398 s
.b3
= (i
& 1) != 0;
1399 BoolStruct orig
= s
;
1400 if (marshal_test_bool_struct(ref s
) != 0)
1402 if (s
.b1
!= !orig
.b1
)
1404 if (s
.b2
!= !orig
.b2
)
1406 if (s
.b3
!= !orig
.b3
)
1414 * Alignment of structs containing longs
1417 struct LongStruct2
{
1423 public LongStruct2 l
;
1426 [DllImport("libtest")]
1427 extern static int mono_test_marshal_long_struct (ref LongStruct s
);
1429 public static int test_47_pass_long_struct () {
1430 LongStruct s
= new LongStruct ();
1432 s
.l
= new LongStruct2 ();
1435 return mono_test_marshal_long_struct (ref s
);
1439 * Invoking pinvoke methods through delegates
1442 delegate int MyDelegate (string name
);
1444 [DllImport ("libtest", EntryPoint
="mono_test_puts_static")]
1445 public static extern int puts_static (string name
);
1447 public static int test_0_invoke_pinvoke_through_delegate () {
1448 puts_static ("A simple Test for PInvoke 1");
1450 MyDelegate d
= new MyDelegate (puts_static
);
1451 d ("A simple Test for PInvoke 2");
1453 object [] args
= {"A simple Test for PInvoke 3"}
;
1454 d
.DynamicInvoke (args
);
1460 * Missing virtual pinvoke methods
1465 public virtual object MyClone ()
1471 public class T2
: T
{
1472 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
1473 public override extern object MyClone ();
1476 public static int test_0_missing_virtual_pinvoke_method () {
1481 } catch (Exception ex
) {