【问题标题】:How to configure my project to work on 64bit and 32 bit machines如何配置我的项目以在 64 位和 32 位机器上工作
【发布时间】:2013-05-21 22:00:09
【问题描述】:

我有一个配置为“任何 CPU”的项目。 现在我必须分别引用具有 x86 和 x64 编译版本的第三方 dll(我无法获得第三方 dll 的 AnyCPU 版本)。 我已将项目的配置文件更改为基于平台引用特定的 dll,如下所示。(这是一个示例项目配置文件)

<PropertyGroup>
<CurrentPlatform>x86</CurrentPlatform>
<CurrentPlatform Condition="'$(PROCESSOR_ARCHITECTURE)'=='AMD64' or '$(PROCESSOR_ARCHITEW6432)'=='AMD64'">x64</CurrentPlatform>
</PropertyGroup>
<ItemGroup Condition=" '$(Platform)' == 'x86' ">
<!--Compiled as ClassLibrary target platform is x86-->
 <Reference Include="ClassLibrary">
    <HintPath>..\..\ClassLibrary_x86\ClassLibrary_x86\bin\Debug\ClassLibrary.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
 <!--Compiled as ClassLibrary target platform is x64-->
 <Reference Include="ClassLibrary">
      <HintPath>..\..\ClassLibrary_x64\ClassLibrary_x64\bin\Debug\ClassLibrary.dll</HintPath>
</Reference>
</ItemGroup>

当我在 Visual Studio 中运行此项目时,它无法正常工作。抛出 BadImageException。当我将我的项目目标平台更改为 x86 并运行应用程序时,它将工作。如果我将其更改为 x64,则抛出相同的 BadImageException

这里有什么问题吗?我不想仅仅因为一个 dll 引用而创建两个项目(x86 和 x64)。如果上述方式错误,还有其他方法可以继续吗?

我的开发环境是VS2010和.NET4.0和Win7 64bit OS。

【问题讨论】:

    标签: c# x86 64-bit 32bit-64bit dll-reference


    【解决方案1】:

    将平台选项更改为 X86 并且仅引用 32 位 dll。这样,可执行文件将在 32 位和 64 位操作系统上运行。 使用 x86 选项构建的应用程序将在 WOW64 下的 64 位操作系统上作为 32 位进程运行。

    下一个链接也可以提供帮助 - Conditionally use 32/64 bit reference when building in Visual Studio

    下一个链接显示how to load the right dll,这就是您要查找的内容

    【讨论】:

    • 我必须为 64 位操作系统引用 64 位 dll。我已经看到你的链接,但没有解决我的问题。
    • 您有什么理由要使用 x64 而不是一直使用 x86 吗?
    • 是的 32 位 dll 不能工作,我的意思是它不会在 64 位机器上给出预期的结果。
    • 好的,也许你可以在构建后使用构建后事件复制正确的dll版本。我已经用相关链接更新了答案
    • @Sree 回答你的问题了吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2019-05-20
    • 2018-05-16
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多