Inno Setup - access to unprivileged account folders from an installer that requires privileges

I use Inno Setup to install documents / files, not the application, and this is primarily for Windows 7 users. Thus, my DestDir based on {userdocs} so that all files will be installed in the folder below this document library.

The problem occurs when I use the same installer to install the TTF font. This requires elevated privileges ( admin or superuser ). The problem that I see is that if a non-administrator user starts the installation, they are correctly requested via UAC for the administrator / superuser password ... but at this point DestDir for installation is changed to the administrator's document folder and not the user folder documents. Is there a way around this or prevent it?

For example, a non-Admin Fre has a document path:

 C:\Users\Fred\My Documents\ 

And if I do not include the TTF font as part of the installation, this is what the installer will use as the base path for the installation of {userdocs} , and it works fine.

If I include the TTF font as part of the installation with the same Admin user who is not an administrator, by the time the installation is complete, {userdocs} become

 C:\Users\AdminUser\My Documents\ 

... which is not the intended result ... you just need administrator privileges for the font installation part and you need files installed in the actual area of โ€‹โ€‹user documents.

Thanks.

+2
source share
1 answer

Create a child installer for fonts with the PrivilegesRequired=admin directive that you will run from the built-in wizard,

The installerโ€™s master code will look like this:

 [Setup] PrivilegesRequired=lowest [Files] Source: "ttfsetup.exe"; DestDir: {tmp}; Flags: deleteafterinstall [Run] Filename: "{tmp}\ttfsetup.exe"; Parameters: /silent; StatusMsg: "Installing TTF fonts..." 

And, of course, you must remove the child installer from the main uninstaller.

You can also make sure that the user did not run the master installer with administrator privileges explicitly. See My answer to How to write to the user documents folder with the installer in the My Documents folder when the user used Run as Administrator .

Another way to implement this is to use the ShellExec function with runas verb to execute a utility with increased external copy ( copy , xcopy , robocopy ). See Inno Setup - Register the components as an administrator (it starts regsvr32 , but the concept is the same).


Another option is to execute the failed process using the installer with elevated rights only to resolve the path to the source folder of user documents.

Use the ExecAsOriginalUser function .

You need to exchange the path between the installers through some temporary file available for both accounts. For instance. The {commondocs} file, as shown in Inno Setup, is always installed in the AppData admin directory .

 [Files] Source: "*.txt"; DestDir: "{code:GetUserDocumentsFolder}" [Code] var UserDocumentsFolder: string; function GetUserDocumentsFolder(Params: string): string; begin Result := UserDocumentsFolder; end; function InitializeSetup(): Boolean; var TempFile: string; Code: string; Buf: TArrayOfString; ResultCode: Integer; begin Result := True; TempFile := { some path accessible by both users }; Code := '[Environment]::GetFolderPath(''MyDocuments'') | ' + 'Out-File "' + TempFile + '" -Encoding UTF8'; Log(Format('Executing: %s', [Code])); if (not ExecAsOriginalUser('powershell.exe', Code, '', SW_HIDE, ewWaitUntilTerminated, ResultCode)) or (ResultCode <> 0) or (not LoadStringsFromFile(TempFile, Buf)) then begin MsgBox('Failed to resolve user MyDocuments path', mbError, MB_OK); Result := False; end else begin UserDocumentsFolder := Buf[0]; Log(Format('User Documents path resolved to "%s"', [UserDocumentsFolder])); end; end; 

Related discussions:

+1
source

Source: https://habr.com/ru/post/915371/


All Articles