在 NET Core 中调用从 C++ 项目构建的 .dll 时出现 BadImageFormatException [英] BadImageFormatException When Calling .dll Built from C++ Project In NET Core

查看:31
本文介绍了在 NET Core 中调用从 C++ 项目构建的 .dll 时出现 BadImageFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个预先存在的项目(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆