7

I was super excited to see that the latest previews of .NET Core 3.1 and Visual Studio 2019 add support for managed C++/CLI projects, as such a project is the only think keeping a particular project on .NET Framework.

So, I installed Visual Studio Preview 16.4.0 Preview 4, along with the "C++/CLI support for v142..." options, and as expected I see the new C++ CLR templates and have .NET Core 3.1 preview 2 installed

I created a new project using the "CLR Class Library (.NET Core)" template, copied the files an old managed C++/CLI project, tweaked a little, and the assembly built - great!

However, when I try to use the assembly in a .NET Core 3.1, I get this fatal exception:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'MyAssembly, Version=2019.0.1.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
File name: 'MyAssembly, Version=2019.0.1.0, Culture=neutral, PublicKeyToken=null'
   at TestApp.Program.Main(String[] args)

Both the managed assembly and test app target X64. Any ideas what could be the problem?

Cocowalla
  • 13,822
  • 6
  • 66
  • 112
  • Well, that's not what the exception says. Keep in mind that the solution platform name is irrelevant for managed builds. Verify assumptions by running Dumpbin.exe /headers on the DLLs, the Machine field tells you what you targeted. – Hans Passant Nov 08 '19 at 11:08
  • @HansPassant Sorry, what's not what the exception says? If you mean because I mentioned the target architecture, I did that because I knew someone would ask if I didn't :) – Cocowalla Nov 08 '19 at 11:41
  • @HansPassant `dumpbin /headers` contains this in the output `8664 machine (x64)` – Cocowalla Nov 08 '19 at 11:42

3 Answers3

10

Someone from Microsoft provided the solution over on the Github repo.

When the managed C++/CLI project is built, a file ijwhost.dll is placed in the output folder alongside the assembly - this file needs to be deployed with the app that uses the assembly.

After putting ijwhost.dll in the same folder as the app, it worked as expected.

As an aside, the old C++/CLI project that I built against .NET Core 3.1 preview is actually quite complex - I'm very pleasantly surprised that it basically "just worked"!

Hopefully a better error message will be used in future!

Cocowalla
  • 13,822
  • 6
  • 66
  • 112
  • 1
    Copying ijwhost.dll did not solve the problem for me, I still get a BadImageFormatException. – Medinoc Nov 27 '20 at 14:22
  • This solved "System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (0x8007000B)" for me, other answers across SO suggest a x86/x64 mismatch when that isn't the case. – Neo Feb 07 '22 at 11:34
1

I am using .net 5.0 as the CLI runtime. I finally found that the problem I have is missing native dependency DLLs.

For native applications, there will be an error prompt telling you which DLL is missing. While in .net core C++/CLI, they only give you a BadImageFormatException.

My solution is, create a pure native console project, paste the code that will cause BadImageFormatException, run it and see which DLL is missing then add it back to C++/CLI project file list.

I just found some unexpected dependencies.

kana
  • 11
  • 1
-1

In my case c# Net 5.0 app loads managed C++ dll, which is wrapping around unmanaged C++ dll.I get this error every time i try to run on on machine with no Visual Studio installed. I debugged it with ProcessMonitor and figure out that it can't found VCRUNTIME140D.dll. Found these dlls in my dev PC, copied them (both 32 and 64 bit versions) from my dev machine to customer's one to corresponded folders, and it made the trick. Hope will help somebody. Cheers.