@@ -129,7 +129,13 @@ internal static bool TryLoad(string fileName, string hintPath)
129129 fileName = Path . ChangeExtension ( fileName , Extension ) ;
130130 }
131131
132- // If we have hint path provided by the user, look there first
132+ // First just try to load it with the file name only
133+ if ( TryLoadDirect ( fileName ) )
134+ {
135+ return true ;
136+ }
137+
138+ // If we have hint path provided by the user, look there next
133139 if ( hintPath != null && TryLoadFromDirectory ( fileName , hintPath ) )
134140 {
135141 return true ;
@@ -277,6 +283,41 @@ static bool TryLoadFromDirectory(string fileName, string directory)
277283 }
278284 }
279285
286+ /// <summary>
287+ /// Try to load a native library by only the file name of the library.
288+ /// </summary>
289+ /// <returns>True if the library was successfully loaded or if it has already been loaded.</returns>
290+ static bool TryLoadDirect ( string fileName )
291+ {
292+ lock ( StaticLock )
293+ {
294+ if ( NativeHandles . Value . TryGetValue ( fileName , out IntPtr libraryHandle ) )
295+ {
296+ return true ;
297+ }
298+
299+ #if NET5_0_OR_GREATER
300+ return NativeLibrary . TryLoad ( fileName , out libraryHandle ) ;
301+ #else
302+ // If successful this will return a handle to the library
303+ libraryHandle = IsUnix ? UnixLoader . LoadLibrary ( fileName ) : WindowsLoader . LoadLibrary ( fileName ) ;
304+ if ( libraryHandle == IntPtr . Zero )
305+ {
306+ int lastError = Marshal . GetLastWin32Error ( ) ;
307+ var exception = new System . ComponentModel . Win32Exception ( lastError ) ;
308+ LastException = exception ;
309+ }
310+ else
311+ {
312+ LastException = null ;
313+ NativeHandles . Value [ fileName ] = libraryHandle ;
314+ }
315+
316+ return libraryHandle != IntPtr . Zero ;
317+ #endif
318+ }
319+ }
320+
280321 /// <summary>
281322 /// Try to load a native library by providing the full path including the file name of the library.
282323 /// </summary>
0 commit comments