@@ -28,9 +28,9 @@ public static ILibraryLoader Instance
28
28
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
29
29
_instance = new WindowsLoader ( ) ;
30
30
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
31
- _instance = new LinuxLoader ( ) ;
31
+ _instance = new PosixLoader ( LinuxLibDL . GetInstance ( ) ) ;
32
32
else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
33
- _instance = new DarwinLoader ( ) ;
33
+ _instance = new PosixLoader ( new MacLibDL ( ) ) ;
34
34
else
35
35
throw new PlatformNotSupportedException (
36
36
"This operating system is not supported"
@@ -42,87 +42,19 @@ public static ILibraryLoader Instance
42
42
}
43
43
}
44
44
45
- class LinuxLoader : ILibraryLoader
45
+ class PosixLoader : ILibraryLoader
46
46
{
47
- private static int RTLD_NOW = 0x2 ;
48
- private static int RTLD_GLOBAL = 0x100 ;
49
- private static IntPtr RTLD_DEFAULT = IntPtr . Zero ;
50
- private const string NativeDll = "libdl.so" ;
47
+ private readonly ILibDL libDL ;
51
48
52
- public IntPtr Load ( string dllToLoad )
53
- {
54
- ClearError ( ) ;
55
- var res = dlopen ( dllToLoad , RTLD_NOW | RTLD_GLOBAL ) ;
56
- if ( res == IntPtr . Zero )
57
- {
58
- var err = GetError ( ) ;
59
- throw new DllNotFoundException ( $ "Could not load { dllToLoad } with flags RTLD_NOW | RTLD_GLOBAL: { err } ") ;
60
- }
61
-
62
- return res ;
63
- }
64
-
65
- public void Free ( IntPtr handle )
66
- {
67
- dlclose ( handle ) ;
68
- }
69
-
70
- public IntPtr GetFunction ( IntPtr dllHandle , string name )
71
- {
72
- // look in the exe if dllHandle is NULL
73
- if ( dllHandle == IntPtr . Zero )
74
- {
75
- dllHandle = RTLD_DEFAULT ;
76
- }
77
-
78
- ClearError ( ) ;
79
- IntPtr res = dlsym ( dllHandle , name ) ;
80
- if ( res == IntPtr . Zero )
81
- {
82
- var err = GetError ( ) ;
83
- throw new MissingMethodException ( $ "Failed to load symbol { name } : { err } ") ;
84
- }
85
- return res ;
86
- }
87
-
88
- void ClearError ( )
49
+ public PosixLoader ( ILibDL libDL )
89
50
{
90
- dlerror ( ) ;
91
- }
92
-
93
- string GetError ( )
94
- {
95
- var res = dlerror ( ) ;
96
- if ( res != IntPtr . Zero )
97
- return Marshal . PtrToStringAnsi ( res ) ;
98
- else
99
- return null ;
51
+ this . libDL = libDL ?? throw new ArgumentNullException ( nameof ( libDL ) ) ;
100
52
}
101
53
102
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
103
- public static extern IntPtr dlopen ( string fileName , int flags ) ;
104
-
105
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
106
- private static extern IntPtr dlsym ( IntPtr handle , string symbol ) ;
107
-
108
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
109
- private static extern int dlclose ( IntPtr handle ) ;
110
-
111
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
112
- private static extern IntPtr dlerror ( ) ;
113
- }
114
-
115
- class DarwinLoader : ILibraryLoader
116
- {
117
- private static int RTLD_NOW = 0x2 ;
118
- private static int RTLD_GLOBAL = 0x8 ;
119
- private const string NativeDll = "/usr/lib/libSystem.dylib" ;
120
- private static IntPtr RTLD_DEFAULT = new IntPtr ( - 2 ) ;
121
-
122
54
public IntPtr Load ( string dllToLoad )
123
55
{
124
56
ClearError ( ) ;
125
- var res = dlopen ( dllToLoad , RTLD_NOW | RTLD_GLOBAL ) ;
57
+ var res = libDL . dlopen ( dllToLoad , libDL . RTLD_NOW | libDL . RTLD_GLOBAL ) ;
126
58
if ( res == IntPtr . Zero )
127
59
{
128
60
var err = GetError ( ) ;
@@ -134,19 +66,19 @@ public IntPtr Load(string dllToLoad)
134
66
135
67
public void Free ( IntPtr handle )
136
68
{
137
- dlclose ( handle ) ;
69
+ libDL . dlclose ( handle ) ;
138
70
}
139
71
140
72
public IntPtr GetFunction ( IntPtr dllHandle , string name )
141
73
{
142
74
// look in the exe if dllHandle is NULL
143
75
if ( dllHandle == IntPtr . Zero )
144
76
{
145
- dllHandle = RTLD_DEFAULT ;
77
+ dllHandle = libDL . RTLD_DEFAULT ;
146
78
}
147
79
148
80
ClearError ( ) ;
149
- IntPtr res = dlsym ( dllHandle , name ) ;
81
+ IntPtr res = libDL . dlsym ( dllHandle , name ) ;
150
82
if ( res == IntPtr . Zero )
151
83
{
152
84
var err = GetError ( ) ;
@@ -157,29 +89,17 @@ public IntPtr GetFunction(IntPtr dllHandle, string name)
157
89
158
90
void ClearError ( )
159
91
{
160
- dlerror ( ) ;
92
+ libDL . dlerror ( ) ;
161
93
}
162
94
163
95
string GetError ( )
164
96
{
165
- var res = dlerror ( ) ;
97
+ var res = libDL . dlerror ( ) ;
166
98
if ( res != IntPtr . Zero )
167
99
return Marshal . PtrToStringAnsi ( res ) ;
168
100
else
169
101
return null ;
170
102
}
171
-
172
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
173
- public static extern IntPtr dlopen ( String fileName , int flags ) ;
174
-
175
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl , CharSet = CharSet . Ansi ) ]
176
- private static extern IntPtr dlsym ( IntPtr handle , String symbol ) ;
177
-
178
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
179
- private static extern int dlclose ( IntPtr handle ) ;
180
-
181
- [ DllImport ( NativeDll , CallingConvention = CallingConvention . Cdecl ) ]
182
- private static extern IntPtr dlerror ( ) ;
183
103
}
184
104
185
105
class WindowsLoader : ILibraryLoader
0 commit comments