4 // Tests for native->managed marshalling
8 using System.Runtime.InteropServices;
12 [StructLayout (LayoutKind.Sequential)]
13 public struct SimpleStruct {
20 [StructLayout (LayoutKind.Sequential)]
21 public class SimpleClass {
28 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
35 res.d = ss.d + "-RES";
40 public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
42 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
53 public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
63 public static SimpleClass delegate_test_class (SimpleClass ss)
68 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
76 public static int delegate_test_class_byref (ref SimpleClass ss)
81 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
93 public static int delegate_test_class_out (out SimpleClass ss)
95 ss = new SimpleClass ();
104 public static int delegate_test_primitive_byref (ref int i)
113 public static int delegate_test_string_marshalling (string s)
115 return s == "ABC" ? 0 : 1;
118 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
119 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
121 public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
123 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
124 public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
126 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
127 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
129 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
130 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
132 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
133 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
135 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
136 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
138 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
139 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
141 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
142 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
144 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
145 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
147 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
148 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
150 [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
151 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
153 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
155 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
157 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
159 public delegate int SimpleDelegate5 (ref SimpleClass ss);
161 public delegate int SimpleDelegate7 (out SimpleClass ss);
163 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
165 public delegate int return_int_delegate (int i);
167 public delegate int SimpleDelegate9 (return_int_delegate del);
169 public delegate int PrimitiveByrefDelegate (ref int i);
171 public static int Main () {
172 return TestDriver.RunTests (typeof (Tests));
175 /* Test structures as arguments and return values of delegates */
176 static int test_0_marshal_struct_delegate () {
177 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
179 return mono_test_marshal_delegate2 (d);
182 /* Test structures as byref arguments of delegates */
183 static int test_0_marshal_byref_struct_delegate () {
184 SimpleStruct ss = new SimpleStruct ();
185 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
190 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
193 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
199 /* Test structures as out arguments of delegates */
200 static int test_0_marshal_out_struct_delegate () {
201 SimpleStruct ss = new SimpleStruct ();
202 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
204 return mono_test_marshal_out_struct (1, out ss, 2, d);
207 /* Test classes as arguments and return values of delegates */
208 static int test_0_marshal_class_delegate () {
209 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
211 return mono_test_marshal_delegate4 (d);
214 /* Test classes as byref arguments of delegates */
215 static int test_0_marshal_byref_class_delegate () {
216 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
218 return mono_test_marshal_delegate5 (d);
221 /* Test classes as out arguments of delegates */
222 static int test_0_marshal_out_class_delegate () {
223 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
225 return mono_test_marshal_delegate7 (d);
228 /* Test string marshalling with delegates */
229 static int test_0_marshal_string_delegate () {
230 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
232 return mono_test_marshal_delegate8 (d, "ABC");
235 /* Test that the delegate wrapper correctly catches null byref arguments */
236 static int test_0_marshal_byref_class_delegate_null () {
237 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
240 mono_test_marshal_delegate6 (d);
243 catch (ArgumentNullException ex) {
248 static int return_self (int i) {
252 static int call_int_delegate (return_int_delegate d) {
256 static int test_55_marshal_delegate_delegate () {
257 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
259 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
262 static int test_0_marshal_delegate_delegate_unmanaged_ftn () {
263 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
266 mono_test_marshal_delegate10 (d);
269 catch (ArgumentException) {
276 static int test_0_marshal_primitive_byref_delegate () {
277 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
279 return mono_test_marshal_primitive_byref_delegate (d);