static IntPtr LoadLibrary(string filename) { if (PlatformDetection.IsWindows) { Win32.SetDllDirectory(Path.GetDirectoryName(filename)); var result = Win32.LoadLibrary(filename); if (result != IntPtr.Zero) { return(result); } else { throw new NativeException("Failed to load '" + filename + "'. " + new Win32Exception(Marshal.GetLastWin32Error()).Message); } } else if (PlatformDetection.IsMac) { var result = Mac.dlopen(filename, 9); if (result != IntPtr.Zero) { return(result); } } throw new NativeException("Failed to load '" + filename + "'"); }
static IntPtr LoadLibrary(string name, string[] searchPaths) { Exception e = null; name = PlatformDetection.IsWindows ? name + ".dll" : PlatformDetection.IsMac ? "lib" + name + ".dylib" : "lib" + name + ".so"; foreach (var dir in searchPaths) { var filename = Path.Combine(dir, name); if (File.Exists(filename)) { if (PlatformDetection.IsWindows) { Win32.SetDllDirectory(Path.GetDirectoryName(filename)); var result = Win32.LoadLibrary(filename); if (result != IntPtr.Zero) { return(result); } } else if (PlatformDetection.IsMac) { var result = Mac.dlopen(filename, 9); if (result != IntPtr.Zero) { return(result); } } e = new NativeException("Failed to load '" + filename + "'", e); } } if (e != null) { throw e; } var sb = new StringBuilder("'" + name + "' was not found in any of these locations:"); foreach (var dir in searchPaths) { sb.AppendLine(" * " + dir); } throw new NativeException(sb.ToString()); }