Search code examples
windowsinstallationregistryinno-setup

Change Environment registry key based on whether installer in user or admin mode


If the user runs the installer in admin mode, the system path should be modified and if the installer is run in user mode, then the user environment variable need to me modified.

[Registry]

; If user installation mode
#define EnvironmentRootKey "HKCU"
#define EnvironmentKey "Environment"
; If admin mode
#define EnvironmentRootKey "HKLM"
#define EnvironmentKey "System\CurrentControlSet\Control\Session Manager\Environment"

Root: {#EnvironmentRootKey}; Subkey: "{#EnvironmentKey}"; ValueType: expandsz; \
  ValueName: "Path"; ValueData: "{olddata};{app}\bin"; Tasks: addtopath; \
  Check: NeedsAddPath(ExpandConstant('{app}\bin'))

I know HKA automatically resolves to HKCU if the installer is in user mode and HKLM in admin mode, but there is no automatic equivalent for EnvironmentKey.

Basically something like:

#if "HKA" == "HKCU"
#define EnvironmentKey "Environment"
#else
#define EnvironmentKey "System\CurrentControlSet\Control\Session Manager\Environment"
#endif

Solution

  • Use a scripted constant:

    [Registry]
    Root: HKA; Subkey: "{code:GetEnvironmentKey}"; ...
    
    [Code]
    function GetEnvironmentKey(Param: string): string;
    begin
      if IsAdminInstallMode then
        Result := 'System\CurrentControlSet\Control\Session Manager\Environment'
      else
        Result := 'Environment';
    end;
    

    Another option is using the Check parameter:

    [Registry]
    Root: HKCU; \
        Subkey: "Environment"; \
        Check: not IsAdminInstallMode; ...
    
    Root: HKLM; \
        Subkey: "System\CurrentControlSet\Control\Session Manager\Environment"; \
        Check: IsAdminInstallMode; ...
    

    It's more "code", but no Code.