Creating registry key for logged in user (not admin user) in Inno Setup

I do not think you can write explicitly to registry keys of logged in user from Inno Setup. You can write to registry keys of the user which is running the installer only.

You can write to any (or all) user’s registry keys via HKEY_USERS, but I do not know, if you can tell, which user is logged in.


But you can execute an external application that writes the registry key as part of the installation using the runasoriginaluser flag or the ExecAsOriginalUser function.

You can use the reg.exe for this:

[Run]
Filename: reg.exe; \
    Parameters: "ADD HKCU\Software\MyProgram /v Foo /t REG_SZ /d Bar"; \
    Flags: runasoriginaluser runhidden

or

procedure CurStepChanged(CurStep: TSetupStep);
var
  Params: string;
  ResultCode: Integer;
begin
  if CurStep = ssPostInstall then
  begin
    Log('Adding registry key for original user');
    Params := 'ADD HKCU\Software\xxxx /v Foo /t REG_SZ /d Bar';
    if ExecAsOriginalUser(
         'reg.exe', Params, '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
       (ResultCode = 0) then
    begin
      Log('Added registry key for original user');
    end
      else
    begin
      Log('Error adding registry key for original user');
    end;
  end;
end;

Credits for the idea: @Markus. Though note that this won’t work if the user explicitly runs your installer as Administrator, as then the “original” user will be the Administrator already. For a general discussion of the problem, see Installing application for currently logged in user from Inno Setup installer running as Administrator.

Leave a Comment