0

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.

Thern
  • 1,017
  • 8
  • 17
  • If you are using native libraries then a) they **all** should be either x86 or x64 b) and so your .net assembly – Selvin Apr 20 '23 at 19:41
  • Well, seems like one more argument for me to finally kick out that legacy code and rewrite it in a modern fashion. – Thern Apr 20 '23 at 20:00

1 Answers1

2

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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Ah, ok, I see my point of error. In this case, there is really not much I can do except getting my hands on that old library and modify it to be compatible with x64 architecture. – Thern Apr 20 '23 at 20:08