我正在编写一个函数,该函数将为我的设备安装所需的组件,该组件是基于 PowerShell 构建的。如果未找到特定版本的 PowerShell,我希望安装程序帮助用户安装它。我遇到的问题是如何正确调用离线安装程序进行安装。这是我的代码,它是一个通用函数(我正在使用 InnoSetup Dependency Installer ):
function SmartExec(product : TProduct; var resultcode : Integer): boolean;
begin
if (LowerCase(Copy(product.File, Length(product.File) - 2, 3)) = 'exe') then begin
Result := Exec(product.File, product.Parameters, '', SW_SHOWNORMAL, ewWaitUntilTerminated, resultcode);
end else begin
Result := ShellExec('', product.File, product.Parameters, '', SW_SHOWNORMAL, ewWaitUntilTerminated, resultcode);
end;
end;
我尝试使用以下方法:
function SmartExec(product : TProduct; var resultcode : Integer): boolean;
begin
if (LowerCase(Copy(product.File, Length(product.File) - 2, 3)) = 'exe') then begin
Result := Exec(product.File, product.Parameters, '', SW_SHOWNORMAL, ewWaitUntilTerminated, resultcode);
end else if (LowerCase(Copy(product.File, Length(product.File) - 2, 3)) = 'msu') then begin
Result := ShellExec('', 'wusa.exe ' + product.File, product.Parameters, '', SW_SHOWNORMAL, ewWaitUntilTerminated, resultcode);
end else begin
Result := ShellExec('', product.File, product.Parameters, '', SW_SHOWNORMAL, ewWaitUntilTerminated, resultcode);
end;
end;
当我编译并测试安装程序时,我会看到:
我将 /quiet/norestart
作为参数传递给 MSU 文件,该文件可以从命令提示符完美执行。
安装文件已下载到当前用户的%tmp%
,并且我看到了该文件。
有任何帮助或意见吗?
最佳答案
.msu
扩展名与 wusa.exe
关联,因此现有分支 ShellExec('', Product.File, ...)
应该可以完成这项工作。您不需要添加特定的 msu
分支。
无论如何,特定分支可以帮助调试,因此值得尝试。
ShellExec
function的第二个参数是一个FileName
,当你传入wusa.exe xxx.msu
时,这不是一个有效的文件名。
这应该有效:
Result := ShellExec('', 'wusa.exe', product.File + ' ' + product.Parameters, ...);
尽管使用 ShellExec
运行可执行文件有点大材小用,但请使用简单的 Exec
function相反:
Result := Exec('wusa.exe', product.File + ' ' + product.Parameters, ...);
当Exec
返回False
时,ResultCode
是一个Windows错误代码,解释执行失败的原因。您收到代码 3,即 ERROR_PATH_NOT_FOUND
(系统找不到指定的路径。)。
看来您使用的路径 (product.File
) 无效。
确保传递 .msu
的完整路径,而不仅仅是文件名。
在调用 Exec
之前尝试记录路径并检查文件是否存在。您可以使用:
Log(Format('Path is [%s], Exists = %d', [product.File, Integer(FileExists(product.File))]));
关于inno-setup - Inno Setup - 如何安装 Windows Update 离线安装程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236592/