-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTest_ManagedFunctionPointer.cs
More file actions
63 lines (44 loc) · 2.02 KB
/
Test_ManagedFunctionPointer.cs
File metadata and controls
63 lines (44 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Runtime.InteropServices;
namespace LibNamespace
{
public static class Test_ManagedFunctionPointer
{
// begin-snippet: Test_ManagedFunctionPointer_FunctionPointerCallbackDelegate_CS
public delegate int FunctionPointerCallbackDelegate(int a);
// end-snippet
#region Test_ManagedFunctionPointer_Instance_CS
public class CallableObject
{
public FunctionPointerCallbackDelegate CallbackDelegate;
public IntPtr CallbackFunctionPointer;
private int Member;
public int Callback(int i) => i + Member;
public CallableObject(int member)
{
Member = member;
CallbackDelegate = new FunctionPointerCallbackDelegate(Callback);
CallbackFunctionPointer = Marshal.GetFunctionPointerForDelegate(CallbackDelegate);
}
}
public static CallableObject CallableObjectInstance;
// Enty point for unmanaged code
[UnmanagedCallersOnly]
public static IntPtr Test_ManagedFunctionPointer_Instance(int member)
{
CallableObjectInstance = new CallableObject(member);
return CallableObjectInstance.CallbackFunctionPointer;
}
#endregion // Test_ManagedFunctionPointer_Instance_CS ----------------------------------------------------------
#region Test_ManagedFunctionPointer_Static_CS
public static FunctionPointerCallbackDelegate FunctionPointerCallbackDelegateStatic = new FunctionPointerCallbackDelegate(Callback);
public static int Callback(int i) => i + 3;
// Enty point for unmanaged code
[UnmanagedCallersOnly]
public static IntPtr Test_ManagedFunctionPointer_Static()
{
return Marshal.GetFunctionPointerForDelegate(FunctionPointerCallbackDelegateStatic);
}
#endregion // Test_ManagedFunctionPointer_Static_CS -------------------------------------------------------------
}
}