Inno Setup-从需要特权的安装程序访问非特权帐户文件夹 [英] Inno Setup - Access unprivileged account folders from installer that requires privileges

查看:112
本文介绍了Inno Setup-从需要特权的安装程序访问非特权帐户文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Inno Setup来安装文档/文件,而不是应用程序,这主要是针对Windows 7用户.因此,我的DestDir基于{userdocs},因此所有文件都将安装在该用户的文档库下方的文件夹中.

当我使用相同的安装程序来安装TTF字体时,会出现问题.这需要提升的特权(adminsuperuser).我看到的问题是,如果非管理员用户运行安装,则会通过UAC正确提示他们输入管理员/超级用户密码...但是,此时安装的DestDir更改为管理员文档"文件夹而不是用户的documents文件夹.有什么办法可以解决此问题或防止这种情况发生?

例如,非管理员帐户Fre的文档路径为:

C:\Users\Fred\My Documents\

如果我没有在安装过程中包含TTF字体,则安装程序将使用它作为安装{userdocs}的基本路径,并且可以完美运行.

如果我确实在非管理员用户Fred的安装中包括了TTF字体,那么安装完成时{userdocs}已成为

C:\Users\AdminUser\My Documents\ 

...这不是预期的结果...只是需要字体安装部分的Admin特权,并且需要将文件安装到实际用户的文档区域中.

谢谢.

解决方案

使用 PrivilegesRequired=admin指令,您将在非高架主安装程序中运行.

主安装程序代码如下:

[Setup]
PrivilegesRequired=lowest

[Files]
Source: "ttfsetup.exe"; DestDir: {tmp}; Flags: deleteafterinstall

[Run]
Filename: "{tmp}\ttfsetup.exe"; Parameters: /silent; StatusMsg: "Installing TTF fonts..."

当然,您应该从主卸载程序中卸载子安装程序.

您可能还想确保用户没有以管理员权限显式运行主安装程序.请参阅我对我的回答,当用户使用以管理员身份运行"时如何使用安装程序写入用户的我的文档"目录

另一种实现方法是将 ShellExec函数runas动词以执行提升的外部复制实用程序( copy xcopy robocopy ) .请参见 Inno设置-将组件注册为管理员(它运行regsvr32,但概念相同).


另一种选择是从提升的安装程序中执行非提升的过程,仅解决到原始用户文档文件夹的路径.

使用 ExecAsOriginalUser函数.

您必须通过两个帐户都可以访问的临时文件在安装程序之间交换路径.例如.可以在 {commondocs} 中找到文件 Inno安装程序始终安装在管理员的AppData目录中.

 [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;
 


相关讨论:

I'm using Inno Setup to install documents/files rather than an application, and this is primarily for Windows 7 users. As such my DestDir is based on {userdocs} so that all files will be installed in a folder below that user's Documents library.

The problem arises when I use the same installer to install a TTF font. This requires elevated privileges (admin or superuser). The problem I'm seeing is that if a non-admin user runs the install, they are correctly prompted via UAC for the admin/superuser password...but at that point the DestDir for the installation changes to the Admin documents folder rather than the user's documents folder. Is there any way to work around this or prevent this from happening?

Example, non-Admin account Fre has a documents path of:

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 {userdocs} and it works perfectly.

If I DO include the TTF font as part of the installation with same non-Admin user Fred, by the time the install is done {userdocs} has become

C:\Users\AdminUser\My Documents\ 

...which is not the intended result...just need Admin privileges for the font installation piece and need the files installed into the actual user's documents area.

Thanks.

解决方案

Create a child installer for the fonts, with the PrivilegesRequired=admin directive, that you will run from within the master non-elevated installer.

Master installer code will be like:

[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 should uninstall the child installer from the master uninstaller.

You may also want to make sure, the user did not run the master installer with administrator privileges explicitly. See my answer to How to write to the user's My Documents directory with installer when the user used 'Run As Administrator'.

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


Another option is to execute a non-elevated process, from the elevated installer, only to resolve the path to the original user documents folder.

Use the ExecAsOriginalUser function.

You have to exchange the path between the installers via some temporary file that is accessible to both accounts. E.g. a file in {commondocs}, as can be seen in the Inno Setup always installs into admin's AppData 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:

这篇关于Inno Setup-从需要特权的安装程序访问非特权帐户文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆