2
votes

I'm trying to create an installer using Inno Setup when I encounter this problem, everything else is being installed, but the registry keys are not being installed in the current user. I found this helpful topic (many thanks!):
Inno Setup Creating registry key for logged in user (not admin user)

This solved the part for adding registry keys, like this:

 [Run]
 Filename: reg.exe; \
   Parameters: "ADD ""HKCU\Software\myprinter"" /v OutputFile /t REG_SZ /d ""{localappdata}\temp\\"""; \
   Flags: runasoriginaluser runhidden

My problem:
{localappdata} refers to the admin user (admin) not the local user (test):

It added: HKCU\Software\myprinter OutputFile REG_SZ c:\users\admin\appdata\local\temp\

Is required: HKCU\Software\myprinter OutputFile REG_SZ c:\users\test\appdata\local\temp\

The software does not work because it is not allowed to use the admin temp (of course)

Although according to the online help it should work for the user that started the setup, it seems it does not, in my case. (with or without the postinstall flag)

runasoriginaluser

Valid only in a [Run] section. If this flag is specified and the system is running Windows Vista or later, the spawned process will execute with the (normally non-elevated) credentials of the user that started Setup initially (i.e., the "pre-UAC dialog" credentials).

This is the default behavior when the postinstall flag is used.

If a user launches Setup by right-clicking its EXE file and selecting "Run as administrator", then this flag, unfortunately, will have no effect, because Setup has no opportunity to run any code with the original user credentials. The same is true if Setup is launched from an already-elevated process. Note, however, that this is not an Inno Setup-specific limitation; Windows Installer-based installers cannot return to the original user credentials either in such cases.

This flag cannot be combined with the runascurrentuser flag.

Note: running Inno Setup: 5.5.9 (a) and Windows 10

1
Do not use Ansi version of Inno Setup, use Unicode version.Martin Prikryl

1 Answers

2
votes

First, your approach is not correct.

There two correct ways:

  1. If the installer installs the application for the current (unprivileged) user only, do not require Administrator privileges. Use PrivilegesRequired=lowest.

    PrivilegesRequired=lowest
    

    Then the {localappdata} constant (and similar) will correctly refer to the current user's folder.

  2. If the installer installs the application for all users, it does not make sense to update registry of one specific users. All users need the registry settings, not just the one. In this case the recommended approach is to set common registry settings in HKLM (or store the settings to some common file). And have the application copy the settings to the user registry hive on the first run.

    See also How to write to the user's My Documents directory with installer when the user used 'Run As Administrator'.

You can also allow the user choose between these two approaches.
See Make Inno Setup installer request privileges elevation only when needed.

For similar questions, see


Anyway, the {localappdata} constant is resolved by the Inno Setup process, running within the Administrator account context. And the resolving is in no way affected by the runasoriginaluser flag.

But you can use an equivalent %LOCALAPPDATA% environment variable, which will be resolved by the executed process, i.e within the "original user" context.

To allow environment variables resolution, you need to execute the command through cmd.exe.

[Run]
Filename: {cmd}; \
    Parameters: "/C reg.exe ADD ""HKCU\Software\myprinter"" /v OutputFile /t REG_SZ /d ""%LOCALAPPDATA%\temp\\"""; \
    Flags: runasoriginaluser runhidden

For another approach, see Inno Setup - Access unprivileged account folders from installer that requires privileges.