ThurmanAllen-0353 avatar image
0 Votes"
ThurmanAllen-0353 asked RobCaplan edited

BadImageFormatException building C++ .dll in Server 2012 R2

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?

1. Both machines have Visual Studio 2019 used to build

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm using the same build process that I use on my Win 10 laptop (x64). I have also tried it building using MsBuild from the command line (where you specifically denote x64). Since it builds and runs fine on my laptop it does NOT seem to be the build process, but maybe the build ENVIRONMENT. Something is different in the Server 2012 environment

0 Votes 0 ·

The target platform is specified as a parameter to the MSBuild tool. The build environment doesn't matter for .NET as target platform is a parameter to the tools.

Please clarify the following:
1) What is the bitness of the process that will be attempting to load the DLL you generated?
2) What is the command line you are using to build the DLL in your C++ app?
3) Open the generated DLL in ILDASM or Just Decompile or similar. What bitness does it show?

0 Votes 0 ·
  1. 64 bit

  2. Msbuild /t:Build /p:Configuration=Debug;Platform=x64; (also tried building directly in visual studio --> devenv)

  3. Corflags output:

Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.8.3928.0
Copyright (c) Microsoft Corporation. All rights reserved.

Version : v4.0.30319
CLR Header: 2.5
PE : PE32+
CorFlags : 0x10
32BITREQ : 0
Signed : 0

0 Votes 0 ·
Show more comments