BadImageFormatException When Calling .dll Built from C++ Project In NET Core

∥☆過路亽.° 提交于 2019-12-24 19:14: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:


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



来源:https://stackoverflow.com/questions/46820197/badimageformatexception-when-calling-dll-built-from-c-project-in-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!