Search code examples
inno-setupregistrykey

Use a part of a registry key/value in the Inno Setup script


I have a need to retrieve a path to be used for some stuffs in the installer according an other application previously installed on the system.

This previous application hosts a service and only provides one registry key/value hosting this information: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\APPLICATION hosting the value ImagePath which Data is "E:\TestingDir\Filename.exe".

I need a way to only extract the installation path (E:\TestingDir) without the Filename.exe file.

Any suggestion? thanks a lot


Solution

  • You can achieve this using a scripted constant.

    You define a function that produces the value you need:

    [Code]
    
    function GetServiceInstallationPath(Param: string): string;
    var
      Value: string;
    begin
      if RegQueryStringValue(
           HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\APPLICATION',
           'ImagePath', Value) then
      begin
        Result := ExtractFileDir(Value);
      end
        else
      begin
        Result := { Some fallback value }
      end;
    end;
    

    And then you refer to it using {code:GetServiceInstallationPath} where you need it (like in the [Run] section).

    For example:

    [Run]
    Filename: "{code:GetServiceIntallationPath}\SomeApp.exe"
    

    Actually, you probably want to retrieve the value in InitializeSetup already, and cache the value in a global variable for use in the scripted constant. And abort the installation (by returning False from InitializeSetup), in case the other application is not installed (= the registry key does not exist).

    [Code]
    
    var
      ServiceInstallationPath: string;
    
    function InitializeSetup(): Boolean;
    var
      Value: string;
    begin
      if RegQueryStringValue(
           HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Services\APPLICATION',
           'ImagePath', Value) then
      begin
        ServiceInstallationPath := ExtractFileDir(Value);
        Log(Format('APPLICATION installed to %s', [ServiceInstallationPath]));
        Result := True;
      end
        else
      begin
        MsgBox('APPLICATION not installed, aborting installation', mbError, MB_OK);
        Result := False;
      end;
    end;
    
    function GetServiceInstallationPath(Param: string): string;
    begin
      Result := ServiceInstallationPath;
    end;
    

    See also a similar question: Using global string script variable in Run section in Inno Setup.