Skip to content

Commit 5299aac

Browse files
committed
loading: on non-windows use libdl to load libfdb_c
1 parent 69f0359 commit 5299aac

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

FoundationDB.Client/Native/UnmanagedLibrary.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,32 @@ protected override bool ReleaseHandle()
7272
[SuppressUnmanagedCodeSecurity]
7373
private static class NativeMethods
7474
{
75-
#if __MonoCS__
76-
const string KERNEL = "dl";
75+
const string LIBDL = "dl";
76+
7777

78-
[DllImport(KERNEL)]
78+
[DllImport(LIBDL)]
7979
public static extern SafeLibraryHandle dlopen(string fileName, int flags);
8080

81-
[DllImport(KERNEL, SetLastError = true)]
82-
[return: MarshalAs(UnmanagedType.Bool)]
83-
public static extern int dlclose(IntPtr hModule);
81+
#if __MonoCS__
8482

85-
public static SafeLibraryHandle LoadLibrary(string fileName)
83+
public static SafeLibraryHandle LoadPlatformLibrary(string fileName)
8684
{
87-
8885
return dlopen(fileName, 1);
89-
9086
}
91-
public static bool FreeLibrary(IntPtr hModule) { return dlclose(hModule) == 0; }
92-
9387
#else
9488
const string KERNEL = "kernel32";
9589

9690
[DllImport(KERNEL, CharSet = CharSet.Auto, BestFitMapping = false, SetLastError = true)]
9791
public static extern SafeLibraryHandle LoadLibrary(string fileName);
9892

99-
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
100-
[DllImport(KERNEL, SetLastError = true)]
101-
[return: MarshalAs(UnmanagedType.Bool)]
102-
public static extern bool FreeLibrary(IntPtr hModule);
93+
public static SafeLibraryHandle LoadPlatformLibrary(string fileName)
94+
{
95+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
96+
{
97+
return LoadLibrary(fileName);
98+
}
99+
return dlopen(fileName, 1);
100+
}
103101
#endif
104102
}
105103

@@ -112,7 +110,7 @@ public static UnmanagedLibrary Load(string path)
112110
{
113111
if (path == null) throw new ArgumentNullException("path");
114112

115-
var handle = NativeMethods.LoadLibrary(path);
113+
var handle = NativeMethods.LoadPlatformLibrary(path);
116114
if (handle == null || handle.IsInvalid)
117115
{
118116
var ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());

0 commit comments

Comments
 (0)