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.


; 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"
#define EnvironmentKey "System\CurrentControlSet\Control\Session Manager\Environment"


Use a scripted constant:

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

Another option is using the Check parameter:

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.

Answered By – Martin Prikryl

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published