Wix中CAQuietExec无法向cmd.exe批处理传递参数的问题求助
解决WiX中CAQuietExec无法传递参数到批处理的问题
我之前也碰到过一模一样的问题,核心原因是你当前的CustomAction配置没把参数正确拼接到CAQuietExec要执行的命令行里。咱们一步步来修复:
问题根源
你原来的配置里,SetCustomActionData把参数存在了BatchCmd属性中,但BatchCmd只给BatchRun设置了批处理的路径,完全没把参数传递过去。CAQuietExec执行的时候只跑了批处理本身,没带任何参数,所以批处理里的%1是空的,echo %1就默认输出echo的状态(也就是"ON")。
修复步骤
我们需要把批处理路径和参数拼接成完整的命令行,再传递给CAQuietExec执行:
简化CustomAction定义
删掉原来的SetCustomActionData和BatchCmd,只保留两个CustomAction:一个用来在即时阶段构建完整命令,另一个是执行命令的延迟CAQuietExec:<CustomAction Id="SetBatchCommand" Property="BatchRun" Value=""[#file_configure.bat]" [BATCHPARAMETER]" Execute="immediate" /> <CustomAction Id="BatchRun" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="yes" />这里的
Value里,"是WiX对双引号的转义,用来包裹批处理路径(避免路径含空格时出错),后面直接跟上你的[BATCHPARAMETER]属性值,作为批处理的第一个参数。调整执行序列
只需要在BatchRun之前执行我们新的SetBatchCommand即可:<InstallExecuteSequence> <Custom Action="SetBatchCommand" Before="BatchRun">NOT Installed</Custom> <Custom Action="BatchRun" After="InstallFiles">NOT Installed</Custom> </InstallExecuteSequence>
修改后的完整WiX代码
<?xml version="1.0" encoding="utf-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Product Id="MY-UID" Name="my_name" Language="1033" Version="1.11.5164" Manufacturer="company" UpgradeCode="MY-UID"> <Package Description="Test file in a Product" Comments="Simple test" InstallerVersion="200" Compressed="yes" /> <Media Id="1" Cabinet="simple.cab" EmbedCab="yes" /> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder" Name="PFiles"> <Directory Name="my_folder" Id="MY_FOLDER"> <Component Id="CONFIGURE.BAT" DiskId="1" Guid="MY-UID"> <File KeyPath="yes" Id="file_configure.bat" Name="configure.bat" Source="configure.bat" /> </Component> </Directory> </Directory> </Directory> <Feature Id="MainFeature" Title="Main Feature" Level="1"> <ComponentRef Id="CONFIGURE.BAT" /> </Feature> <UI /> <UIRef Id="WixUI_Minimal" /> <Property Id="BATCHPARAMETER" Value="HAHIHUHEHO" Secure="yes" /> <CustomAction Id="SetBatchCommand" Property="BatchRun" Value=""[#file_configure.bat]" [BATCHPARAMETER]" Execute="immediate" /> <CustomAction Id="BatchRun" BinaryKey="WixCA" DllEntry="CAQuietExec" Execute="deferred" Return="check" Impersonate="yes" /> <InstallExecuteSequence> <Custom Action="SetBatchCommand" Before="BatchRun">NOT Installed</Custom> <Custom Action="BatchRun" After="InstallFiles">NOT Installed</Custom> </InstallExecuteSequence> </Product> </Wix>
验证效果
修改后重新编译安装程序,你的configure.bat里的echo %1就会正确输出HAHIHUHEHO,而不是"ON"了。如果你的参数包含空格,记得给参数也加上引号,比如改成"[BATCHPARAMETER]"。
内容的提问来源于stack exchange,提问作者illuminate33
相关产品推荐
相关产品推荐

