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
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
I did both (I explicitly set x64) and then my tests started to work.
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.
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 :)
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.
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.
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!