Browse Source

Merge pull request #298 from kjbartel/NativeProviderLoaderFixes

Native provider loader fixes
pull/297/head
Christoph Ruegg 11 years ago
parent
commit
fa3dae304f
  1. 14
      MathNet.Numerics.NativeProviders.sln
  2. 25
      src/Numerics/Providers/NativeProviderLoader.cs
  3. 52
      src/UnitTests/UnitTests-MKL.csproj

14
MathNet.Numerics.NativeProviders.sln

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{5A0892FF-82CE-40FC-BCE1-73810C615F52}"
ProjectSection(SolutionItems) = preProject
@ -97,18 +97,14 @@ Global
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Win32.ActiveCfg = Debug|x86
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Win32.Build.0 = Debug|x86
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|x64.ActiveCfg = Debug|x64
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|x64.Build.0 = Debug|x64
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Win32.ActiveCfg = Debug|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|x64.ActiveCfg = Debug|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Any CPU.Build.0 = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Win32.ActiveCfg = Release|x86
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Win32.Build.0 = Release|x86
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|x64.ActiveCfg = Release|x64
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|x64.Build.0 = Release|x64
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Win32.ActiveCfg = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|x64.ActiveCfg = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Any CPU.ActiveCfg = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Any CPU.Build.0 = Release|Any CPU
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Mixed Platforms.ActiveCfg = Release|Any CPU

25
src/Numerics/Providers/NativeProviderLoader.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.Providers
() => new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{"x86", "x86"},
{"AMD64", "amd64"},
{"AMD64", "x64"},
{"IA64", "ia64"},
{"ARM", "arm"}
},
@ -115,6 +115,7 @@ namespace MathNet.Numerics.Providers
public static Exception LastException { get; private set; }
private static object staticLock = new Object();
/// <summary>
/// Load the native library with the given filename.
/// </summary>
@ -125,15 +126,21 @@ namespace MathNet.Numerics.Providers
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
lock(staticLock)
lock (staticLock)
{
IntPtr libraryHandle = IntPtr.Zero;
if (NativeHandles.TryGetValue(fileName, out libraryHandle))
return true;
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ArchDirectory, fileName);
if (string.IsNullOrEmpty(ArchDirectory))
return false;
// Look for the library in a acrhtecture directory under the current AppDomain's base directory or this assembly's directory
string path = Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory), ArchDirectory, fileName);
if (!File.Exists(path))
path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ArchDirectory, fileName);
if (string.IsNullOrEmpty(ArchDirectory) || !File.Exists(path))
if (!File.Exists(path))
{
// If the library isn't found within an architecture specific folder then return false
// to allow normal P/Invoke searching behaviour when the library is called
@ -162,8 +169,16 @@ namespace MathNet.Numerics.Providers
[SecurityCritical]
private static class WindowsLoader
{
public static IntPtr LoadLibrary(string fileName)
{
return LoadLibraryEx(fileName, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
}
// Search for dependencies in the library's directory rather than the calling process's directory
const uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
[DllImport("kernel32", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr LoadLibrary(string fileName);
private static extern IntPtr LoadLibraryEx(string fileName, IntPtr reservedNull, uint flags);
}
[SuppressUnmanagedCodeSecurity]

52
src/UnitTests/UnitTests-MKL.csproj

@ -17,7 +17,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DefineConstants>TRACE;NATIVE</DefineConstants>
<OutputPath>..\..\out\MKL\Windows\AnyCPU\</OutputPath>
<OutputPath>..\..\out\MKL\Windows\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x86\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x86\</BaseIntermediateOutputPath>
<Optimize>true</Optimize>
@ -29,7 +29,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>TRACE;DEBUG;NATIVE</DefineConstants>
<OutputPath>..\..\out\MKL\Windows\AnyCPU\</OutputPath>
<OutputPath>..\..\out\MKL\Windows\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x86\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x86\</BaseIntermediateOutputPath>
<Optimize>false</Optimize>
@ -40,54 +40,6 @@
<NoWarn>1591</NoWarn>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>..\..\out\MKL\Windows\x86\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x86\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x86\</BaseIntermediateOutputPath>
<DefineConstants>TRACE;NATIVE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>1591</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\out\MKL\Windows\x86\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x86\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x86\</BaseIntermediateOutputPath>
<DefineConstants>TRACE;DEBUG;NATIVE</DefineConstants>
<NoWarn>1591</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>..\..\out\MKL\Windows\x64\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x64\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x64\</BaseIntermediateOutputPath>
<DefineConstants>TRACE;NATIVE</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>1591</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\..\out\MKL\Windows\x64\</OutputPath>
<IntermediateOutputPath>..\..\obj\MKL\Windows\x64\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\MKL\Windows\x64\</BaseIntermediateOutputPath>
<DefineConstants>TRACE;DEBUG;NATIVE</DefineConstants>
<NoWarn>1591</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System" />

Loading…
Cancel
Save