BadImageFormatException building C++ .dll in Server 2012 R2

Thurman, Allen 96 Reputation points
2021-04-01T13:32:59.713+00:00

I have my own solution with 2 projects building in Visual Studio 2019. One is a C++ project and produces a C# wrapper .dll (mytest.dll). The other project is a C# test app that makes calls to the wrapper .dll. When running the test app on my Win 10 laptop, I can run without any problem

Testing on my build machine, which is Windows Server 2012 R2, I do this same process but trying to run my test app I get a runtime exception:

System.BadImageFormatException: 'Could not load file or assembly 'mytest.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)'

I'm guessing there's some SDK or something for C++ that I need to install. Hard to tell because I have a million SDKs and redists installed on my laptop. Or maybe a VS2019 setting?

Any ideas on what to check on the Windows Server machine?

Note:

  1. Both machines have Visual Studio 2019 used to build
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,826 Reputation points
    2021-04-01T14:43:25.607+00:00

    BadImageFormat doesn't indicate a missing dependency but a binary format issue. You are trying to load an x86 DLL in an x64 process or vice versa. That means that your process for building the DLL is incorrect. If you need to host the generated DLL in an x86 process then you need to ensure you build the DLL as either Any CPU or x86. For x64 processes use Any CPU or x64. For a true .NET DLL then the recommendation is to use Any CPU so it works with either process.