forked from QuantBox/QuantBox_XAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoInvoke.cs
52 lines (47 loc) · 1.42 KB
/
SoInvoke.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace QuantBox.XAPI
{
public class SoInvoke : InvokeBase
{
[DllImport("libdl.so")]
private extern static IntPtr dlopen(string path, int mode);
[DllImport("libdl.so")]
private extern static IntPtr dlsym(IntPtr handle, string symbol);
[DllImport("libdl.so")]
private extern static IntPtr dlerror();
[DllImport("libdl.so")]
private extern static int dlclose(IntPtr handle);
public SoInvoke(string DLLPath)
{
hLib = dlopen(DLLPath, 1);
if (hLib == IntPtr.Zero)
{
IntPtr errptr = dlerror();
string error = Marshal.PtrToStringAnsi(errptr);
throw new Exception(error);
}
}
~SoInvoke()
{
Dispose();
}
//将要执行的函数转换为委托
public override Delegate Invoke(String APIName, Type t)
{
if (hLib == IntPtr.Zero)
return (Delegate)null;
IntPtr api = dlsym(hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
public override void Dispose()
{
if (hLib != IntPtr.Zero)
dlclose(hLib);
hLib = IntPtr.Zero;
}
}
}