繁体   English   中英

在 64 位系统上与 gcc 构建的 DLL 互操作上出现 BadImageFormatException

[英]BadImageFormatException on interop from DLL build with gcc on 64-bit system

我正在开发一个 .NET 库。 作为使库跨平台的任务的一部分,我决定将所有特定于平台的函数放入 C 文件并构建 DLL(适用于 Windows)和 dylib(适用于 macOS)。 为了开始这个任务,我创建了一个简单的测试应用程序:Windows 上的控制台应用程序在 .NET 框架上,它从生成的 Z50584E40D2716 调用一个简单的 function。

C 文件的内容 DLL (test.dll) 构建自:

int Foo() { return 123; }

测试程序代码:

class Program
{
    [DllImport("test")]
    public static extern int Foo();

    static void Main(string[] args)
    {
        var result = Foo();
        Console.WriteLine($"Result = {result}");
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
}

当我运行程序时,我得到BadImageFormatException

如果我 go 在 Visual Studio 中的项目设置,我看到:

在此处输入图像描述

如果我在下拉列表中取消选中“首选 32 位”或 select x64,一切都很好,我看到正确的 output:

Result = 123
Press any key to exit...

但这对我来说不是解决方案。 该库被许多用户使用,我不想(也不能)强迫他们进行一些环境设置,比如选择目标平台。 它也应该适用于 x86 和 x64。

DLL 是用 gcc 构建的:

gcc -v -c test.c
gcc -v -shared -o test.dll test.o

我的问题是如何处理问题并创建可以在任何 .NET 应用程序中定位的 DLL,而无需采取初步行动? 也许可以构建具有双平台目标(不仅是 x64)的 DLL?

我最终构建了两个版本的 DLL,32 位和 64 位。 所以在代码中我们有这样的类:

Api.cs

namespace DualLibClassLibrary
{
    internal abstract class Api
    {
        public abstract int Method();
    }
}

api32.cs

using System.Runtime.InteropServices;

namespace DualLibClassLibrary
{
    internal sealed class Api32 : Api
    {
        [DllImport("test32")]
        public static extern int Foo();

        public override int Method()
        {
            return Foo();
        }
    }
}

api64.cs

using System.Runtime.InteropServices;

namespace DualLibClassLibrary
{
    internal sealed class Api64 : Api
    {
        [DllImport("test64")]
        public static extern int Foo();

        public override int Method()
        {
            return Foo();
        }
    }
}

ApiProvider.cs

using System;

namespace DualLibClassLibrary
{
    internal static class ApiProvider
    {
        private static readonly bool Is64Bit = IntPtr.Size == 8;
        private static Api _api;

        public static Api Api
        {
            get
            {
                if (_api == null)
                    _api = Is64Bit ? (Api)new Api64() : new Api32();

                return _api;
            }
        }
    }
}

因此,如果我们要调用Method方法,我们将编写:

var result = ApiProvider.Api.Method();

当然,我们需要在 output 中有以下文件:

  • test32.dll(还有dylib等)
  • test64.dll(还有dylib等)

使用此系统,程序将 select 所需文件取决于当前进程是否为 64 位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM