NET Core中调用从C ++项目构建的.dll时出现BadImageFormatException [英] BadImageFormatException When Calling .dll Built from C++ Project In NET Core
问题描述
我有两个预先存在的项目(C ++,C#NET 4.5.2),其中C#项目调用了C ++项目。在我将NET 4.5.2项目移植到NET Core之前,这一直很好。现在,当我尝试访问从C ++项目构建的程序集时,出现BadImageFormatException。可以从NET Core程序集中调用该程序集吗?
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);
}
推荐答案
请参见此处 https://msdn.microsoft.com/en-us/library/k7137bfe.aspx
检查dll是否不是专门针对您所运行的体系结构而设计的。检查您编译C ++ dll的格式为
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
即不要在32位应用程序中使用64位dll,反之亦然
i.e. Don't use a 64-bit dll in a 32-bit application and vice versa
编辑:我还看到您将此标签为ASP.NET。如果您正在IIS中运行此程序并遇到此问题,则可以尝试更改分配给项目的应用程序池的高级设置中的启用32位应用程序选项
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
这篇关于NET Core中调用从C ++项目构建的.dll时出现BadImageFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!