“An attempt was made to load a program with an incorrect format” even when the platforms are the same

前端 未结 20 2610
悲&欢浪女
悲&欢浪女 2020-11-22 13:57

I\'m calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is:

BadImageFormatException: An attempt was made to load a program w

相关标签:
20条回答
  • 2020-11-22 14:34

    We had a similar issue and we managed to fix it by setting the Platform target to x86.

    0 讨论(0)
  • 2020-11-22 14:35

    If you encounter this error when you click green arrow button to run the application, but still want to run the app in 64 bit. You can do this in VS 2013, 2015, 2017, and 2019

    Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express

    0 讨论(0)
  • 2020-11-22 14:36

    We were having the same issue in .NET core. The solution was to download 32-bit .netcore runtime, and having your project target x86

    In your csproj file add

      <PropertyGroup>
        <PlatformTarget>x86</PlatformTarget>  
      </PropertyGroup>
    
      <PropertyGroup>
        <RunCommand Condition="'$(PlatformTarget)' == 'x86'">$(MSBuildProgramFiles32)\dotnet\dotnet</RunCommand>    
      </PropertyGroup>
    

    This was used for a Windows machine, you'd have to adjust paths and such for Linux/OSX

    0 讨论(0)
  • 2020-11-22 14:37

    If you are importing unmanaged DLL then use

    CallingConvention = CallingConvention.Cdecl 
    

    in your DLL import method.

    0 讨论(0)
  • 2020-11-22 14:38

    In Visual Studio, Right Click your project -> On the left pane click the Build tab,

    Project properties, build tab

    under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)

    Project properties, platform target

    I hope this helps someone! :)

    0 讨论(0)
  • 2020-11-22 14:38

    In my case, I was running tests through MSTest and found out that I was deploying both a 32-bit and 64-bit DLL to the test directory. The program was favoring the 64-bit DLL and causing it to fail.

    TL;DR Make sure you only deploy 32-bit DLLs to tests.

    0 讨论(0)
提交回复
热议问题