如何用Wix5创建含VC Redist(x86+x64)与应用的安装程序?
问题描述
我想用Wix5创建一个安装程序,流程是先安装VC Redistributable(x86 + x64),再安装我的应用程序。但Wix5(包括Wix4)相关实现文档不足,现有讨论多为已不推荐的合并模块方案。官方文档仅提及XML树结构,教程也只演示单exe的简单安装。找到的示例构建时出现Error (active) WIX0350 The package being validated requires a higher version of Windows Installer than is installed on this machine. Validation cannot continue.错误。
最初用Package.wxs创建了基础安装程序:
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"> <Package Name="Test" Manufacturer="TestManufacturer" Version="1.0.0.0" UpgradeCode="0506dc46-deae-40ef-aed7-c9cd2e7ac08e"> <MajorUpgrade DowngradeErrorMessage="!(loc.DowngradeError)" /> <MediaTemplate EmbedCab="yes" /> <Feature Id="Main"> <Files Include="..\TestApp\publish\**"/> </Feature> </Package> </Wix>
尝试用Bundle+Chain包含VC Redist的ExePackage和应用包时,无法在Chain中直接引用应用包(仅接受包组或捆绑包),且仍存在WIX0350错误:
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:bal="http://wixtoolset.org/schemas/v4/wxs/bal" xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util"> <Bundle Name="TestInstaller" Version="1.0.0.0" Manufacturer="TestManufacturer" AboutUrl="https://test.com/" UpgradeCode="01234567-89AB-CDEF-0123-456789ABCDEF"> <BootstrapperApplication> <bal:WixStandardBootstrapperApplication LicenseUrl="https://example.net/license.html" ShowVersion="yes" SuppressOptionsUI="yes" Theme="hyperlinkLargeLicense" /> </BootstrapperApplication> <Chain> <ExePackage Id="VC_REDIST_X86" DisplayName="Microsoft Visual C++ 2015-2022 Redistributable (x86) - 14.40.33810" Cache="remove" PerMachine="yes" Permanent="yes" Protocol="burn" InstallCondition="VersionNT" DetectCondition="(VCRUNTIME_X86_VER >= VCRUNTIME_VER) AND VersionNT" InstallArguments="/install /quiet /norestart" RepairArguments="/repair /quiet /norestart" UninstallArguments="/uninstall /quiet /norestart"> <ExePackagePayload Name="VC_redist.x86.exe" ProductName="Microsoft Visual C++ 2015-2022 Redistributable (x86) - 14.40.33810" Description="Microsoft Visual C++ 2015-2022 Redistributable (x86) - 14.40.33810" Hash="91A6283F774F9E2338B65AA835156854E9E76AED32F821B13CFD070DD6C87E1542CE2D5845BEB5E4AF1DDB102314BB6E0AD6214D896BB3E387590A01EAE0C182" Size="13867304" Version="14.40.33810.0" DownloadUrl="https://download.visualstudio.microsoft.com/download/pr/9c69db26-cda4-472d-bdae-f2b87f4a0177/A32DD41EAAB0C5E1EAA78BE3C0BB73B48593DE8D97A7510B97DE3FD993538600/VC_redist.x86.exe" /> </ExePackage> </Chain> </Bundle> </Wix>
请问正确的实现方式是什么?
解决方案
1. 解决WIX0350错误
该错误是因为Wix默认的Windows Installer版本要求过高,在Package元素中显式指定兼容的InstallerVersion即可,比如指定为500(对应Windows Installer 5.0,支持Win7及以上系统):
<Package ... InstallerVersion="500" ...>
2. 正确的Bundle+Chain结构逻辑
要实现先装VC Redist再装应用的流程,需分两步:
- 先将你的应用打包成独立MSI(即完善最初的
Package.wxs配置) - 在Bundle的
Chain中依次添加VC Redist的ExePackage和应用的MsiPackage,Chain内元素按从上到下顺序执行安装
关键配置细节
- VC Redist检测:使用Wix内置属性检测是否已安装对应版本,无需自定义变量。比如x86版本用
NOT VCRUNTIME140_1_X86_OR_LATER >= v14.40.33810作为安装条件 - MsiPackage引用:通过
SourceFile指定应用MSI的路径,若Bundle项目已引用MSI项目,可使用$(var.项目名.TargetPath)自动获取输出路径 - 安装可靠性:将应用MSI的
Vital属性设为yes,确保应用安装失败时整个引导流程终止
3. 完整示例代码
第一步:完善应用MSI配置(Package.wxs)
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"> <Package Name="TestApp" Manufacturer="TestManufacturer" Version="1.0.0.0" UpgradeCode="0506dc46-deae-40ef-aed7-c9cd2e7ac08e" InstallerVersion="500" <!-- 解决WIX0350错误 --> Platform="x64" <!-- 根据应用实际平台选择x86/x64 --> Scope="perMachine"> <!-- 与VC Redist保持一致,按机器安装 --> <MajorUpgrade DowngradeErrorMessage="已安装更高版本的TestApp,请先卸载后重试。" /> <MediaTemplate EmbedCab="yes" /> <Feature Id="Main" Title="主应用" Level="1"> <Files Include="..\TestApp\publish\**"/> </Feature> </Package> </Wix>
第二步:创建Bundle引导程序(Bundle.wxs)
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:bal="http://wixtoolset.org/schemas/v4/wxs/bal"> <Bundle Name="TestApp完整安装程序" Version="1.0.0.0" Manufacturer="TestManufacturer" UpgradeCode="01234567-89AB-CDEF-0123-456789ABCDEF" BootstrapperApplicationRef="WixStandardBootstrapperApplication.HyperlinkLargeLicense"> <bal:WixStandardBootstrapperApplication LicenseUrl="license.html" <!-- 将许可证HTML文件放在项目目录中 --> ShowVersion="yes" SuppressOptionsUI="yes" /> <Chain> <!-- VC Redist x86 --> <ExePackage Id="VC_REDIST_X86" DisplayName="Microsoft Visual C++ 2015-2022 Redistributable (x86)" Cache="yes" PerMachine="yes" Permanent="yes" Protocol="burn" InstallCondition="NOT VCRUNTIME140_1_X86_OR_LATER >= v14.40.33810" InstallArguments="/install /quiet /norestart" RepairArguments="/repair /quiet /norestart" UninstallArguments="/uninstall /quiet /norestart"> <ExePackagePayload Name="VC_redist.x86.exe" Size="13867304" Version="14.40.33810.0" Hash="91A6283F774F9E2338B65AA835156854E9E76AED32F821B13CFD070DD6C87E1542CE2D5845BEB5E4AF1DDB102314BB6E0AD6214D896BB3E387590A01EAE0C182" SourceFile=".\Resources\VC_redist.x86.exe" /> <!-- 将VC文件放入项目Resources目录 --> </ExePackage> <!-- VC Redist x64 --> <ExePackage Id="VC_REDIST_X64" DisplayName="Microsoft Visual C++ 2015-2022 Redistributable (x64)" Cache="yes" PerMachine="yes" Permanent="yes" Protocol="burn" InstallCondition="NOT VCRUNTIME140_1_X64_OR_LATER >= v14.40.33810" InstallArguments="/install /quiet /norestart" RepairArguments="/repair /quiet /norestart" UninstallArguments="/uninstall /quiet /norestart"> <ExePackagePayload Name="VC_redist.x64.exe" Size="15100952" Version="14.40.33810.0" Hash="A32DD41EAAB0C5E1EAA78BE3C0BB73B48593DE8D97A7510B97DE3FD993538600" <!-- 用certutil -hashfile命令获取SHA512哈希 --> SourceFile=".\Resources\VC_redist.x64.exe" /> <!-- 将VC文件放入项目Resources目录 --> </ExePackage> <!-- 你的应用MSI --> <MsiPackage Id="TestApp_MSI" DisplayName="TestApp主程序" SourceFile="$(var.TestApp.TargetPath)" <!-- 需确保Bundle项目已引用MSI项目 --> Cache="yes" PerMachine="yes" Permanent="no" Vital="yes" /> <!-- 应用安装失败则终止整个流程 --> </Chain> </Bundle> </Wix>
4. 构建注意事项
- 确保Bundle项目已添加对MSI项目的引用,
$(var.TestApp.TargetPath)才能正确解析 - VC Redist文件的哈希值可通过命令
certutil -hashfile 文件名.exe SHA512获取,需与配置中的Hash值完全一致 - 若需离线安装,将VC Redist文件放入项目目录并设置
SourceFile路径即可
内容的提问来源于stack exchange,提问作者FullStackOverflowDev
相关产品推荐
相关产品推荐

