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 loaded = GetModuleHandle("QmlNet"); var bytes = Marshal.AllocHGlobal(2000); var r = GetModuleFileName(loaded, bytes, 2000); var path = Marshal.PtrToStringAnsi(bytes); Marshal.FreeHGlobal(bytes); if (File.Exists(path)) { return(ResolvePathResult.FromSuccess(path)); } } catch (Exception) { } } return(result); }
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(); using (var currentProcess = Process.GetCurrentProcess()) { foreach (ProcessModule module in currentProcess.Modules) { if (Path.GetFileNameWithoutExtension(module.FileName) == "libQmlNet") { return(ResolvePathResult.FromSuccess(module.FileName)); } } } } // ReSharper disable EmptyGeneralCatchClause catch (Exception) // ReSharper restore EmptyGeneralCatchClause { } } return(result); }
/// <summary> /// Resolves the absolute path to the given library. /// </summary> /// <param name="library">The name or path of the library to load.</param> /// <param name="localFirst">Whether or not the executable's local directory should be searched first.</param> /// <returns>The absolute path to the library.</returns> /// <exception cref="PlatformNotSupportedException"> /// Thrown if the current platform doesn't have a path /// resolver defined.</exception> /// <exception cref="FileNotFoundException">Thrown if no library file can be found.</exception> private ResolvePathResult ResolveAbsolutePath([NotNull] string library, bool localFirst) { var candidates = GenerateLibraryCandidates(library).ToList(); if (library.IsValidPath()) { foreach (var candidate in candidates) { if (File.Exists(candidate)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidate))); } } } if (localFirst) { foreach (var candidate in candidates) { var result = LocalPathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } } foreach (var candidate in candidates) { var result = PathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } if (!(Type.GetType("Mono.Runtime") is null) && library == "__Internal") { // Mono extension: Search the main program return(ResolvePathResult.FromSuccess(null)); } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }
/// <summary> /// Resolves the absolute path to the given library. /// </summary> /// <param name="library">The name or path of the library to load.</param> /// <param name="localFirst">Whether or not the executable's local directory should be searched first.</param> /// <returns>The absolute path to the library.</returns> /// <exception cref="PlatformNotSupportedException"> /// Thrown if the current platform doesn't have a path /// resolver defined.</exception> /// <exception cref="FileNotFoundException">Thrown if no library file can be found.</exception> private ResolvePathResult ResolveAbsolutePath([NotNull] string library, bool localFirst) { var candidates = GenerateLibraryCandidates(library).ToList(); if (library.IsValidPath()) { foreach (var candidate in candidates) { if (File.Exists(candidate)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidate))); } } } // Check the native probing paths (.NET Core defines this, Mono doesn't. Users can set this at runtime, too) if (AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES") is string directories) { var paths = directories.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); foreach (var path in paths) { foreach (var candidate in candidates) { var candidatePath = Path.Combine(path, candidate); if (File.Exists(candidatePath)) { return(ResolvePathResult.FromSuccess(Path.GetFullPath(candidatePath))); } } } } if (localFirst) { foreach (var candidate in candidates) { var result = LocalPathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } } foreach (var candidate in candidates) { var result = PathResolver.Resolve(candidate); if (result.IsSuccess) { return(result); } } if (library == "__Internal") { // Mono extension: Search the main program. Allowed for all runtimes return(ResolvePathResult.FromSuccess(null)); } return(ResolvePathResult.FromError(new FileNotFoundException("The specified library was not found in any of the loader search paths.", library))); }