4 // Tests for native->managed marshalling
8 using System.Runtime.InteropServices;
12 [StructLayout (LayoutKind.Sequential)]
13 public struct SimpleStruct {
18 [MarshalAs(UnmanagedType.LPWStr)]
22 [StructLayout (LayoutKind.Sequential)]
23 public class SimpleClass {
30 public static SimpleStruct delegate_test_struct (SimpleStruct ss)
37 res.d = ss.d + "-RES";
38 res.d2 = ss.d2 + "-RES";
43 public static int delegate_test_struct_byref (int a, ref SimpleStruct ss, int b)
45 if (a == 1 && b == 2 && ss.a && !ss.b && ss.c && ss.d == "TEST2") {
56 public static int delegate_test_struct_out (int a, out SimpleStruct ss, int b)
67 public static SimpleClass delegate_test_class (SimpleClass ss)
72 if (! (!ss.a && ss.b && !ss.c && ss.d == "TEST"))
80 public static int delegate_test_class_byref (ref SimpleClass ss)
85 if (!ss.a && ss.b && !ss.c && ss.d == "TEST") {
97 public static int delegate_test_class_out (out SimpleClass ss)
99 ss = new SimpleClass ();
108 public static int delegate_test_primitive_byref (ref int i)
117 public static int delegate_test_string_marshalling (string s)
119 return s == "ABC" ? 0 : 1;
122 [DllImport ("libtest", EntryPoint="mono_test_ref_vtype")]
123 public static extern int mono_test_ref_vtype (int a, ref SimpleStruct ss, int b, TestDelegate d);
125 public delegate int OutStructDelegate (int a, out SimpleStruct ss, int b);
127 [DllImport ("libtest", EntryPoint="mono_test_marshal_out_struct")]
128 public static extern int mono_test_marshal_out_struct (int a, out SimpleStruct ss, int b, OutStructDelegate d);
130 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate2")]
131 public static extern int mono_test_marshal_delegate2 (SimpleDelegate2 d);
133 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate4")]
134 public static extern int mono_test_marshal_delegate4 (SimpleDelegate4 d);
136 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate5")]
137 public static extern int mono_test_marshal_delegate5 (SimpleDelegate5 d);
139 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate6")]
140 public static extern int mono_test_marshal_delegate6 (SimpleDelegate5 d);
142 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate7")]
143 public static extern int mono_test_marshal_delegate7 (SimpleDelegate7 d);
145 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate8", CharSet=CharSet.Unicode)]
146 public static extern int mono_test_marshal_delegate8 (SimpleDelegate8 d, string s);
148 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate9")]
149 public static extern int mono_test_marshal_delegate9 (SimpleDelegate9 d, return_int_delegate d2);
151 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate10")]
152 public static extern int mono_test_marshal_delegate10 (SimpleDelegate9 d);
154 [DllImport ("libtest", EntryPoint="mono_test_marshal_primitive_byref_delegate")]
155 public static extern int mono_test_marshal_primitive_byref_delegate (PrimitiveByrefDelegate d);
157 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
158 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
160 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
162 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
164 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
166 public delegate int SimpleDelegate5 (ref SimpleClass ss);
168 public delegate int SimpleDelegate7 (out SimpleClass ss);
170 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
172 public delegate int return_int_delegate (int i);
174 public delegate int SimpleDelegate9 (return_int_delegate del);
176 public delegate int PrimitiveByrefDelegate (ref int i);
178 public delegate return_int_delegate ReturnDelegateDelegate ();
180 public static int Main () {
181 return TestDriver.RunTests (typeof (Tests));
184 /* Test structures as arguments and return values of delegates */
185 static int test_0_marshal_struct_delegate () {
186 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
188 return mono_test_marshal_delegate2 (d);
191 /* Test structures as byref arguments of delegates */
192 static int test_0_marshal_byref_struct_delegate () {
193 SimpleStruct ss = new SimpleStruct ();
194 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
199 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
202 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
208 /* Test structures as out arguments of delegates */
209 static int test_0_marshal_out_struct_delegate () {
210 SimpleStruct ss = new SimpleStruct ();
211 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
213 return mono_test_marshal_out_struct (1, out ss, 2, d);
216 /* Test classes as arguments and return values of delegates */
217 static int test_0_marshal_class_delegate () {
218 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
220 return mono_test_marshal_delegate4 (d);
223 /* Test classes as byref arguments of delegates */
224 static int test_0_marshal_byref_class_delegate () {
225 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
227 return mono_test_marshal_delegate5 (d);
230 /* Test classes as out arguments of delegates */
231 static int test_0_marshal_out_class_delegate () {
232 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
234 return mono_test_marshal_delegate7 (d);
237 /* Test string marshalling with delegates */
238 static int test_0_marshal_string_delegate () {
239 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
241 return mono_test_marshal_delegate8 (d, "ABC");
244 /* Test that the delegate wrapper correctly catches null byref arguments */
245 static int test_0_marshal_byref_class_delegate_null () {
246 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
249 mono_test_marshal_delegate6 (d);
252 catch (ArgumentNullException ex) {
257 static int return_self (int i) {
261 static int call_int_delegate (return_int_delegate d) {
265 static int test_55_marshal_delegate_delegate () {
266 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
268 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
271 static int test_0_marshal_delegate_delegate_unmanaged_ftn () {
272 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
275 mono_test_marshal_delegate10 (d);
278 catch (ArgumentException) {
285 static int test_0_marshal_primitive_byref_delegate () {
286 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
288 return mono_test_marshal_primitive_byref_delegate (d);
291 public static return_int_delegate return_delegate () {
292 return new return_int_delegate (return_self);
295 static int test_55_marshal_return_delegate_delegate () {
296 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));
299 /* Passing and returning strings */
301 public delegate String ReturnStringDelegate (String s);
303 [DllImport ("libtest", EntryPoint="mono_test_return_string")]
304 public static extern String mono_test_marshal_return_string_delegate (ReturnStringDelegate d);
306 public static String managed_return_string (String s) {
313 static int test_0_marshal_return_string_delegate () {
314 ReturnStringDelegate d = new ReturnStringDelegate (managed_return_string);
315 String s = mono_test_marshal_return_string_delegate (d);
317 return (s == "12345") ? 0 : 1;
320 /* Passing and returning enums */
322 public enum FooEnum {
328 public delegate FooEnum ReturnEnumDelegate (FooEnum e);
330 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_enum_delegate")]
331 public static extern int mono_test_marshal_return_enum_delegate (ReturnEnumDelegate d);
333 public static FooEnum managed_return_enum (FooEnum e) {
334 return (FooEnum)((int)e + 1);
337 static int test_0_marshal_return_enum_delegate () {
338 ReturnEnumDelegate d = new ReturnEnumDelegate (managed_return_enum);
339 FooEnum e = (FooEnum)mono_test_marshal_return_enum_delegate (d);
341 return e == FooEnum.Foo3 ? 0 : 1;
344 /* Passing and returning blittable structs */
346 [StructLayout (LayoutKind.Sequential)]
347 public struct BlittableStruct {
352 public static BlittableStruct delegate_test_blittable_struct (BlittableStruct ss)
364 public delegate BlittableStruct SimpleDelegate10 (BlittableStruct ss);
366 [DllImport ("libtest", EntryPoint="mono_test_marshal_blittable_struct_delegate")]
367 public static extern int mono_test_marshal_blittable_struct_delegate (SimpleDelegate10 d);
369 static int test_0_marshal_blittable_struct_delegate () {
370 return mono_test_marshal_blittable_struct_delegate (new SimpleDelegate10 (delegate_test_blittable_struct));