我正在编写一个将为我的设备安装必需组件的函数,该函数基于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的第二个参数是ShellExec
,当您传入FileName
时,无效的文件名。
这应该起作用:
wusa.exe xxx.msu
尽管使用Result := ShellExec('', 'wusa.exe', product.File + ' ' + product.Parameters, ...);
运行可执行文件是过大的,但改用普通的ShellExec
:
Exec
function
[Exec
返回Result := Exec('wusa.exe', product.File + ' ' + product.Parameters, ...);
时,Exec
是Windows错误代码,说明执行失败的原因。您得到的代码3是False
(系统找不到指定的路径。)。
因此看来您正在使用的路径(ResultCode
)无效。
请确保您传递的是ERROR_PATH_NOT_FOUND
的完整路径,而不仅仅是文件名。
在调用product.File
之前尝试记录路径,并检查文件是否存在。您可以使用:
.msu