BadImageFormatException When Calling .dll Built fr

2019-08-22 08:41发布

I have two preexisting projects (C++, C# NET 4.5.2) where the C# projects calls into the C++ project. This worked perfectly until I ported the NET 4.5.2 project to NET Core. Now I get a BadImageFormatException when trying to access the assembly built from the C++ project. Is it possible to call this assembly from a NET Core assembly?

public static string CallCPlusPlusConvert(string inputFileName)
{
    if (inputFileName == null) { throw new ArgumentNullException(nameof(inputFileName)); };

    return SafeNativeMethods.Convert(inputFileName);
}

internal class SafeNativeMethods
{
    [return: MarshalAs(UnmanagedType.BStr)]
    [DllImport("CPlusPlusProject", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    internal static extern string Convert([MarshalAs(UnmanagedType.BStr)][In]string filePath);
}

1条回答
forever°为你锁心
2楼-- · 2019-08-22 09:37

See here https://msdn.microsoft.com/en-us/library/k7137bfe.aspx

Check that the dll isn't specifically for an architecture other than what you're running as. Check what you complied the C++ dll as

i.e. Don't use a 64-bit dll in a 32-bit application and vice versa

EDIT: I also see that you tagged this as ASP.NET. If you're running this in IIS and get this issue then you can try to change the Enable 32-bit Applications option in the Advanced settings for the app pool assigned to the project

查看更多
登录 后发表回答