Skip to content

BadImageFormatException when loading C++/CLI DLLs in Core 3.1 #31743

@Farengeto

Description

@Farengeto

Operating System Version: Azure virtual machine with a Windows 10 Pro, Version 1809 base
.NET Core Version: 3.1
Visual Studio Version: 16.4.3 and 16.4.4

Whenever I attempt to access a C++/CLI project (targeted for .NET Core 3.1) from a C# project, the execution will immediately crash when attempting to load the assembly.

I have managed to reproduce this error in multiple independent solutions, on multiple computers, so it does not appear to be tied to a code- or computer-specific issue on my end.

If the project is executed in Visual Studio with the debugger, the debugger itself appears crash upon reaching the method which calls the C++/CLI DLL. No information will be provided in Debug other than these lines:

The program '[13640] NET Core CLI Test.exe: Program Trace' has exited with code 0 (0x0).
The program '[13640] NET Core CLI Test.exe' has exited with code -529697949 (0xe06d7363) 'Microsoft C++ Exception'.

As a curious note, the debugger also skips over the calling method if I attempt to step into it, reporting it as "non-user code".

If executed without a debugger, more information is provided when the crash occurs:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'Mock CLI Project, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.
File name: 'Mock CLI Project, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
at NET_Core_CLI_Test.Program.CallingMethod(Int32 a, Int32 b)
at NET_Core_CLI_Test.Program.Main(String[] args) in C:\Users\travis.ridge\source\repos\NET Core CLI Test\NET Core CLI Test\Program.cs:line 11

In both situations, an error will be added to the Windows event log, displaying the same basic information each time.

Faulting application name: NET Core CLI Test.exe, version: 1.0.0.0, time stamp: 0x5dedc13b
Faulting module name: KERNELBASE.dll, version: 10.0.17763.914, time stamp: 0xfb6790ac
Exception code: 0xe0434352
Fault offset: 0x0000000000039159
Faulting process id: 0x31e0
Faulting application start time: 0x01d5d8549d80476e
Faulting application path: C:\Users\travis.ridge\source\repos\NET Core CLI Test\NET Core CLI Test\bin\x64\Debug\netcoreapp3.1\NET Core CLI Test.exe
Faulting module path: C:\windows\System32\KERNELBASE.dll
Report Id: 575a2b9a-1f69-4d48-8d07-ac8dfe6917d4
Faulting package full name:
Faulting package-relative application ID:

Below I've included the code example from a minimal solution in Visual Studio with which I am able to consistently reproduce this issue.

Example C# script. The program crashes when CallingMethod() is invoked.

using CLI;

namespace NET_Core_CLI_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2;
            int b = 2;
            int sum = CallingMethod(a, b); //Crash occurs when executing this line
        }

        static int CallingMethod(int a, int b)
        {
            //Execution never reaches here
            MockWrapper wrapper = new MockWrapper();
            return wrapper.Add(a, b);
        }
    }
}

The ModelWrapper.cpp class:

#pragma once
#include "pch.h"
#include "MockWrapper.h"

#include <exception>

using namespace System;
using namespace System::Runtime::InteropServices;

namespace CLI {
	MockWrapper::MockWrapper() {

	}

	int MockWrapper::Add(int a, int b) {
		return a + b;
	}
}

I can provide further information upon request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-AssemblyLoader-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions