forked from samiamlabs/ros2_dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DllLoadUtils.cs
362 lines (300 loc) · 11.9 KB
/
DllLoadUtils.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Copyright 2016-2018 Esteve Fernandez <esteve@apache.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Based on http://dimitry-i.blogspot.com.es/2013/01/mononet-how-to-dynamically-load-native.html
using System;
using System.Runtime;
using System.Runtime.InteropServices;
namespace ROS2 {
namespace Utils {
public class GlobalVariables {
public static bool preloadLibrary = false;
public static string preloadLibraryName = "";
}
public class UnsatisfiedLinkError : System.Exception {
public UnsatisfiedLinkError () : base () { }
public UnsatisfiedLinkError (string message) : base (message) { }
public UnsatisfiedLinkError (string message, System.Exception inner) : base (message, inner) { }
}
public class UnknownPlatformError : System.Exception {
public UnknownPlatformError () : base () { }
public UnknownPlatformError (string message) : base (message) { }
public UnknownPlatformError (string message, System.Exception inner) : base (message, inner) { }
}
public enum Platform {
Unix,
MacOSX,
WindowsDesktop,
UWP,
Unknown
}
public class DllLoadUtilsFactory {
[DllImport ("api-ms-win-core-libraryloader-l2-1-0.dll", EntryPoint = "LoadPackagedLibrary", SetLastError = true, ExactSpelling = true)]
private static extern IntPtr LoadPackagedLibrary ([MarshalAs (UnmanagedType.LPWStr)] string fileName, int reserved = 0);
[DllImport ("api-ms-win-core-libraryloader-l1-2-0.dll", EntryPoint = "FreeLibrary", SetLastError = true, ExactSpelling = true)]
private static extern int FreeLibraryUWP (IntPtr handle);
[DllImport ("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary (string fileName);
[DllImport ("kernel32.dll", EntryPoint = "FreeLibrary", SetLastError = true)]
private static extern int FreeLibraryDesktop (IntPtr handle);
[DllImport ("libdl.so", EntryPoint = "dlopen")]
private static extern IntPtr dlopen_unix (String fileName, int flags);
[DllImport ("libdl.so", EntryPoint = "dlclose")]
private static extern int dlclose_unix (IntPtr handle);
[DllImport ("libdl.dylib", EntryPoint = "dlopen")]
private static extern IntPtr dlopen_macosx (String fileName, int flags);
[DllImport ("libdl.dylib", EntryPoint = "dlclose")]
private static extern int dlclose_macosx (IntPtr handle);
const int RTLD_NOW = 2;
public static DllLoadUtils GetDllLoadUtils () {
switch (CheckPlatform ()) {
case Platform.Unix:
return new DllLoadUtilsUnix ();
case Platform.MacOSX:
return new DllLoadUtilsMacOSX ();
case Platform.WindowsDesktop:
return new DllLoadUtilsWindowsDesktop ();
case Platform.UWP:
return new DllLoadUtilsUWP ();
case Platform.Unknown:
default:
throw new UnknownPlatformError ();
}
}
private static bool IsUWP () {
try {
IntPtr ptr = LoadPackagedLibrary ("api-ms-win-core-libraryloader-l2-1-0.dll");
FreeLibraryUWP (ptr);
return true;
} catch (TypeLoadException) {
return false;
}
}
private static bool IsWindowsDesktop () {
try {
IntPtr ptr = LoadLibrary ("kernel32.dll");
FreeLibraryDesktop (ptr);
return true;
} catch (TypeLoadException) {
return false;
}
}
private static bool IsUnix () {
try {
IntPtr ptr = dlopen_unix ("libdl.so", RTLD_NOW);
dlclose_unix (ptr);
return true;
} catch (TypeLoadException) {
return false;
}
}
private static bool IsMacOSX () {
try {
IntPtr ptr = dlopen_macosx ("libdl.dylib", RTLD_NOW);
dlclose_macosx (ptr);
return true;
} catch (TypeLoadException) {
return false;
}
}
private static Platform CheckPlatform () {
if (IsUnix())
{
return Platform.Unix;
}
else if (IsMacOSX())
{
return Platform.MacOSX;
}
else if (IsWindowsDesktop())
{
return Platform.WindowsDesktop;
}
else if (IsUWP())
{
return Platform.UWP;
}
else
{
return Platform.Unknown;
}
}
}
public interface DllLoadUtils {
IntPtr LoadLibrary (string fileName);
IntPtr LoadLibraryNoSuffix (string fileName);
void FreeLibrary (IntPtr handle);
IntPtr GetProcAddress (IntPtr dllHandle, string name);
}
public class DllLoadUtilsUWP : DllLoadUtils {
[DllImport ("api-ms-win-core-libraryloader-l2-1-0.dll", SetLastError = true, ExactSpelling = true)]
private static extern IntPtr LoadPackagedLibrary ([MarshalAs (UnmanagedType.LPWStr)] string fileName, int reserved = 0);
[DllImport ("api-ms-win-core-libraryloader-l1-2-0.dll", SetLastError = true, ExactSpelling = true)]
private static extern int FreeLibrary (IntPtr handle);
[DllImport ("api-ms-win-core-libraryloader-l1-2-0.dll", SetLastError = true, ExactSpelling = true)]
private static extern IntPtr GetProcAddress (IntPtr handle, string procedureName);
void DllLoadUtils.FreeLibrary (IntPtr handle) {
FreeLibrary (handle);
}
IntPtr DllLoadUtils.GetProcAddress (IntPtr dllHandle, string name) {
return GetProcAddress (dllHandle, name);
}
IntPtr DllLoadUtils.LoadLibrary (string fileName) {
string libraryName = fileName + "_native.dll";
IntPtr ptr = LoadPackagedLibrary (libraryName);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
IntPtr DllLoadUtils.LoadLibraryNoSuffix (string fileName) {
string libraryName = fileName + ".dll";
IntPtr ptr = LoadPackagedLibrary (libraryName);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
}
public class DllLoadUtilsWindowsDesktop : DllLoadUtils {
[DllImport ("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary (string fileName);
[DllImport ("kernel32.dll", SetLastError = true, ExactSpelling = true)]
private static extern int FreeLibrary (IntPtr handle);
[DllImport ("kernel32.dll", SetLastError = true, ExactSpelling = true)]
private static extern IntPtr GetProcAddress (IntPtr handle, string procedureName);
void DllLoadUtils.FreeLibrary (IntPtr handle) {
FreeLibrary (handle);
}
IntPtr DllLoadUtils.GetProcAddress (IntPtr dllHandle, string name) {
return GetProcAddress (dllHandle, name);
}
IntPtr DllLoadUtils.LoadLibrary (string fileName) {
string libraryName = fileName + "_native.dll";
IntPtr ptr = LoadLibrary (libraryName);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
IntPtr DllLoadUtils.LoadLibraryNoSuffix (string fileName) {
string libraryName = fileName + ".dll";
IntPtr ptr = LoadLibrary (libraryName);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
}
internal class DllLoadUtilsUnix : DllLoadUtils {
[DllImport ("libdl.so", ExactSpelling = true)]
private static extern IntPtr dlopen (String fileName, int flags);
[DllImport ("libdl.so", ExactSpelling = true)]
private static extern IntPtr dlsym (IntPtr handle, String symbol);
[DllImport ("libdl.so", ExactSpelling = true)]
private static extern int dlclose (IntPtr handle);
[DllImport ("libdl.so", ExactSpelling = true)]
private static extern IntPtr dlerror ();
const int RTLD_NOW = 0x00002;
const int RTLD_DEEPBIND = 0x00008;
//(adamdbrw) Somewhat hacky solution to open (and dereference) the problematic library
//that otherwise causes crashes in Unity Editor. TODO - improve this
private bool libPreloaded = false;
void CheckPreloadLibraries()
{
if (libPreloaded || GlobalVariables.preloadLibraryName == "")
return;
IntPtr libPtr = dlopen(GlobalVariables.preloadLibraryName, RTLD_NOW | RTLD_DEEPBIND);
if (libPtr == IntPtr.Zero)
throw new InvalidOperationException("Zero pointer");
//Dereference the counter right away
FreeLibrary(libPtr); //TODO retval?
libPreloaded = true;
}
public void FreeLibrary (IntPtr handle) {
dlclose (handle);
}
public IntPtr GetProcAddress (IntPtr dllHandle, string name) {
// clear previous errors if any
dlerror ();
var res = dlsym (dllHandle, name);
var errPtr = dlerror ();
if (errPtr != IntPtr.Zero) {
throw new Exception ("dlsym: " + Marshal.PtrToStringAnsi (errPtr));
}
return res;
}
public IntPtr LoadLibrary (string fileName) {
if (GlobalVariables.preloadLibrary)
CheckPreloadLibraries();
string libraryName = "lib" + fileName + "_native.so";
IntPtr ptr = dlopen (libraryName, RTLD_NOW);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
public IntPtr LoadLibraryNoSuffix (string fileName) {
if (GlobalVariables.preloadLibrary)
CheckPreloadLibraries();
string libraryName = "lib" + fileName + ".so";
IntPtr ptr = dlopen (libraryName, RTLD_NOW);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
}
internal class DllLoadUtilsMacOSX : DllLoadUtils {
[DllImport ("libdl.dylib", ExactSpelling = true)]
private static extern IntPtr dlopen (String fileName, int flags);
[DllImport ("libdl.dylib", ExactSpelling = true)]
private static extern IntPtr dlsym (IntPtr handle, String symbol);
[DllImport ("libdl.dylib", ExactSpelling = true)]
private static extern int dlclose (IntPtr handle);
[DllImport ("libdl.dylib", ExactSpelling = true)]
private static extern IntPtr dlerror ();
const int RTLD_NOW = 2;
public void FreeLibrary (IntPtr handle) {
dlclose (handle);
}
public IntPtr GetProcAddress (IntPtr dllHandle, string name) {
// clear previous errors if any
dlerror ();
var res = dlsym (dllHandle, name);
var errPtr = dlerror ();
if (errPtr != IntPtr.Zero) {
throw new Exception ("dlsym: " + Marshal.PtrToStringAnsi (errPtr));
}
return res;
}
public IntPtr LoadLibrary (string fileName) {
string libraryName = "lib" + fileName + "_native.dylib";
IntPtr ptr = dlopen (libraryName, RTLD_NOW);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
public IntPtr LoadLibraryNoSuffix (string fileName) {
string libraryName = "lib" + fileName + ".dylib";
IntPtr ptr = dlopen (libraryName, RTLD_NOW);
if (ptr == IntPtr.Zero) {
throw new UnsatisfiedLinkError (libraryName);
}
return ptr;
}
}
}
}