-
Notifications
You must be signed in to change notification settings - Fork 21
/
LinuxLoader.cs
45 lines (38 loc) · 1.23 KB
/
LinuxLoader.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
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace EvHttpSharp.Interop
{
class LinuxLoader : IDynLoader
{
// ReSharper disable InconsistentNaming
[DllImport("libdl.so.2")]
private static extern IntPtr dlopen(string path, int flags);
[DllImport ("libdl.so.2")]
private static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport ("libdl.so.2")]
private static extern IntPtr dlerror ();
// ReSharper restore InconsistentNaming
static string DlError()
{
return Marshal.PtrToStringAuto(dlerror());
}
public IntPtr LoadLibrary(string basePath, string dll)
{
dll += "-2.0.so.5";
if (basePath != null)
dll = System.IO.Path.Combine(basePath, dll);
var handle = dlopen(dll, 1);
if (handle == IntPtr.Zero)
throw new Win32Exception(DlError());
return handle;
}
public IntPtr GetProcAddress(IntPtr dll, string proc)
{
var ptr = dlsym(dll, proc);
if (ptr == IntPtr.Zero)
throw new Win32Exception(DlError());
return ptr;
}
}
}