C# .NET核心-DllImport BadImageFormatException

C# .NET核心-DllImport BadImageFormatException,c#,exception,.net-core,C#,Exception,.net Core,我创建了一个引用x86 dll的项目。但当我调用导入的函数时,它会引发异常: System.BadImageFormatException:尝试加载程序 格式不正确 Program.cs: [DllImport("lib.dll", CallingConvention = CallingConvention.StdCall)] static extern int func(int a, int ab, int c, int d, int e); static void Main(string[

我创建了一个引用x86 dll的项目。但当我调用导入的函数时,它会引发异常:

System.BadImageFormatException:尝试加载程序 格式不正确

Program.cs:

[DllImport("lib.dll", CallingConvention = CallingConvention.StdCall)]
static extern int func(int a, int ab, int c, int d, int e);

static void Main(string[] args)
{
    func(0, 0, 0, 0, 0);
}
.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
  </PropertyGroup>
  <ItemGroup>
    <None Update="lib.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App" Version="2.0.0-preview2-25407-01" />
  </ItemGroup>
</Project>`

Exe
netcoreapp2.0
win-x86
保存最新
`
如果它以netcoreapp2.0为目标,它就不起作用,但当我将目标框架更改为“net45”时,它就起作用了(我需要netcoreapp2.0,因为它是多平台的)


有人知道哪里会有问题吗?

有没有可能——仅仅有可能——您的lib.dll是为.NET 4.5而不是.NET Core编译的?它是用C编写的本机x86 dll,由MSVC编译,所以我不这么认为。您是否确保这两个dll都是为x86编译的?因为要么是这样,要么你的dll不是dll。我使用“dumpbin/headers lib.dll”来分析dll,它显示“14C machine(x86)”,在csproj中我将“RuntimeIdentifier”设置为win-x86,所以我认为它都是x86,但我如何验证.Net Core应用程序是否真的在x86上运行?这不是dll windows的概念?本机dll如何与跨平台的.NET内核一起工作?