/// <summary> /// When overridden in a derived class, executes the code required to free the handle. /// </summary> /// <returns><see langword="true"/> if the handle is released successfully; otherwise, in the event of a catastrophic failure, <see langword="false"/>.</returns> protected override bool ReleaseHandle() { bool released; try { if (handle == IntPtr.Zero) { throw new InvalidHandleException(); } switch (PlatformHelper.CurrentPlatform) { case PlatformHelper.Platform.Windows: Kernel32.FreeLibrary(handle); break; case PlatformHelper.Platform.Linux: case PlatformHelper.Platform.MacOS: case PlatformHelper.Platform.FreeBSD: Libdl.dlclose(handle); break; default: break; } handle = IntPtr.Zero; released = true; } catch { released = false; } return(released); }
public IntPtr Resolve(string entryPoint) { #if !MONO return(Win32.GetProcAddress(_p, entryPoint)); #else return(Libdl.dlsym(_p, entryPoint)); #endif }
private void Free() { if (_p != IntPtr.Zero) { #if !MONO Win32.FreeLibrary(_p); #else Libdl.dlclose(_p); #endif _p = IntPtr.Zero; } }
public DynamicLibraryImportResolver(string dllName) { #if !MONO _p = Win32.LoadLibrary(dllName); #else // TODO: how can we read name remaps out of app.confg <dllmap> ? _p = Libdl.dlopen(dllName, Libdl.RTLD_NOW); #endif if (_p == IntPtr.Zero) { throw new InvalidOperationException("LoadLibrary returned NULL"); } }
public static void FreeDL(IntPtr DLHandle) { switch (SystemType) { case System.Linux: Libdl.DLclose(DLHandle); break; case System.Windows: Kernel32.FreeLibrary(DLHandle); break; } }
/// <summary> /// Loads a function pointer with the given name. /// </summary> /// <param name="name">The name of the native function.</param> /// <returns>A function pointer for the given name, or <see cref="IntPtr.Zero"/> if no function with the specified name was found.</returns> public IntPtr LoadFunction(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException(nameof(name)); } switch (PlatformHelper.CurrentPlatform) { case PlatformHelper.Platform.Windows: return(Kernel32.GetProcAddress(Handle, name)); case PlatformHelper.Platform.Linux: case PlatformHelper.Platform.MacOS: case PlatformHelper.Platform.FreeBSD: return(Libdl.dlsym(Handle, name)); default: return(IntPtr.Zero); } }
private static IntPtr GetHandle(NativeAssemblyResolver resolver, params string[] names) { if (names == null || names.Length == 0) { throw new ArgumentNullException(nameof(names)); } IntPtr value = IntPtr.Zero; foreach (string name in names) { if (Path.IsPathRooted(name)) { switch (PlatformHelper.CurrentPlatform) { case PlatformHelper.Platform.Windows: value = Kernel32.LoadLibrary(name); break; case PlatformHelper.Platform.Linux: case PlatformHelper.Platform.MacOS: case PlatformHelper.Platform.FreeBSD: value = Libdl.dlopen(name, 0x002); break; default: break; } } else { foreach (string loadTarget in resolver.EnumerateLoadTargets(name)) { if (!Path.IsPathRooted(loadTarget) || File.Exists(loadTarget)) { IntPtr v = IntPtr.Zero; switch (PlatformHelper.CurrentPlatform) { case PlatformHelper.Platform.Windows: v = Kernel32.LoadLibrary(loadTarget); break; case PlatformHelper.Platform.Linux: case PlatformHelper.Platform.MacOS: case PlatformHelper.Platform.FreeBSD: v = Libdl.dlopen(loadTarget, 0x002); break; default: break; } if (v != IntPtr.Zero) { value = v; } } } } if (value != IntPtr.Zero) { break; } } if (value == IntPtr.Zero) { throw new FileNotFoundException($"Could not load a library with the specified name(s): [ {string.Join(", ", names)} ]"); } return(value); }