Merge pull request #5390 from kumpera/fix_58637
[mono.git] / mono / tests / pinvoke-2.2.cs
1 //
2 // pinvoke-2.cs:
3 //
4 //  Tests for net 2.0 pinvoke features
5 //
6
7 using System;
8 using System.Runtime.InteropServices;
9
10 public class Tests {
11
12         public static int Main () {
13                 return TestDriver.RunTests (typeof (Tests));
14         }
15
16         [UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl)]
17         public delegate int CdeclDelegate (int i, int j);
18
19         [DllImport ("libtest", EntryPoint="mono_test_marshal_cdecl_delegate")]
20         public static extern int mono_test_marshal_cdecl_delegate (CdeclDelegate d);    
21
22         public static int cdecl_delegate (int i, int j) {
23                 return i + j;
24         }
25
26         public static int test_0_marshal_cdecl_delegate () {
27                 CdeclDelegate d = new CdeclDelegate (cdecl_delegate);
28
29                 return mono_test_marshal_cdecl_delegate (d);
30         }
31
32         [DllImport ("libtest", EntryPoint="mono_test_marshal_return_fnptr")]
33         public static extern IntPtr mono_test_marshal_return_fnptr ();
34
35         delegate int AddDelegate (int i, int j);
36
37         public static int test_4_get_delegate_for_function_pointer () {
38                 IntPtr ptr = mono_test_marshal_return_fnptr ();
39
40                 AddDelegate d = (AddDelegate)Marshal.GetDelegateForFunctionPointer (ptr, typeof (AddDelegate));
41
42                 return d (2, 2);
43         }
44
45         [DllImport ("libtest")]
46         public static extern int mono_return_int (int i);
47
48         [DllImport ("libtest", EntryPoint="not_found")]
49         public static extern int not_found (int i);
50
51         public delegate int SimpleDelegate (int i);
52
53         [DllImport ("libtest")]
54         public static extern int mono_invoke_simple_delegate (SimpleDelegate d);
55
56         public static int test_4_native_function_to_ftnptr () {
57                 return mono_invoke_simple_delegate (mono_return_int);
58         }
59
60         public static int test_0_native_function_to_ftnptr_not_found () {
61                 try {
62                         return mono_invoke_simple_delegate (not_found);
63                 } catch (EntryPointNotFoundException) {
64                         return 0;
65                 }
66         }               
67
68 }