I have to use an old DLL for my current software that causes a BadImageFormatException when called during runtime. I have read that this is a typical 32 bit / 64 bit mismatch problem, so I set the target platform to x86 and it worked. However, when switching back to Any CPU, I get the BadImageFormatException again.
I am confused - isn't the point of Any CPU that I should be able to load DLLs of either 32 bit or 64 bit type? Setting the target platform to x86 is not an option because other DLLs require x64. What do I have to do to get things working?
I am using Visual Studio 2022 and my project is targeted to .NET 6.0.
isn't the point of AnyCPU that I should be able to load DLLs of either 32 bit or 64 bit type?
No, the point is that DLL compiled with AnyCPU option can be run as 32 or 64-bit process depending on the environment. From the docs:
anycpu (default) compiles your assembly to run on any platform. Your application runs as a 64-bit process whenever possible and falls back to 32-bit when only that mode is available.