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 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_delegate")]
154 public static extern int mono_test_marshal_return_delegate_delegate (ReturnDelegateDelegate d);
156 public delegate int TestDelegate (int a, ref SimpleStruct ss, int b);
158 public delegate SimpleStruct SimpleDelegate2 (SimpleStruct ss);
160 public delegate SimpleClass SimpleDelegate4 (SimpleClass ss);
162 public delegate int SimpleDelegate5 (ref SimpleClass ss);
164 public delegate int SimpleDelegate7 (out SimpleClass ss);
166 public delegate int SimpleDelegate8 ([MarshalAs (UnmanagedType.LPWStr)] string s1);
168 public delegate int return_int_delegate (int i);
170 public delegate int SimpleDelegate9 (return_int_delegate del);
172 public delegate int PrimitiveByrefDelegate (ref int i);
174 public delegate return_int_delegate ReturnDelegateDelegate ();
176 public static int Main () {
177 return TestDriver.RunTests (typeof (Tests));
180 /* Test structures as arguments and return values of delegates */
181 static int test_0_marshal_struct_delegate () {
182 SimpleDelegate2 d = new SimpleDelegate2 (delegate_test_struct);
184 return mono_test_marshal_delegate2 (d);
187 /* Test structures as byref arguments of delegates */
188 static int test_0_marshal_byref_struct_delegate () {
189 SimpleStruct ss = new SimpleStruct ();
190 TestDelegate d = new TestDelegate (delegate_test_struct_byref);
195 if (mono_test_ref_vtype (1, ref ss, 2, d) != 0)
198 if (! (ss.a && ss.b && ss.c && ss.d == "TEST3"))
204 /* Test structures as out arguments of delegates */
205 static int test_0_marshal_out_struct_delegate () {
206 SimpleStruct ss = new SimpleStruct ();
207 OutStructDelegate d = new OutStructDelegate (delegate_test_struct_out);
209 return mono_test_marshal_out_struct (1, out ss, 2, d);
212 /* Test classes as arguments and return values of delegates */
213 static int test_0_marshal_class_delegate () {
214 SimpleDelegate4 d = new SimpleDelegate4 (delegate_test_class);
216 return mono_test_marshal_delegate4 (d);
219 /* Test classes as byref arguments of delegates */
220 static int test_0_marshal_byref_class_delegate () {
221 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
223 return mono_test_marshal_delegate5 (d);
226 /* Test classes as out arguments of delegates */
227 static int test_0_marshal_out_class_delegate () {
228 SimpleDelegate7 d = new SimpleDelegate7 (delegate_test_class_out);
230 return mono_test_marshal_delegate7 (d);
233 /* Test string marshalling with delegates */
234 static int test_0_marshal_string_delegate () {
235 SimpleDelegate8 d = new SimpleDelegate8 (delegate_test_string_marshalling);
237 return mono_test_marshal_delegate8 (d, "ABC");
240 /* Test that the delegate wrapper correctly catches null byref arguments */
241 static int test_0_marshal_byref_class_delegate_null () {
242 SimpleDelegate5 d = new SimpleDelegate5 (delegate_test_class_byref);
245 mono_test_marshal_delegate6 (d);
248 catch (ArgumentNullException ex) {
253 static int return_self (int i) {
257 static int call_int_delegate (return_int_delegate d) {
261 static int test_55_marshal_delegate_delegate () {
262 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
264 return mono_test_marshal_delegate9 (d, new return_int_delegate (return_self));
267 static int test_0_marshal_delegate_delegate_unmanaged_ftn () {
268 SimpleDelegate9 d = new SimpleDelegate9 (call_int_delegate);
271 mono_test_marshal_delegate10 (d);
274 catch (ArgumentException) {
281 static int test_0_marshal_primitive_byref_delegate () {
282 PrimitiveByrefDelegate d = new PrimitiveByrefDelegate (delegate_test_primitive_byref);
284 return mono_test_marshal_primitive_byref_delegate (d);
287 public static return_int_delegate return_delegate () {
288 return new return_int_delegate (return_self);
291 static int test_55_marshal_return_delegate_delegate () {
292 return mono_test_marshal_return_delegate_delegate (new ReturnDelegateDelegate (return_delegate));