从.NET Core中的C ++项目调用.dll时调用BadImageFormatException

时间:2017-10-18 22:27:25

标签: c# c++ .net .net-core interop

我有两个预先存在的项目(C ++,C#NET 4.5.2),其中C#项目调用C ++项目。这非常有效,直到我将.NET 4.5.2项目移植到NET Core。现在我在尝试访问从C ++项目构建的程序集时遇到BadImageFormatException。是否可以从NET Core程序集调用此程序集?

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 个答案:

答案 0 :(得分:2)

请参阅此处https://msdn.microsoft.com/en-us/library/k7137bfe.aspx

检查dll是否专门用于除您正在运行的体系结构之外的体系结构。检查你编写的C ++ dll是什么

即。不要在32位应用程序中使用64位dll,反之亦然

编辑:我也看到你把它标记为ASP.NET。如果您在IIS中运行此操作并遇到此问题,则可以尝试更改分配给项目的应用池的高级设置中的启用32位应用选项

相关问题