Inno Setup - Registry uninsdeletekey - exclude upg

2019-07-25 00:24发布

问题:

I have an Inno-Setup installation with the following entry in the [Registry] section

Root: HKCU; Subkey: SOFTWARE\MyCompany\MyApp; ValueName: MyKey; Flags: uninsdeletekey noerror

Which as stated by the flags is to be deleted upon uninstall.

But I need it to be preserved in case the uninstall is due to version upgrade.

How can it be done? Check maybe?

Thanks.

回答1:

You will need to pass the information that you want to preserve the key to the uninstaller since it doesn't know who executes it (well, you would not need to pass this information explicitly if you'd find the parent process programatically, but it's a rather hacky way).

A reliable solution is telling this to the uninstaller by the command line parameter. One implementation of an uninstaller which will conditionally delete a registry key is shown in the following example:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

#define MyAppRegKey "SOFTWARE\MyCompany\MyApp"

[Registry]
; no uninsdeletekey flag must be used here
Root: HKCU; Subkey: "{#MyAppRegKey}"; Flags: noerror
Root: HKCU; Subkey: "{#MyAppRegKey}"; ValueType: string; ValueName: "MyKey"; ValueData: "MyValue"; Flags: noerror

[Code]
function CmdLineParamExists(const Value: string): Boolean;
var
  I: Integer;  
begin
  Result := False;
  for I := 1 to ParamCount do
    if CompareText(ParamStr(I), Value) = 0 then
    begin
      Result := True;
      Exit;
    end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  // if we are at the post uninstall step and the uninstaller wasn't executed with
  // the /PRESERVEREGKEY parameter, delete the key and all of its subkeys (here is
  // no error handling used, as your original script ignores all the errors by the
  // noerror flag as well)
  if (CurUninstallStep = usPostUninstall) and not CmdLineParamExists('/PRESERVEREGKEY') then
    RegDeleteKeyIncludingSubkeys(HKCU, '{#MyAppRegKey}');
end;

If you run unistaller of such setup with the /PRESERVEREGKEY command line parameter, the registry key will be preserved, deleted otherwise, e.g.:

unins000.exe /PRESERVEREGKEY

But I can't think of a real usage of the above other than that you are sharing a registry key between two different applications. Applications installed by a different setup (e.g. a setup with different AppId).

Note that there's no need to uninstall the previous version of your application in case of update (in case when you're installing a new version of the setup with the same AppId). If you will follow this rule, Inno Setup will perform an update and your registry key will be preserved until you uninstall the application.