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

前端 未结 20 2656
悲&欢浪女
悲&欢浪女 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:25

    With Visual Studio 2019 I had a similar issue when I wanted to run tests (MSTest directly from VS). In my case I only had an x64 native DLL and I received this error message. First, I thought it is because Visual Studio runs as x86 but this page helped me to solve the issue:

    Run unit test as a 64-bit process

    It says

    1. Set your projects to Any CPU
    2. Explicitly define processor architecture

    I did both (I explicitly set x64) and then my tests started to work.

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

    Somehow, the Build checkbox in the Configuration Manager had been unchecked for my executable, so it was still running with the old Any CPU build. After I fixed that, Visual Studio complained that it couldn't debug the assembly, but that was fixed with a restart.

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

    In my case it was wrong content of the file. DLL was downloaded from the web, but content of the DLL was HTML page :D Try to check if it is binary file, if it seems like correct DLL :)

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

    I was able to fix this issue by matching my build version to the .NET version on the server.

    I double clicked the .exe just to see what would happen and it told me to install 4.5....

    So I downgraded to 4.0 and it worked!

    So make sure your versions match. It ran on my dev box fine, but server had older .NET version.

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

    In my case I was using a native DLL in C#. This DLL depended on couple of other DLLs that were missing. Once those other DLLs were added everything worked.

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

    If you are using Any CPU, you might encounter this issue if the Prefer 32-bit option is checked:

    Make sure you uncheck this option in the project's property's Build tab!

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