WiX 3自定义操作msiexec执行正常,双击MSI安装未触发如何解决?
问题现象
我编写了一个用于测试自定义操作的简单函数,功能为将文件解压到目标目录。
- 以管理员身份打开命令行,执行以下命令运行MSI安装包时,自定义操作可正常执行:
msiexec /i MyProgramm.msi /l*v thelog.txt
- 直接双击MSI安装包运行安装时,该自定义操作不会执行。
原始WiX 3配置代码如下:
<Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" /> <CustomAction Id="UnzipFiles" BinaryKey="CustomActions" DllEntry="CustomAction1" Execute='immediate' Impersonate='no' Return="check"/> <InstallExecuteSequence> <Custom Action="UnzipFiles" After="InstallFinalize"></Custom> </InstallExecuteSequence>
故障原因
Execute='immediate'配置的立即执行自定义操作,始终以启动安装程序的用户身份运行,Impersonate参数对立即执行模式无效。双击运行MSI时,默认运行在普通用户上下文,若解压目标为Program Files等系统保护目录,会因权限不足执行失败。- 自定义操作挂载在
InstallFinalize之后,此时MSI安装的提升权限上下文已经被回收,即便安装过程中触发了UAC提权,该节点后的操作也无法拿到系统级操作权限。
修复方案
修改自定义操作配置为延迟执行模式,调整执行顺序到InstallFinalize之前即可:
<Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" /> <!-- 修改Execute为deferred,保持Impersonate=no即以系统权限运行,适配公共目录写入需求 --> <CustomAction Id="UnzipFiles" BinaryKey="CustomActions" DllEntry="CustomAction1" Execute='deferred' Impersonate='no' Return="check"/> <InstallExecuteSequence> <!-- 调整到InstallFiles之后,InstallFinalize之前执行 --> <Custom Action="UnzipFiles" After="InstallFiles"></Custom> </InstallExecuteSequence>
如果解压目标为当前用户的私有目录,可将Impersonate改为yes,以当前登录用户身份执行操作
内容的提问来源于stack exchange,提问作者Jens H
相关产品推荐
相关产品推荐

