◐ Shell
clean mode source ↗

Try libdl.so.2 on Linux before libdl.so by lostmsu · Pull Request #1425 · pythonnet/pythonnet

Expand Up @@ -28,9 +28,9 @@ public static ILibraryLoader Instance if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) _instance = new WindowsLoader(); else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) _instance = new LinuxLoader(); _instance = new PosixLoader(LinuxLibDL.GetInstance()); else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) _instance = new DarwinLoader(); _instance = new PosixLoader(new MacLibDL()); else throw new PlatformNotSupportedException( "This operating system is not supported" Expand All @@ -42,87 +42,19 @@ public static ILibraryLoader Instance } }
class LinuxLoader : ILibraryLoader class PosixLoader : ILibraryLoader { private static int RTLD_NOW = 0x2; private static int RTLD_GLOBAL = 0x100; private static IntPtr RTLD_DEFAULT = IntPtr.Zero; private const string NativeDll = "libdl.so"; private readonly ILibDL libDL;
public IntPtr Load(string dllToLoad) { ClearError(); var res = dlopen(dllToLoad, RTLD_NOW | RTLD_GLOBAL); if (res == IntPtr.Zero) { var err = GetError(); throw new DllNotFoundException($"Could not load {dllToLoad} with flags RTLD_NOW | RTLD_GLOBAL: {err}"); }
return res; }
public void Free(IntPtr handle) { dlclose(handle); }
public IntPtr GetFunction(IntPtr dllHandle, string name) { // look in the exe if dllHandle is NULL if (dllHandle == IntPtr.Zero) { dllHandle = RTLD_DEFAULT; }
ClearError(); IntPtr res = dlsym(dllHandle, name); if (res == IntPtr.Zero) { var err = GetError(); throw new MissingMethodException($"Failed to load symbol {name}: {err}"); } return res; }
void ClearError() public PosixLoader(ILibDL libDL) { dlerror(); }
string GetError() { var res = dlerror(); if (res != IntPtr.Zero) return Marshal.PtrToStringAnsi(res); else return null; this.libDL = libDL ?? throw new ArgumentNullException(nameof(libDL)); }
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public static extern IntPtr dlopen(string fileName, int flags);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] private static extern int dlclose(IntPtr handle);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr dlerror(); }
class DarwinLoader : ILibraryLoader { private static int RTLD_NOW = 0x2; private static int RTLD_GLOBAL = 0x8; private const string NativeDll = "/usr/lib/libSystem.dylib"; private static IntPtr RTLD_DEFAULT = new IntPtr(-2);
public IntPtr Load(string dllToLoad) { ClearError(); var res = dlopen(dllToLoad, RTLD_NOW | RTLD_GLOBAL); var res = libDL.dlopen(dllToLoad, libDL.RTLD_NOW | libDL.RTLD_GLOBAL); if (res == IntPtr.Zero) { var err = GetError(); Expand All @@ -134,19 +66,19 @@ public IntPtr Load(string dllToLoad)
public void Free(IntPtr handle) { dlclose(handle); libDL.dlclose(handle); }
public IntPtr GetFunction(IntPtr dllHandle, string name) { // look in the exe if dllHandle is NULL if (dllHandle == IntPtr.Zero) { dllHandle = RTLD_DEFAULT; dllHandle = libDL.RTLD_DEFAULT; }
ClearError(); IntPtr res = dlsym(dllHandle, name); IntPtr res = libDL.dlsym(dllHandle, name); if (res == IntPtr.Zero) { var err = GetError(); Expand All @@ -157,29 +89,17 @@ public IntPtr GetFunction(IntPtr dllHandle, string name)
void ClearError() { dlerror(); libDL.dlerror(); }
string GetError() { var res = dlerror(); var res = libDL.dlerror(); if (res != IntPtr.Zero) return Marshal.PtrToStringAnsi(res); else return null; }
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] public static extern IntPtr dlopen(String fileName, int flags);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private static extern IntPtr dlsym(IntPtr handle, String symbol);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] private static extern int dlclose(IntPtr handle);
[DllImport(NativeDll, CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr dlerror(); }
class WindowsLoader : ILibraryLoader Expand Down