You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Inno Setup 5:如何在[Code]段手动调用管理员安装模式?

Inno Setup 5.x 提权后仍保持非管理员安装模式的解决方法

场景

使用Inno Setup 5.5.6(u)制作安装包,因兼容旧系统必须采用5.x版本。已通过Martin Prikryl的第二个方案实现逻辑:默认触发UAC提权请求,用户点击“否”则切换为非提权方式运行。

问题

非管理员组账户运行安装包,通过UAC输入管理员凭证完成提权后,Inno Setup仍维持非管理员安装模式——卸载项、桌面图标仅对输入凭证的管理员账户可见;虽程序可正常安装至ProgramFiles目录(IsAdminLoggedOn返回True),但其他用户(包括执行安装的受限账户)无法访问桌面图标或卸载程序。

已尝试的无效方案

  • PrivilegesRequiredOverridesAllowed:5.x版本不支持该指令
  • PrivilegesRequired=admin或PrivilegesRequired=none:无法捕获用户拒绝提权的场景并提供非提权安装路径
  • 命令行参数/ALLUSERS:在5.x版本中无效果
  • Function IsAdminInstallMode:编译时提示“未知标识符”
  • 创建两个子安装程序:因已嵌套一个安装程序,不愿进一步增加嵌套层级
  • 预处理器方案:PrivilegesRequired为静态设置,运行时无法通过[Code]段修改

可行解决方案(适配Inno Setup 5.x)

Inno Setup 5.x无法在运行时直接修改PrivilegesRequired,但可通过以下方式手动模拟管理员全局安装模式:

1. 强制切换全局安装状态

在[Code]段中,检测到提权成功(IsAdminLoggedOn返回True)时,手动指定全局安装路径并写入全局注册表项:

procedure InitializeSetup();
begin
  if IsAdminLoggedOn then
  begin
    // 勾选全局安装选项
    WizardForm.AllUsersRadio.Checked := True;
    WizardForm.CurrentUserRadio.Checked := False;
    
    // 切换注册表视图(按需适配64位系统)
    SetRegView(rvRegistry64);
    
    // 覆盖默认用户路径为公共路径
    WizardDirValue := ExpandConstant('{commonpf}\你的程序目录');
    DesktopFolder := ExpandConstant('{commondesktop}');
    StartMenuFolder := ExpandConstant('{commonstartmenu}');
    ProgramsFolder := ExpandConstant('{commonprograms}');
    
    // 设置全局卸载项参数
    UninstallDisplayName := ExpandConstant('{cm:UninstallProgram,你的程序名}');
    UninstallDir := ExpandConstant('{commonpf}\你的程序目录');
    
    // 写入全局卸载注册表项
    WriteRegStr(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}', 'DisplayName', UninstallDisplayName);
    WriteRegStr(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}', 'UninstallString', ExpandConstant('"{uninstallexe}"'));
    
    // 删除可能存在的用户特定卸载项
    DeleteRegKey(HKEY_CURRENT_USER, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}');
  end;
end;

2. 调整组件/任务的安装范围

在定义组件或任务时,确保Flags参数包含common(而非默认的user),强制资源安装到公共位置:

[Components]
Name: "main"; Description: "主程序"; Types: full compact; Flags: common
Name: "desktopicon"; Description: "桌面图标"; Types: full; Flags: common

[Tasks]
Name: "desktopicon"; Description: "创建桌面快捷方式"; GroupDescription: "附加任务:"; Flags: common

3. 验证安装模式

在安装过程中添加日志验证,确认已切换到全局模式:

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    if IsAdminLoggedOn then
    begin
      if RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Uninstall\{#SetupSetting("AppId")}') then
        Log('已成功切换到管理员全局安装模式');
    end;
  end;
end;

注意事项

  • 需替换代码中的你的程序目录、你的程序名为实际内容
  • 根据程序架构调整注册表视图(rvRegistry64或rvRegistry32)

内容的提问来源于stack exchange,提问作者Dragodraki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 07:34:56