1

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.

Captain
  • 713
  • 1
  • 7
  • 20
  • So you are uninstalling the previous version when installing the new one and you want to preserve that key, is that correct ? – TLama Dec 04 '14 at 09:19
  • Yes. That is correct. – Captain Dec 04 '14 at 09:22
  • Hm, so then we'll need to pass this information (that someone wants to conditionally preserve the key) to the uninstaller since it doesn't know who executes it. – TLama Dec 04 '14 at 09:25
  • This is what I asked. How do I get this information, and how do I pass it? – Captain Dec 04 '14 at 09:27
  • Just one last thing. You are talking about upgrade, is that really upgrade ? Not just update ? Aren't you victim of what the people said here http://stackoverflow.com/q/2000296/960757 ? Setup **for an update** keeps the same `AppId` and **you don't have to uninstall the already installed version** (only in corner cases). Setup for an upgrade has the `AppId` different from the setup of the previous version. Which one is this ? Do you even need to uninstall the previous version ? – TLama Dec 04 '14 at 10:42
  • Ok, so I guess I *update*, and not *upgrade*. I do use the same AppId and I don't invoke the uninstall by myself, but rather it is invoked automatically by InnoSetup. Still, it seems that because of the uninsdeletekey flag, the registry key is deleted when I run a new setup on top of the previous one. – Captain Dec 04 '14 at 10:48
  • No, that is not so. The `uninsdeletekey` flag deletes the key only when you run uninstaller. If you run an update, the key is preserved. Hence all that you need is not uninstalling the previous version when you run update (a setup with the same `AppId`). – TLama Dec 04 '14 at 11:09

1 Answers1

0

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.

TLama
  • 75,147
  • 17
  • 214
  • 392