在 .NetCore 2.1 中加载本机库 (Windows)
load native libraries in .NetCore 2.1 (Windows)
我正在尝试像这样在 .NetCore 2.1 中加载本机库:
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern bool SetDllDirectoryA(string lpPathName);
...
SetDllDirectoryA(pathToDll);
var pDll = LoadLibrary(pathToDll+dllName);
if (pDll == IntPtr.Zero)
{
throw new System.ArgumentException("DLL not found", "pDll");
}
但函数 LoadLibrary returns 始终为零。 此代码适用于 .NET Framework。
我不确定 .NetCore 是否支持加载本机库。如果可能的话,正确的做法是什么?
我认为您使用的是 32 位 DLL。在 netcore 中,无法使用 64 位进程加载 32 位 DLL。
试试这个代码来检查:
class Program
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
static void Main(string[] args)
{
if (System.Environment.Is64BitProcess)
{
Console.WriteLine("This is 64 bit process");
}
var pDll = LoadLibrary("aDLL.dll");
if (pDll == IntPtr.Zero)
{
Console.WriteLine("pDll: " + pDll);
throw new System.ArgumentException("DLL not found", "pDll");
}
Console.WriteLine("pDll: " + pDll);
}
}
更新:如果你想强制 NetCore 在 x86 平台上运行(使用 32 位 DLL)。首先从 https://dotnet.microsoft.com/download/thank-you/dotnet-sdk-2.1.500-windows-x86-installer 下载 NetCore x86。然后你应该通过添加 RunCommand
并将 PlatformTarget
更改为 x86 来编辑 .CSPROJ
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>x86</PlatformTarget>
<Optimize>false</Optimize>
<RunCommand Condition="'$(PlatformTarget)' == 'x86'">$(MSBuildProgramFiles32)\dotnet\dotnet</RunCommand>
<RunCommand Condition="'$(PlatformTarget)' == 'x64'">$(ProgramW6432)\dotnet\dotnet</RunCommand>
</PropertyGroup>
</Project>
我正在尝试像这样在 .NetCore 2.1 中加载本机库:
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern bool SetDllDirectoryA(string lpPathName);
...
SetDllDirectoryA(pathToDll);
var pDll = LoadLibrary(pathToDll+dllName);
if (pDll == IntPtr.Zero)
{
throw new System.ArgumentException("DLL not found", "pDll");
}
但函数 LoadLibrary returns 始终为零。 此代码适用于 .NET Framework。
我不确定 .NetCore 是否支持加载本机库。如果可能的话,正确的做法是什么?
我认为您使用的是 32 位 DLL。在 netcore 中,无法使用 64 位进程加载 32 位 DLL。 试试这个代码来检查:
class Program
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
static void Main(string[] args)
{
if (System.Environment.Is64BitProcess)
{
Console.WriteLine("This is 64 bit process");
}
var pDll = LoadLibrary("aDLL.dll");
if (pDll == IntPtr.Zero)
{
Console.WriteLine("pDll: " + pDll);
throw new System.ArgumentException("DLL not found", "pDll");
}
Console.WriteLine("pDll: " + pDll);
}
}
更新:如果你想强制 NetCore 在 x86 平台上运行(使用 32 位 DLL)。首先从 https://dotnet.microsoft.com/download/thank-you/dotnet-sdk-2.1.500-windows-x86-installer 下载 NetCore x86。然后你应该通过添加 RunCommand
并将 PlatformTarget
更改为 x86 来编辑 .CSPROJ
文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>x86</PlatformTarget>
<Optimize>false</Optimize>
<RunCommand Condition="'$(PlatformTarget)' == 'x86'">$(MSBuildProgramFiles32)\dotnet\dotnet</RunCommand>
<RunCommand Condition="'$(PlatformTarget)' == 'x64'">$(ProgramW6432)\dotnet\dotnet</RunCommand>
</PropertyGroup>
</Project>