4
votes

I would like to set a registry key to HKLM when user is an administrator or HKCU when user is a normal user.

I tried this:

[Registry]
Root: "{code:DefRegRoot}"; Subkey: "Software\MyCompany\MySW\Settings"; ValueType: string; ValueName: "InstallPath"; ValueData: "{app}"

[Code]
function IsRegularUser(): Boolean;
begin
  Result := not (IsAdminLoggedOn or IsPowerUserLoggedOn);
end;

function DefRegRoot(Param: String): String;
begin
  if IsRegularUser then
    Result := HKCU
  else
    Result := HKLM;
end;

But the compiler returns an error on the first Registry line:

Parameter "Root" is not a valid value. 

Any suggestion about this ?

1
Given that PrivilegesRequired is fixed at compile time, there is no point trying to make a single installer do both per-machine and per-user installs. Pick one yourself and run with it. - Miral
What is your PrivilegesRequired directive set to? By default your setup will never have run as a non admin user, and if a non admin user tries to run it, then your settings will be saved for the user they elevate to. - Deanna

1 Answers

5
votes

I don't think it's possible to use function as a Root parameter value getter. I'd workaround this situation by using Check condition like this:

[Registry]
Root: HKCU; Subkey: "Software\MyCompany\MySW\Settings"; ValueType: string; ValueName: "InstallPath"; ValueData: "{app}"; Check: IsRegularUser 
Root: HKLM; Subkey: "Software\MyCompany\MySW\Settings"; ValueType: string; ValueName: "InstallPath"; ValueData: "{app}"; Check: not IsRegularUser

[Code]
function IsRegularUser: Boolean;
begin
  Result := not (IsAdminLoggedOn or IsPowerUserLoggedOn);
end;