@@ -55,7 +55,7 @@ public static class Importers
55
55
56
56
private class WindowsImporter : INativeLibImporter
57
57
{
58
- [ DllImport ( "kernel32.dll" , EntryPoint = "LoadLibrary" ) ]
58
+ [ DllImport ( "kernel32.dll" , EntryPoint = "LoadLibrary" , SetLastError = true ) ]
59
59
public static extern IntPtr WinLoadLibrary ( string dllToLoad ) ;
60
60
61
61
[ DllImport ( "kernel32.dll" , EntryPoint = "GetProcAddress" ) ]
@@ -66,7 +66,10 @@ private class WindowsImporter : INativeLibImporter
66
66
67
67
public IntPtr LoadLibrary ( string path )
68
68
{
69
- return WinLoadLibrary ( path ) ;
69
+ var result = WinLoadLibrary ( path ) ;
70
+ if ( result == IntPtr . Zero )
71
+ throw new System . ComponentModel . Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
72
+ return result ;
70
73
}
71
74
72
75
public IntPtr GetProcAddress ( IntPtr lib , string entryPoint )
@@ -134,25 +137,27 @@ static string GetPlatform()
134
137
Marshal . FreeHGlobal ( buf ) ;
135
138
}
136
139
}
140
+
137
141
public IntPtr LoadLibrary ( string path )
138
142
{
139
143
dlerror ( ) ;
140
144
var lib = dlopen ( path , 2 ) ;
141
- /*
142
145
var errPtr = dlerror ( ) ;
143
146
if ( errPtr != IntPtr . Zero )
144
147
throw new NativeLoadException ( "dlopen: " + Marshal . PtrToStringAnsi ( errPtr ) , null ) ;
145
- */
146
148
return lib ;
147
149
}
148
150
149
151
public IntPtr GetProcAddress ( IntPtr lib , string entryPoint )
150
152
{
151
153
dlerror ( ) ;
152
154
IntPtr address = dlsym ( lib , entryPoint ) ;
153
- var errPtr = dlerror ( ) ;
154
- if ( errPtr != IntPtr . Zero )
155
- throw new NativeLoadException ( "dlsym: " + Marshal . PtrToStringAnsi ( errPtr ) , null ) ;
155
+ if ( address == IntPtr . Zero )
156
+ {
157
+ var errPtr = dlerror ( ) ;
158
+ if ( errPtr != IntPtr . Zero )
159
+ throw new NativeLoadException ( "dlsym: " + Marshal . PtrToStringAnsi ( errPtr ) , null ) ;
160
+ }
156
161
return address ;
157
162
}
158
163
@@ -174,6 +179,8 @@ public static T LoadFunc<T>(INativeLibImporter importer, IntPtr libraryHandle, s
174
179
IntPtr procAddress = importer . GetProcAddress ( libraryHandle , entryPoint ) ;
175
180
if ( procAddress == IntPtr . Zero )
176
181
{
182
+ throw new Exception ( $ "Cannot get proc address of { entryPoint } ") ;
183
+ /*
177
184
var invokeMethod = typeof(T).GetTypeInfo().GetMethod("Invoke");
178
185
var parameters = invokeMethod.GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToArray();
179
186
var returnType = invokeMethod.ReturnType;
@@ -184,6 +191,7 @@ public static T LoadFunc<T>(INativeLibImporter importer, IntPtr libraryHandle, s
184
191
var block = Expression.Block(returnType, Expression.Invoke(callThrowExpr), defaultExpr);
185
192
var lambda = Expression.Lambda<T>(block, parameters);
186
193
return lambda.Compile();
194
+ */
187
195
}
188
196
return CurrentFramework . GetDelegateForFunctionPointer < T > ( procAddress ) ;
189
197
}
@@ -359,20 +367,41 @@ public static T Import<T>(INativeLibImporter importer, string libName, string ve
359
367
paths . SelectMany ( path => names . Select ( n => Path . Combine ( basePath , path , importer . Translate ( n ) ) ) )
360
368
. Concat ( names . Select ( n => importer . Translate ( n ) ) )
361
369
)
370
+ . Select ( path => new SearchPath { Path = path } )
362
371
. ToArray ( ) ;
363
372
364
373
foreach ( var spec in search )
365
374
{
366
375
var construct = type . GetConstructor ( new Type [ ] { typeof ( INativeLibImporter ) , typeof ( IntPtr ) } ) ;
367
- var lib = importer . LoadLibrary ( spec ) ;
368
- if ( lib == IntPtr . Zero )
376
+ IntPtr lib = IntPtr . Zero ;
377
+ try
378
+ {
379
+ lib = importer . LoadLibrary ( spec . Path ) ;
380
+ if ( lib == IntPtr . Zero )
381
+ throw new NativeLoadException ( "LoadLibrary returned 0" , null ) ;
382
+ }
383
+ catch ( TargetInvocationException tie )
384
+ {
385
+ spec . Error = tie . InnerException ;
386
+ continue ;
387
+ }
388
+ catch ( Exception e )
389
+ {
390
+ spec . Error = e ;
369
391
continue ;
392
+ }
370
393
var obj = construct . Invoke ( new object [ ] { importer , lib } ) ;
371
394
var t = obj as T ;
372
395
return t ;
373
396
}
374
397
375
- throw new NativeLoadException ( "Unable to locate rocksdb native library, either install it, or use RocksDbNative nuget package\n Searched:" + string . Join ( "\n " , search ) , null ) ;
398
+ throw new NativeLoadException ( "Unable to locate rocksdb native library, either install it, or use RocksDbNative nuget package\n Searched:" + string . Join ( "\n " , search . Select ( s => $ "{ s . Path } : ({ s . Error . GetType ( ) . Name } ) { s . Error . Message } ") ) , null ) ;
399
+ }
400
+
401
+ private class SearchPath
402
+ {
403
+ public string Path { get ; set ; }
404
+ public Exception Error { get ; set ; }
376
405
}
377
406
378
407
private static string GetMethodSig ( MethodInfo m )
0 commit comments