在升级时丢弃/ TYPE和/ COMPONENTS参数

时间:2017-07-24 05:41:39

标签: inno-setup

如果是升级/重新安装,有没有办法丢弃在命令行上传递给安装程序的/TYPE/COMPONENTS参数值,而是使用以前使用的值? 我可以从Registry中读取之前使用的值(或者根据文件的存在来确定细节,假设它们没有被手动更改)

我已阅读以下主题并可在UI模式下禁用“选择组件”页面

  1. Inno Setup Skip "Select Components" page when /Type command-line parameter is specified
  2. InnoSetup: Disable components page on upgrade
  3. 但是,如果从命令行传递上述参数,它们似乎会覆盖默认值。

1 个答案:

答案 0 :(得分:2)

你不能丢弃它们。

您可以做的是检查这些参数是否已提供,以及是否:

  • 重新启动不带它们的安装程序(如下所示)或
  • 从注册表中读取以前选择的类型和组件,并相应地重新设置控件。

在没有/TYPE=/COMPONENTS=

的情况下重新启动安装程序
const
  UninstallKey =
     'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}_is1';

function IsUpgrade: Boolean;
var
  Value: string;
begin
  Result :=
    (RegQueryStringValue(HKLM, UninstallKey, 'UninstallString', Value) or
     RegQueryStringValue(HKCU, UninstallKey, 'UninstallString', Value)) and
    (Value <> '');
end;

function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string;
  lpParameters: string; lpDirectory: string; nShowCmd: Integer): THandle;
  external 'ShellExecuteW@shell32.dll stdcall';

function InitializeSetup(): Boolean;
var
  Params, S: string;
  Relaunch: Boolean;
  I, RetVal: Integer;
begin
  Result := True;

  if IsUpgrade then
  begin
    Relaunch := False;
    { Collect current instance parameters }
    for I := 1 to ParamCount do
    begin
      S := ParamStr(I);
      if (CompareText(Copy(S, 1, 7), '/TYPES=') = 0) or
         (CompareText(Copy(S, 1, 12), '/COMPONENTS=') = 0) then
      begin
        Log(Format('Will re-launch due to %s', [S]));
        Relaunch := True;
      end
        else
      begin
        { Unique log file name for the child instance }
        if CompareText(Copy(S, 1, 5), '/LOG=') = 0 then
        begin
          S := S + '-sub';
        end;
        { Do not pass our /SL5 switch }
        if CompareText(Copy(S, 1, 5), '/SL5=') <> 0 then
        begin
          Params := Params + AddQuotes(S) + ' ';
        end;
      end;
    end;

    if not Relaunch then
    begin
      Log('No need to re-launch');
    end
      else
    begin
     Log(Format('Re-launching setup with parameters [%s]', [Params]));
      RetVal := ShellExecute(0, '', ExpandConstant('{srcexe}'), Params, '', SW_SHOW);
      Log(Format('Re-launching setup returned [%d]', [RetVal]));
      Result := (RetVal > 32);
      { if re-launching of this setup succeeded, then... }
      if Result then
      begin
        Log('Re-launching succeeded');
        { exit this setup instance }
        Result := False;
      end
        else
      begin
        Log(Format('Elevation failed [%s]', [SysErrorMessage(RetVal)]));
      end;
    end;
  end;
end;

该代码适用于Inno Setup的Unicode版本。

可以进一步改进代码以使主安装程序等待子安装程序完成。什么时候可以有所作为,特别是如果安装程序是由某个自动部署过程执行的。