【问题标题】:C# and PInvoke into 32/64 bit DLL [duplicate]C# 和 PInvoke 到 32/64 位 DLL [重复]
【发布时间】:2015-05-08 03:59:25
【问题描述】:

我正在创建一个类库,我希望最终将它放在 NuGet 上。目前,它的目标是 AnyCPU。

我现在想 PInvoke 到一个 DLL,它有 32 位和 64 位版本。最初,我以为我只使用 32 位版本,但这引发了 BadImageFormatException。我将库更改为仅针对 X86,虽然这有效,但它也要求调用者是 32 位进程。这自然不适用于 NuGet 项目。

感谢您对如何使用 32/64 位版本的本机库以及如何将其打包到 NuGet 中的任何想法(最好不要有 2 个不同的程序集)。

【问题讨论】:

  • 看看SQLite是怎么做的。

标签: c# nuget pinvoke


【解决方案1】:

您可以在运行时检查平台并 PInvoke 到不同的 DLL。

static void NativeFuncWrapper(){
    if(Environment.Is64BitProcess){
        NativeFuncWrapper64(); //this calls 64-bit dll
    }else{
        NativeFuncWrapper32(); //this calls 32-bit dll
    }
}

如果您希望它在没有Environment.Is64BitProcess 的情况下工作,请阅读How to know a process is 32-bit or 64-bit programmatically 了解替代方法。

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多