public override void Load() { // https://docs.microsoft.com/en-us/dotnet/core/rid-catalog var runtimeIdentifier = $"win-{GetProcessorArchitecture()}"; var rootDirectory = GetCurrentDir(); // Search a few different locations for our native assembly var paths = new[] { // This is where native libraries in our nupkg should end up GetRuntimeLibraryPath(rootDirectory, runtimeIdentifier, LibraryName), // The build output folder GetCurrentDirectoryLibraryPath(rootDirectory, LibraryName), }; foreach (var path in paths) { if (string.IsNullOrEmpty(path)) { continue; } if (File.Exists(path)) { var libHandle = NativeMethodsSystemWindows.LoadLibraryEx( path, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_SYSTEM32); if (libHandle.IsInvalid) { throw new DllNotLoadedException($"LoadLibrary failed: {path}"); } _libraryHandle = libHandle; return; } } throw new DllNotLoadedException(); }
protected override bool ReleaseHandle() { return(NativeMethodsSystemWindows.FreeLibrary(handle)); }