You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wix4 Bundle安装时HKCU写入非当前用户问题求助

Wix4 Bundle 解决非管理员安装时HKCU注册表项写入当前用户 hive 的问题

核心问题在于Bundle提升权限后,默认所有MSI包都在管理员上下文执行,导致HKCU项写入管理员账户的注册表 hive,而单独运行MSI时系统会保留原始用户上下文。以下是Wix4原生配置方案:

1. 拆分MSI为机器级和用户级两个包

将原MSI拆分为两个独立安装包,分别处理不同权限的注册表操作:

  • 机器级MSI:仅包含HKCR的COM注册项,设置Scope="perMachine",需要管理员权限完成机器级注册。
  • 用户级MSI:仅包含HKCU的Office插件识别条目,设置Scope="perUser",无需管理员权限,在当前登录用户上下文执行。

2. Bundle中配置两个MSI的执行上下文

在Bundle的Chain节点中,分别为两个MSI设置不同的Elevated属性,确保用户级MSI在原始用户上下文运行:

<Bundle Name="OfficeCOMAddinBundle" Version="1.0.0.0" Manufacturer="YourCompany" UpgradeCode="YOUR-UPGRADE-GUID">
  <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense" />
  
  <Chain>
    <!-- 机器级MSI:处理HKCR COM注册,强制提升权限 -->
    <MsiPackage SourceFile="MachineLevelSetup.msi" 
                Elevated="yes" 
                DisplayInternalUI="no" />
    
    <!-- 用户级MSI:处理HKCU Office条目,使用原始用户上下文 -->
    <MsiPackage SourceFile="UserLevelSetup.msi" 
                Elevated="no" 
                DisplayInternalUI="no">
      <MsiProperty Name="MSIINSTALLPERUSER" Value="1" />
    </MsiPackage>
  </Chain>
</Bundle>

3. 两个MSI的具体配置示例

机器级MSI(COM注册)

<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <Product Id="*" Name="OfficeCOMAddin Machine Setup" Version="1.0.0.0" Manufacturer="YourCompany" Language="1033" Scope="perMachine">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
    
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="OfficeCOMAddin" />
      </Directory>
    </Directory>

    <!-- HKCR下的COM注册项示例 -->
    <RegistryKey Root="HKCR" Key="CLSID\{YOUR-COM-CLSID}">
      <RegistryValue Name="InprocServer32" Value="[INSTALLFOLDER]YourAddin.dll" Type="string" />
      <RegistryValue Name="ThreadingModel" Value="Both" Type="string" />
      <!-- 其他COM必需注册项(如ProgID、TypeLib等) -->
    </RegistryKey>

    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
      <Component Id="AddinDll" Guid="YOUR-COMPONENT-GUID">
        <File Id="AddinDllFile" Source="path\to\YourAddin.dll" KeyPath="yes" />
      </Component>
    </ComponentGroup>
    
    <Feature Id="ProductFeature" Title="Machine Level Setup" Level="1">
      <ComponentGroupRef Id="ProductComponents" />
    </Feature>
  </Product>
</Wix>

用户级MSI(Office HKCU条目)

<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
  <Product Id="*" Name="OfficeCOMAddin User Setup" Version="1.0.0.0" Manufacturer="YourCompany" Language="1033" Scope="perUser">
    <Package InstallerVersion="200" Compressed="yes" InstallScope="perUser" />
    
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="AppDataFolder">
        <Directory Id="ADDINFOLDER" Name="OfficeCOMAddin" />
      </Directory>
    </Directory>

    <!-- HKCU下的Office插件识别条目示例(以Excel为例) -->
    <RegistryKey Root="HKCU" Key="Software\Microsoft\Office\Excel\Addins\YourAddin.ProgId">
      <RegistryValue Name="Description" Value="Your Office COM Addin" Type="string" />
      <RegistryValue Name="FriendlyName" Value="Your Addin" Type="string" />
      <RegistryValue Name="LoadBehavior" Value="3" Type="integer" />
      <RegistryValue Name="Manifest" Value="[ADDINFOLDER]YourAddin.vsto" Type="string" />
    </RegistryKey>

    <Feature Id="ProductFeature" Title="User Level Setup" Level="1">
      <!-- 若需要部署用户级配置文件,可在此添加组件 -->
    </Feature>
  </Product>
</Wix>

方案原理

  • 机器级MSI通过Elevated="yes"强制在管理员上下文执行,确保HKCR的COM注册项写入机器级注册表。
  • 用户级MSI通过Elevated="no"和MSIINSTALLPERUSER=1,强制在Bundle启动时的原始用户上下文执行,从而将HKCU项写入当前登录用户的注册表 hive。
  • 拆分包的方式既满足了COM注册的机器级权限要求,又保证了Office插件识别条目的用户级正确性,完全基于Wix4原生配置实现,无需自定义EXE。

内容的提问来源于stack exchange,提问作者S Bullen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:04:52