0

My setup is set to run with lowest privileges

PrivilegesRequired=lowest

But I'm executing setup as Admin (right click-> run as admin, enter Admin credential in UAC), and want to check Logged In User's registry in InitializeSetup()

function InitializeSetup(): boolean;
begin
  if RegQueryStringValue(HKCU,'SOFTWARE\{some path}','Version', {some value}) then
  begin
     { do something here }
  end
end

But this checks the registry value for the Admin Account, not for Logged In User Account

Is there a way to check the logged in users registry at this point?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
RN92
  • 1,380
  • 1
  • 13
  • 32
  • But maybe you should explain us, why do you execute the setup as Admin, if you set `PrivilegesRequired=lowest`. It doe not make sense to me. – Martin Prikryl Jul 26 '17 at 07:47
  • For a general discussion, see also [Installing application for currently logged in user from Inno Setup installer running as Administrator](https://stackoverflow.com/q/44575666/850848). – Martin Prikryl Jul 26 '17 at 07:47
  • @Martin - I'm bundling two EXEs generated via Visual Studio to one Inno setup. One installer need Admin privileges and other don't. That's why setup need to be run in both modes. – RN92 Jul 26 '17 at 08:23
  • @Martin - and also the suggested links show how to create reg keys. but I need to **search** registry here – RN92 Jul 26 '17 at 08:24
  • OK, but mainly the second answer explains that what you are trying to do is just wrong. – Martin Prikryl Jul 26 '17 at 09:59

1 Answers1

0

First, you should not try to access a user environment from an installer running with Administrator privileges. That's just wrong.

For a general discussion on this topic, see:
Installing application for currently logged in user from Inno Setup installer running as Administrator.


Anyway, you can use a function below.

The code combines these solutions:

function ReqQueryValueOfOriginalUser(var ResultStr: String): Boolean;
var
  Uniq: string;
  TempFileName: string;
  Cmd: string;
  Key: string;
  Value: string;
  Params: string;
  Lines: TArrayOfString;
  Buf: string;
  ResultCode: Integer;
  P: Integer;
begin
  Log('Querying registry value of original user');
  Uniq := ExtractFileName(ExpandConstant('{tmp}'));
  TempFileName :=
    ExpandConstant(Format('{commondocs}\appdata-%s.txt', [Uniq]));
  Cmd := ExpandConstant('{cmd}');
  Key := 'HKEY_CURRENT_USER\Software\{some path}';
  Value := 'Version';
  Params := Format('/C reg.exe QUERY "%s" /v "%s" > "%s"', [Key, Value, TempFileName]);
  Result := False;
  if ExecAsOriginalUser(Cmd, Params, '', SW_HIDE, ewWaitUntilTerminated, ResultCode)
     and (ResultCode = 0) then
  begin
    if LoadStringsFromFile(TempFileName, Lines) then
    begin
      if (Length(Lines[0]) > 0) or
         (Lines[1] <> Key) then
      begin
        Log(Format('Unexpected output of reg.exe QUERY: "%s" - "%s"', [
          Lines[0], Lines[1]]));
      end
        else
      begin
        Buf := Trim(Lines[2]);
        if Copy(Buf, 1, Length(Value)) <> Value then
        begin
          Log(Format('Unexpected output of value query: "%s"', [Buf]));
        end
          else
        begin
          Buf := Trim(Copy(Buf, Length(Value) + 1, Length(Buf) - Length(Value)));
          P := Pos(' ', Buf);
          if P = 0 then
          begin
            Log(Format('Cannot find type and value separator in "%s"', [Buf]));
          end
            else
          begin
            ResultStr := Trim(Copy(Buf, P + 1, Length(Buf) - P));
            Log(Format('Value is "%s"', [ResultStr]));
            Result := True;
          end;
        end;
      end;
    end
      else
    begin
      Log(Format('Error reading %s', [TempFileName]));
    end;
    DeleteFile(TempFileName);
  end
    else
  begin
    Log('Error querying registry key of original user');
  end;

  Result := True;
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992