From abafe3b6b6fce1dbf562152aac1e0316fae8300a Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 21 Dec 2021 08:45:23 +0100 Subject: [PATCH] Providers: on .NET 5.0 and newer leverage NativeLibrary from System.Runtime.interopServices --- src/Providers.MKL/NativeProviderLoader.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Providers.MKL/NativeProviderLoader.cs b/src/Providers.MKL/NativeProviderLoader.cs index 873510e8..5f1d7d6f 100644 --- a/src/Providers.MKL/NativeProviderLoader.cs +++ b/src/Providers.MKL/NativeProviderLoader.cs @@ -127,13 +127,13 @@ namespace MathNet.Numerics.Providers.Common } // If we have hint path provided by the user, look there first - if (TryLoadFromDirectory(fileName, hintPath)) + if (hintPath != null && TryLoadFromDirectory(fileName, hintPath)) { return true; } // If we have an overall hint path provided by the user, look there next - if (Control.NativeProviderPath != hintPath && TryLoadFromDirectory(fileName, Control.NativeProviderPath)) + if (Control.NativeProviderPath != null && Control.NativeProviderPath != hintPath && TryLoadFromDirectory(fileName, Control.NativeProviderPath)) { return true; } @@ -252,6 +252,9 @@ namespace MathNet.Numerics.Providers.Common return false; } +#if NET5_0_OR_GREATER + return NativeLibrary.TryLoad(fullPath, out libraryHandle); +#else // If successful this will return a handle to the library libraryHandle = IsUnix ? UnixLoader.LoadLibrary(fullPath) : WindowsLoader.LoadLibrary(fullPath); if (libraryHandle == IntPtr.Zero) @@ -267,9 +270,11 @@ namespace MathNet.Numerics.Providers.Common } return libraryHandle != IntPtr.Zero; +#endif } } +#if !NET5_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [SecurityCritical] static class WindowsLoader @@ -297,8 +302,9 @@ namespace MathNet.Numerics.Providers.Common const int RTLD_NOW = 2; - [DllImport("libdl.so", SetLastError = true)] + [DllImport("libdl", SetLastError = true)] static extern IntPtr dlopen(String fileName, int flags); } +#endif } }