2 // marshal.cs: tests for the System.Runtime.InteropServices.Marshal class
6 using System.Reflection;
7 using System.Runtime.InteropServices;
11 public static int Main (string[] args) {
12 return TestDriver.RunTests (typeof (Tests), args);
15 public delegate int SimpleDelegate (int a);
17 public static int delegate_test (int a)
22 [DllImport ("libtest", EntryPoint="mono_test_marshal_delegate")]
23 public static extern int mono_test_marshal_delegate (IntPtr ptr);
25 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate")]
26 public static extern IntPtr mono_test_marshal_return_delegate (SimpleDelegate d);
28 [DllImport ("libtest", EntryPoint="mono_test_marshal_return_delegate_2")]
29 public static extern IntPtr mono_test_marshal_return_delegate_2 ();
31 static int test_0_get_function_pointer_for_delegate () {
32 IntPtr fnPtr = Marshal.GetFunctionPointerForDelegate (new SimpleDelegate (delegate_test));
34 if (mono_test_marshal_delegate (fnPtr) != 3)
40 static int test_0_get_delegate_for_function_pointer () {
41 IntPtr ptr = mono_test_marshal_return_delegate (new SimpleDelegate (delegate_test));
43 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
45 return d (5) == 6 ? 0 : 1;
48 /* Obtain a delegate from a native function pointer */
49 static int test_0_get_delegate_for_ftnptr_native () {
50 IntPtr ptr = mono_test_marshal_return_delegate_2 ();
52 SimpleDelegate d = (SimpleDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (SimpleDelegate));
54 return d (5) == 6 ? 0 : 1;