2 // marshal9.cs: tests for custom marshalling
6 using System
.Runtime
.InteropServices
;
8 public class Marshal1
: ICustomMarshaler
12 public static int cleanup_managed_count
= 0;
14 public static int cleanup_native_count
= 0;
16 public static int native_to_managed_count
= 0;
18 public static object native_to_managed_result
= null;
20 public Marshal1 (int param
) {
24 public static ICustomMarshaler
GetInstance (string s
) {
25 int param
= Int32
.Parse (s
);
26 return new Marshal1 (param
);
29 public void CleanUpManagedData (object managedObj
)
31 //Console.WriteLine ("CleanUpManagedData called");
32 cleanup_managed_count
++;
35 public void CleanUpNativeData (IntPtr pNativeData
)
37 //Console.WriteLine("CleanUpNativeData:" + pNativeData);
38 /* Might be allocated in libtest.c using g_new0 so dont free it */
39 int alloc_type
= Marshal
.ReadInt32 (pNativeData
);
41 Marshal
.FreeHGlobal (pNativeData
);
42 cleanup_native_count
++;
45 // I really do not understand the purpose of this method
46 // or when it would be called. In fact, Rotor never seems
48 public int GetNativeDataSize ()
50 //Console.WriteLine("GetNativeDataSize() called");
54 public IntPtr
MarshalManagedToNative (object managedObj
)
59 number
= Convert
.ToInt32 (managedObj
);
60 ptr
= Marshal
.AllocHGlobal (8);
61 Marshal
.WriteInt32 (ptr
, 1); /* Allocated by AllocHGlobal */
62 Marshal
.WriteInt32(new IntPtr (ptr
.ToInt64 () + 4), number
);
64 //Console.WriteLine ("ToNative: " + ptr);
68 public object MarshalNativeToManaged (IntPtr pNativeData
)
70 //Console.WriteLine ("ToManaged: " + pNativeData);
71 native_to_managed_count
++;
72 native_to_managed_result
= param
+ Marshal
.ReadInt32 (new IntPtr (pNativeData
.ToInt64 () + 4));
73 return native_to_managed_result
;
79 public static int Main (string[] args
) {
80 return TestDriver
.RunTests (typeof (Tests
));
83 [DllImport ("libtest")]
84 [return : MarshalAs(UnmanagedType
.CustomMarshaler
,MarshalTypeRef
= typeof
85 (Marshal1
), MarshalCookie
= "5")]
86 private static extern object mono_test_marshal_pass_return_custom (int i
,
87 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5")] object number
, int j
);
89 public static int test_0_pass_return () {
91 Marshal1
.cleanup_managed_count
= 0;
92 Marshal1
.cleanup_native_count
= 0;
94 int res
= (int)mono_test_marshal_pass_return_custom (5, 10, 5);
96 if (Marshal1
.cleanup_managed_count
!= 0)
98 if (Marshal1
.cleanup_native_count
!= 2)
101 return res
== 15 ? 0 : 3;
104 [DllImport ("libtest")]
105 private static extern int mono_test_marshal_pass_out_custom (int i
,
106 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5"), Out
] out object number
, int j
);
108 public static int test_0_pass_out () {
110 Marshal1
.cleanup_managed_count
= 0;
111 Marshal1
.cleanup_native_count
= 0;
114 int res
= mono_test_marshal_pass_out_custom (5, out o
, 5);
120 if (Marshal1
.cleanup_managed_count
!= 0)
122 if (Marshal1
.cleanup_native_count
!= 1)
128 [DllImport ("libtest")]
129 private static extern int mono_test_marshal_pass_inout_custom (int i
,
130 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5"), In
, Out
] object number
, int j
);
132 public static int test_0_pass_inout () {
133 Marshal1
.cleanup_managed_count
= 0;
134 Marshal1
.cleanup_native_count
= 0;
135 Marshal1
.native_to_managed_result
= null;
137 int res
= mono_test_marshal_pass_inout_custom (5, 5, 5);
139 // The changes made by the [Out] custom marshaller are not visible to the caller
140 if ((int)Marshal1
.native_to_managed_result
!= 20)
143 if (Marshal1
.cleanup_managed_count
!= 0)
145 if (Marshal1
.cleanup_native_count
!= 1)
151 [DllImport ("libtest")]
152 private static extern int mono_test_marshal_pass_out_byval_custom (int i
,
153 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5"), Out
] object number
, int j
);
155 public static int test_0_pass_out_byval () {
156 Marshal1
.cleanup_managed_count
= 0;
157 Marshal1
.cleanup_native_count
= 0;
158 Marshal1
.native_to_managed_result
= null;
160 // MS.NET passes NULL and does no marshalling in this case
161 int res
= mono_test_marshal_pass_out_byval_custom (5, 5, 5);
166 if (Marshal1
.native_to_managed_result
!= null)
169 if (Marshal1
.cleanup_managed_count
!= 0)
171 if (Marshal1
.cleanup_native_count
!= 0)
177 [DllImport ("libtest")]
178 private static extern int mono_test_marshal_pass_byref_custom (int i
,
179 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5")] ref object number
, int j
);
181 public static int test_0_pass_ref () {
183 Marshal1
.cleanup_managed_count
= 0;
184 Marshal1
.cleanup_native_count
= 0;
187 int res
= mono_test_marshal_pass_byref_custom (5, ref o
, 5);
193 if (Marshal1
.cleanup_managed_count
!= 0)
195 if (Marshal1
.cleanup_native_count
!= 1)
201 [DllImport ("libtest")]
202 [return : MarshalAs(UnmanagedType
.CustomMarshaler
,MarshalTypeRef
= typeof
203 (Marshal1
), MarshalCookie
= "5")]
204 private static extern object mono_test_marshal_pass_return_custom_null (int i
,
205 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5")] object number
, int j
);
207 public static int test_0_pass_return_null () {
209 Marshal1
.cleanup_managed_count
= 0;
210 Marshal1
.cleanup_native_count
= 0;
212 object o
= mono_test_marshal_pass_return_custom_null (5, null, 5);
214 if (Marshal1
.cleanup_managed_count
!= 0)
216 if (Marshal1
.cleanup_native_count
!= 0)
219 return (o
== null) ? 0 : 1;
222 [return : MarshalAs(UnmanagedType
.CustomMarshaler
,MarshalTypeRef
= typeof
223 (Marshal1
), MarshalCookie
= "5")] public delegate object pass_return_int_delegate ([MarshalAs (UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal1
), MarshalCookie
= "5")] object o
);
225 [DllImport ("libtest")]
226 private static extern int mono_test_marshal_pass_return_custom_in_delegate (pass_return_int_delegate del
);
228 [DllImport ("libtest")]
229 private static extern int mono_test_marshal_pass_return_custom_null_in_delegate (pass_return_int_delegate del
);
231 private static object pass_return_int (object i
) {
235 private static object pass_return_null (object i
) {
236 return (i
== null) ? null : new Object ();
239 public static int test_0_pass_return_delegate () {
241 Marshal1
.cleanup_managed_count
= 0;
242 Marshal1
.cleanup_native_count
= 0;
244 int res
= mono_test_marshal_pass_return_custom_in_delegate (new pass_return_int_delegate (pass_return_int
));
246 if (Marshal1
.cleanup_managed_count
!= 2)
248 if (Marshal1
.cleanup_native_count
!= 0)
251 return res
== 15 ? 0 : 3;
254 public static int test_0_pass_return_null_delegate () {
256 Marshal1
.cleanup_managed_count
= 0;
257 Marshal1
.cleanup_native_count
= 0;
259 int res
= mono_test_marshal_pass_return_custom_null_in_delegate (new pass_return_int_delegate (pass_return_null
));
261 if (Marshal1
.cleanup_managed_count
!= 0)
263 if (Marshal1
.cleanup_native_count
!= 0)
266 return res
== 15 ? 0 : 3;
270 * Test custom marshaller class not implementing ICustomMarshaler
273 public class Marshal2
{
276 [DllImport ("libtest")]
277 private static extern IntPtr
mono_test_marshal_pass_return_custom2 (int i
,
278 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal3
), MarshalCookie
= "5")] object number
, int j
);
280 public static int test_0_not_icustommarshaller () {
282 mono_test_marshal_pass_return_custom2 (5, 10, 5);
284 catch (ApplicationException
) {
292 * Test custom marshaller class missing GetInstance method
295 public class Marshal3
: ICustomMarshaler
{
296 public void CleanUpManagedData (object managedObj
)
300 public void CleanUpNativeData (IntPtr pNativeData
)
304 public int GetNativeDataSize ()
309 public IntPtr
MarshalManagedToNative (object managedObj
)
314 public object MarshalNativeToManaged (IntPtr pNativeData
)
320 public class Marshal4
: Marshal3
{
321 public static object GetInstance (string s
) {
326 public class Marshal5
: Marshal3
{
327 public static ICustomMarshaler
GetInstance (object s
) {
332 [DllImport ("libtest", EntryPoint
= "mono_test_marshal_pass_return_custom2")]
333 private static extern IntPtr
mono_test_marshal_pass_return_custom3 (int i
,
334 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal3
), MarshalCookie
= "5")] object number
, int j
);
336 [DllImport ("libtest", EntryPoint
= "mono_test_marshal_pass_return_custom2")]
337 private static extern IntPtr
mono_test_marshal_pass_return_custom4 (int i
,
338 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal4
), MarshalCookie
= "5")] object number
, int j
);
340 [DllImport ("libtest", EntryPoint
= "mono_test_marshal_pass_return_custom2")]
341 private static extern IntPtr
mono_test_marshal_pass_return_custom5 (int i
,
342 [MarshalAs( UnmanagedType
.CustomMarshaler
, MarshalTypeRef
= typeof (Marshal5
), MarshalCookie
= "5")] object number
, int j
);
344 public static int test_0_missing_getinstance1 () {
346 mono_test_marshal_pass_return_custom3 (5, 10, 5);
348 catch (ApplicationException
) {
354 public static int test_0_missing_getinstance2 () {
356 mono_test_marshal_pass_return_custom4 (5, 10, 5);
358 catch (ApplicationException
) {
364 public static int test_0_missing_getinstance3 () {
366 mono_test_marshal_pass_return_custom5 (5, 10, 5);
368 catch (ApplicationException
) {