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
We had a similar issue and we managed to fix it by setting the Platform target to x86.
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
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
If you are importing unmanaged DLL then use
CallingConvention = CallingConvention.Cdecl
in your DLL import method.
In Visual Studio, Right Click your project -> On the left pane click the Build tab,
under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)
I hope this helps someone! :)
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.