在同一解决方案/项目中使用 Visual Studio 同时面向 32 位和 64 位 [英] Targeting both 32bit and 64bit with Visual Studio in same solution/project

查看:16
本文介绍了在同一解决方案/项目中使用 Visual Studio 同时面向 32 位和 64 位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何设置我的 Visual Studio 构建以实现多目标,我有点进退两难.

I have a little dilemma on how to set up my visual studio builds for multi-targeting.

背景:c# .NET v2.0,p/invoking into 3rd party 32 bit DLL's, SQL compact v3.5 SP1,有一个安装项目.目前,平台目标设置为 x86,因此可以在 Windows x64 上运行.

Background: c# .NET v2.0 with p/invoking into 3rd party 32 bit DLL's, SQL compact v3.5 SP1, with a Setup project. Right now, the platform target is set to x86 so it can be run on Windows x64.

第 3 方公司刚刚发布了他们 DLL 的 64 位版本,我想构建一个专用的 64 位程序.

The 3rd party company has just released 64 bit versions of their DLL's and I want to build a dedicated 64bit program.

这提出了一些我还没有得到答案的问题.我想要完全相同的代码库.我必须使用对 32 位 DLL 集或 64 位 DLL 的引用来构建.(第 3 方和 SQL Server Compact)

This raises some questions which I haven't got the answers to yet. I want to have the exact same code base. I must build with references to either the 32bit set of DLL's or 64bit DLL's. (Both 3rd party and SQL Server Compact)

这可以通过 2 组新配置(Debug64 和 Release64)解决吗?

Can this be solved with 2 new sets of configurations (Debug64 and Release64) ?

我是否必须创建 2 个单独的安装项目(std.visual studio 项目,没有 Wix 或任何其他实用程序),或者这可以在同一个 .msi 中解决吗?

Must I create 2 separate setup projects(std. visual studio projects, no Wix or any other utility), or can this be solved within the same .msi?

欢迎提出任何想法和/或建议.

Any ideas and/or recommendations would be welcomed.

推荐答案

是的,您可以在同一个项目中使用相同的代码库同时面向 x86 和 x64.一般来说,如果您在 VS.NET 中创建正确的解决方案配置,事情就会正常工作(尽管 P/Invoke 到完全非托管的 DLL 很可能需要一些条件代码):我发现需要特别注意的项目是:

Yes, you can target both x86 and x64 with the same code base in the same project. In general, things will Just Work if you create the right solution configurations in VS.NET (although P/Invoke to entirely unmanaged DLLs will most likely require some conditional code): the items that I found to require special attention are:

  • 对具有相同名称但具有特定位数的外部托管程序集的引用(这也适用于 COM 互操作程序集)
  • MSI 包(如前所述,需要面向 x86 或 x64)
  • MSI 包中任何基于 .NET 安装程序类的自定义操作

程序集引用问题无法在 VS.NET 中完全解决,因为它只允许您向项目添加具有给定名称的引用一次.要解决此问题,请手动编辑您的项目文件(在 VS 中,在解决方案资源管理器中右键单击您的项目文件,选择卸载项目,然后再次右键单击并选择编辑).添加对 x86 版本程序集的引用后,您的项目文件将包含如下内容:

The assembly reference issue can't be solved entirely within VS.NET, as it will only allow you to add a reference with a given name to a project once. To work around this, edit your project file manually (in VS, right-click your project file in the Solution Explorer, select Unload Project, then right-click again and select Edit). After adding a reference to, say, the x86 version of an assembly, your project file will contain something like:

<Reference Include="Filename, ..., processorArchitecture=x86">
  <HintPath>C:path	ox86DLL</HintPath>
</Reference>

将该 Reference 标签包裹在一个 ItemGroup 标签中,表明它适用的解决方案配置,例如:

Wrap that Reference tag inside an ItemGroup tag indicating the solution configuration it applies to, e.g:

<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
   <Reference ...>....</Reference>
</ItemGroup>

然后,复制并粘贴整个 ItemGroup 标记,并对其进行编辑以包含 64 位 DLL 的详细信息,例如:

Then, copy and paste the entire ItemGroup tag, and edit it to contain the details of your 64-bit DLL, e.g.:

<ItemGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
  <Reference Include="Filename, ..., processorArchitecture=AMD64">
     <HintPath>C:path	ox64DLL</HintPath>
   </Reference>
</ItemGroup>

在 VS.NET 中重新加载您的项目后,程序集参考"对话框会因这些更改而有些混乱,您可能会遇到一些有关使用错误目标处理器的程序集的警告,但您的所有构建都可以正常工作.

After reloading your project in VS.NET, the Assembly Reference dialog will be a bit confused by these changes, and you may encounter some warnings about assemblies with the wrong target processor, but all your builds will work just fine.

接下来要解决 MSI 问题,不幸的是,这需要一个非 VS.NET 工具:我更喜欢 Caphyon 的 Advanced Installer 用于此目的,因为它实现了所涉及的基本技巧(创建通用 MSI,以及 32 位和 64 位特定 MSI,并使用 .EXE 安装启动器提取正确的版本并在运行时进行所需的修复)非常非常好.

Solving the MSI issue is up next, and unfortunately this will require a non-VS.NET tool: I prefer Caphyon's Advanced Installer for that purpose, as it pulls off the basic trick involved (create a common MSI, as well as 32-bit and 64-bit specific MSIs, and use an .EXE setup launcher to extract the right version and do the required fixups at runtime) very, very well.

您可能可以使用其他工具或 Windows Installer XML (WiX) 工具集获得相同的结果,但是高级安装程序让事情变得如此简单(而且相当实惠),我从未真正考虑过替代方案.

You can probably achieve the same results using other tools or the Windows Installer XML (WiX) toolset, but Advanced Installer makes things so easy (and is quite affordable at that) that I've never really looked at alternatives.

可能仍然需要 WiX 的一件事是,即使在使用高级安装程序时,您的 .NET 安装程序类自定义操作也是如此.虽然指定应该只在某些平台上运行的某些动作很简单(分别使用 VersionNT64 和 NOT VersionNT64 执行条件),但内置的 AI 自定义动作将使用 32 位框架执行,即使在 64 位机器上.

One thing you may still require WiX for though, even when using Advanced Installer, is for your .NET Installer Class custom actions. Although it's trivial to specify certain actions that should only run on certain platforms (using the VersionNT64 and NOT VersionNT64 execution conditions, respectively), the built-in AI custom actions will be executed using the 32-bit Framework, even on 64-bit machines.

这可能会在未来的版本中修复,但就目前而言(或者在使用不同的工具创建具有相同问题的 MSI 时),您可以使用 WiX 3.0 的托管自定义操作支持来创建带有将使用相应的框架执行的适当位数.

This may be fixed in a future release, but for now (or when using a different tool to create your MSIs that has the same issue), you can use WiX 3.0's managed custom action support to create action DLLs with the proper bitness that will be executed using the corresponding Framework.

从 8.1.2 版开始,高级安装程序正确支持 64 位自定义操作.不幸的是,自从我最初的回答以来,它的价格已经上涨了很多,尽管与 InstallShield 及其同类产品相比,它仍然非常物有所值...

as of version 8.1.2, Advanced Installer correctly supports 64-bit custom actions. Since my original answer, its price has increased quite a bit, unfortunately, even though it's still extremely good value when compared to InstallShield and its ilk...

如果您的 DLL 已在 GAC 中注册,您也可以通过这种方式使用标准引用标签(以 SQLite 为例):

If your DLLs are registered in the GAC, you can also use the standard reference tags this way (SQLite as an example):

<ItemGroup Condition="'$(Platform)' == 'x86'">
    <Reference Include="System.Data.SQLite, Version=1.0.80.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86" />
</ItemGroup>
<ItemGroup Condition="'$(Platform)' == 'x64'">
    <Reference Include="System.Data.SQLite, Version=1.0.80.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64" />
</ItemGroup>

条件也简化为所有构建类型、发布或调试,并且仅指定处理器架构.

The condition is also reduced down to all build types, release or debug, and just specifies the processor architecture.

这篇关于在同一解决方案/项目中使用 Visual Studio 同时面向 32 位和 64 位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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