-
Notifications
You must be signed in to change notification settings - Fork 1
/
DynamicDll.cs
192 lines (159 loc) · 7.11 KB
/
DynamicDll.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System.Reflection;
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
namespace MidiToMGBA {
public static class DynamicDll {
private readonly static IntPtr NULL = IntPtr.Zero;
public static Dictionary<string, string> DllMap = new Dictionary<string, string>();
// Windows
[DllImport("kernel32")]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32")]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
// Linux
private const int RTLD_NOW = 2;
[DllImport("dl", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlopen([MarshalAs(UnmanagedType.LPTStr)] string filename, int flags);
[DllImport("dl", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlsym(IntPtr handle, [MarshalAs(UnmanagedType.LPTStr)] string symbol);
[DllImport("dl", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr dlerror();
private static IntPtr _EntryPoint = NULL;
public static IntPtr EntryPoint {
get {
if (_EntryPoint != NULL)
return _EntryPoint;
return _EntryPoint = OpenLibrary(null);
}
}
private static IntPtr _Mono = NULL;
public static IntPtr Mono {
get {
if (_Mono != NULL)
return _Mono;
return _Mono = OpenLibrary(
Environment.OSVersion.Platform == PlatformID.Win32NT ? "mono.dll" :
Environment.OSVersion.Platform == PlatformID.MacOSX ? "libmono.0.dylib" :
"libmono.so"
);
}
}
private static IntPtr _PThread = NULL;
public static IntPtr PThread {
get {
if (_PThread != NULL)
return _PThread;
if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX)
return NULL;
return _PThread = OpenLibrary(
Environment.OSVersion.Platform == PlatformID.MacOSX ? "libpthread.dylib" :
"libpthread.so"
);
}
}
public static IntPtr OpenLibrary(string name) {
string mapped;
if (DllMap.TryGetValue(name, out mapped))
name = mapped;
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
IntPtr lib = GetModuleHandle(name);
if (lib == NULL) {
lib = LoadLibrary(name);
}
return lib;
}
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) {
IntPtr e = IntPtr.Zero;
IntPtr lib = dlopen(name, RTLD_NOW);
if ((e = dlerror()) != IntPtr.Zero) {
Console.WriteLine($"PInvokeHelper can't access {name}!");
Console.WriteLine("dlerror: " + Marshal.PtrToStringAnsi(e));
return NULL;
}
return lib;
}
return NULL;
}
public static IntPtr GetFunction(this IntPtr lib, string name) {
if (lib == NULL)
return NULL;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
return GetProcAddress(lib, name);
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX) {
IntPtr s, e;
s = dlsym(lib, name);
if ((e = dlerror()) != IntPtr.Zero) {
Console.WriteLine("PInvokeHelper can't access " + name + "!");
Console.WriteLine("dlerror: " + Marshal.PtrToStringAnsi(e));
return NULL;
}
return s;
}
return NULL;
}
public static T GetDelegate<T>(this IntPtr lib, string name) where T : class {
if (lib == NULL)
return null;
IntPtr s = lib.GetFunction(name);
if (s == NULL)
return null;
return s.AsDelegate<T>();
}
public static T GetDelegateAtRVA<T>(this IntPtr basea, long rva) where T : class {
return new IntPtr(basea.ToInt64() + rva).AsDelegate<T>();
}
public static T AsDelegate<T>(this IntPtr s) where T : class {
return Marshal.GetDelegateForFunctionPointer(s, typeof(T)) as T;
}
// Windows
[DllImport("kernel32")]
private static extern uint GetCurrentThreadId();
// Linux
private delegate ulong d_pthread_self();
private static d_pthread_self pthread_self;
public static ulong CurrentThreadId {
get {
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
return GetCurrentThreadId();
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
return (pthread_self = pthread_self ?? PThread.GetDelegate<d_pthread_self>("pthread_self"))?.Invoke() ?? 0;
return 0;
}
}
public static void ResolveDynamicDllImports(this Type type) {
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) {
bool found = true;
foreach (DynamicDllImportAttribute attrib in field.GetCustomAttributes(typeof(DynamicDllImportAttribute), true)) {
found = false;
IntPtr asm = OpenLibrary(attrib.DLL);
if (asm == NULL)
continue;
foreach (string ep in attrib.EntryPoints) {
IntPtr func = asm.GetFunction(ep);
if (func == NULL)
continue;
field.SetValue(null, Marshal.GetDelegateForFunctionPointer(func, field.FieldType));
found = true;
break;
}
if (found)
break;
}
if (!found)
throw new EntryPointNotFoundException($"No matching entry point found for {field.Name} in {field.DeclaringType.FullName}");
}
}
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class DynamicDllImportAttribute : Attribute {
public string DLL;
public string[] EntryPoints;
public DynamicDllImportAttribute(string dll, params string[] entryPoints) {
DLL = dll;
EntryPoints = entryPoints;
}
}
}