【问题标题】:.NET AnyCPU project links platform specific library [duplicate].NET AnyCPU 项目链接平台特定库 [重复]
【发布时间】:2012-07-22 17:00:24
【问题描述】:

可能重复:
Loading x86 or x64 assembly

我正在尝试编译任何 CPU .NET 项目,但我必须链接 SQLite 库,该库具有不同版本的 x86 和 x64 平台。仅将 DLL 版本更改为 x64 没有帮助,应用程序无法启动,我需要使用 x64 参考重新编译代码。 当我同时添加 x86 和 x64 引用时,由于冲突,它无法编译。 我无法使用 x86 编译应用程序,因为我使用的系统 COM 库之一在 WOW64 下不起作用。

All 32-bit VSS applications (requesters, providers, and writers) must run as native 32-bit or 64-bit applications. Running them under WOW64 is not supported

http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/eadf5dcd-fbd1-4224-9a56-b5843efebb15/

所以我需要构建 Any CPU 项目,但我目前看到的唯一解决方案是为 x86 和 x64 提供重复项目。有没有更好的?

更新

当我在项目中引用 x64 库,但尝试加载 x86 库时,出现以下异常。

The located assembly's manifest definition does not match the assembly reference.

【问题讨论】:

  • 谢谢。我见过这样的帖子,但问题是只更改 DLL 并没有多大帮助。当我将 64 位 DLL 复制到我的文件夹中时它仍然不起作用,我仍然需要重新编译引用那些 64 位库的应用程序。难道我做错了什么?而且我也不明白应该参考哪个版本?
  • 是的。本机库必须具有相同的名称,但必须位于不同的文件夹中(例如您的应用程序的子文件夹)。在运行时,您调用SetDllDirectory 以使操作系统根据位数在正确的文件夹中查找。或者使用AppDomain.CurrentDomain.AssemblyResolve 事件作为suggested by Rover。您不直接引用这些库,SQLite 托管包装器会这样做。您的代码将使包装器加载底层本机库的正确版本。
  • 我又试了一次,对我不起作用。应用程序在启动时崩溃。问题是我在项目中引用 x64 库,但我在运行时选择 x86 库。如果我使用 x86 库构建我的应用程序,它可以在 x86 窗口上完美运行,否则就不行。
  • 糟糕。对不起,是我的错。其实我刚刚从网上下载了 64 位版本,但是 32 位版本有点旧。他们设法上传了新版本,我没有注意到。谢谢,问题已结束。
  • @axe 然后发布一个描述你所做的答案,并接受它。

标签: .net x86 project 64-bit anycpu


【解决方案1】:

主要问题是我在 x86 和 x64 上使用了不同版本的 SQLite。 我添加了方法

static private Assembly SQLitePlatformSpecificResolve(object sender, ResolveEventArgs args)
{
    string platform = Environment.Is64BitProcess ? "x64" : "x86";
    string assemblyName = new AssemblyName(args.Name).Name;
    string assemblyPath = Path.Combine(
        Environment.CurrentDirectory, "SQLite", platform, assemblyName + ".dll");

    return !File.Exists(assemblyPath) ? null : Assembly.LoadFrom(assemblyPath);
}

并设置事件处理程序

AppDomain.CurrentDomain.AssemblyResolve += SQLitePlatformSpecificResolve;

在主应用程序入口点。 现在它为 x86 平台和 64 位平台上的 x64 加载 x86 程序集。

谢谢。

【讨论】:

    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2010-11-09
    • 2016-12-30
    相关资源
    最近更新 更多