-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathMacDllImportLibraryPathResolver.cs
92 lines (75 loc) · 2.52 KB
/
MacDllImportLibraryPathResolver.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
using System;
using System.IO;
using System.Runtime.InteropServices;
using NetNativeLibLoader.PathResolver;
namespace Qml.Net.Internal
{
public class MacDllImportLibraryPathResolver : IPathResolver
{
IPathResolver _original;
public MacDllImportLibraryPathResolver(IPathResolver original)
{
_original = original;
}
public ResolvePathResult Resolve(string library)
{
var result = _original.Resolve(library);
if (!result.IsSuccess && library == "QmlNet")
{
// Try to let .NET load the library.
try
{
qml_net_getVersion();
// The method invoked correctly, so .NET loaded it.
// Let's return the path to it.
var dll = dlopen("libQmlNet.dylib", SymbolFlag.RtldLazy);
if (dll == IntPtr.Zero)
{
return result;
}
var sym = dlsym(dll, "qml_net_getVersion");
if (sym == IntPtr.Zero)
{
return result;
}
var info = new DlInfo();
if (dladdr(sym, ref info) != 1)
{
return result;
}
var location = Marshal.PtrToStringAnsi(info.fname);
if (File.Exists(location))
{
return ResolvePathResult.FromSuccess(location);
}
}
// ReSharper disable EmptyGeneralCatchClause
catch (Exception)
// ReSharper restore EmptyGeneralCatchClause
{
}
}
return result;
}
[DllImport("libQmlNet.dylib")]
static extern long qml_net_getVersion();
[DllImport("dl")]
static extern IntPtr dlopen(string fileName, SymbolFlag flags);
[DllImport("dl")]
static extern IntPtr dlsym(IntPtr handle, string name);
[DllImport("dl")]
static extern int dladdr(IntPtr handle, ref DlInfo info);
private struct DlInfo
{
public IntPtr fname;
private IntPtr notUsed1;
private IntPtr notUsed2;
private IntPtr notUsed3;
}
[Flags]
private enum SymbolFlag
{
RtldLazy = 0x00001
}
}
}