作者: Jim Wang 公众号: 巴博萨船长

Inno Setup 如何以管理员的身份相关

1. 如何以管理员身份运行一个Batch脚本文件

If you are using [Run] section then make sure you use runascurrentuser flag (If this flag is specified, the spawned process will inherit Setup/Uninstall’s user credentials (typically, full administrative privileges))

Else there are three ways how to run applications programatically (recommended way):

1
2
3
4
5
function Exec(const Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ResultCode: Integer): Boolean;

function ShellExec(const Verb, Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ErrorCode: Integer): Boolean;

function ShellExecAsOriginalUser(const Verb, Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ErrorCode: Integer): Boolean;

You should use Exec() or ShellExec() because they open the specified file or performs another action specified by Verb, using the same credentials as Setup/Uninstall.

But none of mentioned ways will work if your installer is not running in elevated mode. So make sure the UAC window will appear before installer starts:

In section [Setup] use directive PrivilegesRequired

Valid values:

1
none`, `poweruser`, `admin`, or `lowest

Use admin to ensure appropriate credentials.

2. 各个合法值在不同版本中的表现

合法值为:

  • none, poweruser, admin, or lowest

默认值为:Default value:

  • admin

描述Description:

The effect of this directive depends on which version of Windows the user is running:

此参数的效果取决于用户运行的Windows版本:

On Windows Vista and later:

This directive affects whether elevated rights are requested (via a User Account Control dialog) when the installation is started.

该指令影响安装开始时是否要求提升权限(通过“用户帐户控制”对话框)。

When set to admin (the default) or poweruser, Setup will always run with administrative privileges. If Setup was started by an unprivileged user, Windows will ask for the password to an account that has administrative privileges, and Setup will then run under that account.

当设置为admin(默认)或超级用户时,安装程序将始终以管理特权运行。 如果安装程序是由非特权用户启动的,则Windows将要求输入具有管理特权的帐户的密码,然后安装程序将在该帐户下运行。

When set to none, Setup will only run with administrative privileges if it was started by a member of the Administrators group. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

如果设置为none,则安装程序仅由Administrators组的成员启动时才具有管理特权。 除非您确定安装将在非特权帐户上成功运行,否则不要使用此设置。

When set to lowest, Setup will not request to be run administrative privileges even if it was started by a member of the Administrators group. Additionally, the uninstall info root key will always be HKEY_CURRENT_USER, and the “common” forms of the Shell Folder constants are mapped to the “user” forms, even if administrative privileges are available. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

设置为最低时,即使安装程序是由Administrators组的成员启动的,它也不会请求运行管理权限。 此外,卸载信息的根密钥将始终为HKEY_CURRENT_USER,并且即使具有管理特权,Shell文件夹常量的“公共”形式也将映射到“用户”形式。 除非您确定安装将在非特权帐户上成功运行,否则不要使用此设置。

On Windows NT/2000/XP/2003

This directive specifies the minimum user privileges required to run the installation.

该指令指定运行安装所需的最低用户特权。

When set to admin (the default), Setup will only run if the user is a member of the Administrators group. Otherwise, it will display the following message and exit: “You must be logged in as an administrator when installing this program.”

当设置为admin(默认值)时,仅当用户是Administrators组的成员时,安装程序才会运行。 否则,它将显示以下消息并退出:“安装此程序时,您必须以管理员身份登录。”

When set to poweruser, Setup will only run if the user is a member of the Administrators or Power Users groups. Otherwise, it will display the following message and exit: “You must be logged in as an administrator or as a member of the Power Users group when installing this program.”

设置为poweruser时,仅当用户是Administrators或Power Users组的成员时,安装程序才会运行。 否则,它将显示以下消息并退出:“安装此程序时,您必须以管理员或Power Users组成员的身份登录。”

When set to none Setup will not check the user’s group membership. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

设置为none时,安装程序将不检查用户的组成员身份。 除非您确定安装将在非特权帐户上成功运行,否则不要使用此设置。

When set to lowest Setup will not check the user’s group membership. Additionally, the uninstall info root key will always be HKEY_CURRENT_USER, and the “common” forms of the Shell Folder constants are mapped to the “user” forms, even if administrative privileges are available. Do not use this setting unless you are sure your installation will run successfully on unprivileged accounts.

设置为最低时,安装程序将不检查用户的组成员身份。 此外,卸载信息的根密钥将始终为HKEY_CURRENT_USER,并且即使具有管理特权,Shell文件夹常量的“公共”形式也将映射到“用户”形式。 除非您确定安装将在非特权帐户上成功运行,否则不要使用此设置。

On Windows 95/98/Me

This directive has no effect on these versions of Windows.

该指令对这些版本的Windows无效。

以管理员身份安装和非管理员身份安装的区别

An installation can run in one of two modes: administrative or non administrative. Which mode is selected is specified by the PrivilegesRequired and PrivilegesRequiredOverridesAllowed [Setup] section directives.

In administrative install mode:

  • The {group} folder is created in the All Users profile.
  • The “auto” form of the directory and Shell Folder constants is mapped to the “common” form.
  • The HKA and uninstall info root keys will be HKEY_LOCAL_MACHINE.

In non administrative install mode:

  • The {group} folder is created in the current user’s profile.
  • The “auto” form of the directory and Shell Folder constants is mapped to the “user” form.
  • The HKA and uninstall info root keys will be HKEY_CURRENT_USER.

Notes:

Regardless of the version of Windows, if the installation is running in administrative install mode then you should be careful about making any per-user area changes: such changes may not achieve what you are intending. The compiler will warn you about this, which can be disabled using UsedUserAreasWarning.

无论Windows的版本如何,如果安装均以管理安装模式运行,则应谨慎进行每个用户区域的更改:此类更改可能无法实现您的预期。 编译器将对此警告,可以使用UsedUserAreasWarning禁用。

If the installation is running in non administrative install mode, but administrative privileges are available anyway then Setup or the [Code] section might still make use of these privileges. For this reason the uninstaller will always be marked as requiring administrative privileges in this case, just as if the installation was running in administrative install mode.

如果安装程序在非管理安装模式下运行,但是仍然具有管理特权,则安装程序或[Code]部分可能仍会使用这些特权。 因此,在这种情况下,卸载程序将始终被标记为需要管理特权,就像安装在管理安装模式下运行一样。


版权声明:
文章首发于 Jim Wang's blog , 转载文章请务必以超链接形式标明文章出处,作者信息及本版权声明。