Delphi:需要时提示 UAC 提升
我们需要在运行时更改 HKEY_LOCAL_MACHINE 的一些设置。
如果需要在运行时,是否可以提示 uac 提升,或者我是否必须启动第二个提升的进程来完成“肮脏的工作”?
We need to change some settings to the HKEY_LOCAL_MACHINE at runtime.
Is it possible to prompt for uac elevation if needed at runtime, or do I have to launch a second elevated process to do 'the dirty work'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法“提升”现有流程。 UAC 下的提升进程具有不同的令牌,具有不同的 LUID、不同的强制完整性级别和不同的组成员身份。 这种级别的更改无法在正在运行的进程中完成 - 如果发生这种情况,这将是一个安全问题。
您需要启动第二个提升的进程来完成这项工作,或者创建一个在提升的 dllhost 中运行的 COM 对象。
http://msdn.microsoft.com/en-us/library/bb756922.aspx 给出了示例“RunAsAdmin”函数和“CoCreateInstanceAsAdmin”函数。
编辑:我刚刚在你的标题中看到“Delphi”。 我列出的所有内容显然都是本机的,但如果 Delphi 提供对类似 ShellExecute 的功能的访问,您应该能够从链接中调整代码。
You can't "elevate" an existing process. Elevated processes under UAC have a different token with a different LUID, different mandatory integrity level, and different group membership. This level of change can't be done within a running process - and it would be a security problem if that could happen.
You need to launch a second process elevated that would do the work or by creating a COM object that runs in an elevated dllhost.
http://msdn.microsoft.com/en-us/library/bb756922.aspx gives an example "RunAsAdmin" function and a "CoCreateInstanceAsAdmin" function.
EDIT: I just saw "Delphi" in your title. Everything I listed is obviously native, but if Delphi provides access to ShellExecute-like functionality you should be able to adapt the code from the link.
我会以提升的身份重新启动自己,传递命令行参数来指示您想要执行的提升的操作。 然后,您可以直接跳转到适当的表格,或者只保存您的 HKLM 内容。
Microsoft 建议的另一个解决方案是在进程外创建 COM 对象(使用专门创建的 CoCreateInstanceAsAdmin 函数)。 我不喜欢这个想法,因为你必须编写并注册一个 COM 对象。
注意:没有“CoCreateInstanceAsAdmin”API 调用。 这只是一些浮动的代码。 这是我偶然发现的 Dephi 版本。 它显然是基于当通常隐藏的代码内部调用 CoGetObject 时,在类 guid 字符串前添加“Elevation:Administrator!new:”前缀的技巧:
One其他问题: 如何处理在 Windows XP 中以标准用户身份运行的人?
i would relaunch yourself as elevated, passing command line parameters indicating what elevated thing you want to do. You can then jump right to the appropriate form, or just save your HKLM stuff.
The other Microsoft suggested solution is to create an COM object out of process (using the specially created CoCreateInstanceAsAdmin function). i don't like this idea because you have to write and register a COM object.
Note: There is no "CoCreateInstanceAsAdmin" API call. It's just some code floating around. Here's the Dephi version i stumbled around for. It is apparently based on the trick of prefixing a class guid string with the "Elevation:Administrator!new:" prefix when normally hidden code internally calls CoGetObject:
One other question: How do you handle someone running as standard user in Windows XP?
即用型代码示例:
使用示例:
以及支持单元本身:
A sample of ready-to-use code:
Usage example:
And support unit itself:
通常,将文本“Setup”或“Install”放在 EXE 名称中的某个位置就足以使 Windows 自动以提升的权限运行,并且如果您正在编写一个安装实用程序,则非常值得这样做,因为它很容易做到。
我现在在 Windows 7 上遇到问题,当未以管理员身份登录时,并且在手动运行时必须使用右键单击以管理员身份运行(通过 Wise 安装向导运行程序仍然可以)
我看到Delphi 10.1 Berlin 在项目选项 | 下有一个非常易于使用的新选项 应用。 只需勾选启用管理员权限,清单就为您完成,如此简单!
注意。 确保您仅通过单独的安装程序进行此类更改,始终以提升的权限运行应用程序可能会导致其他问题,例如电子邮件,不再选择默认邮件配置文件。
编辑:2018 年 1 月:自从 2017 年 8 月写下这个答案以来,似乎已经出现了很多 Windows 更新,现在要求用户右键单击并以管理员身份运行几乎所有内容,甚至在使用 Wise 构建的安装 exe 上也是如此。 如果不以管理员身份运行,甚至 Outlook 也无法正确安装。 似乎根本没有更多的自动化提升。
Usually, putting the text "Setup" or "Install" somewhere in your EXE name is enough to make Windows run with elevated privileges automatically, and is well worth doing if it is a setup utility you are writing, as it's so easy to do.
I am now running into problems though on Windows 7, when not logged in as an Administrator, and am having to use the right-click Run As Administrator when running manually (running the program via Wise installation wizard is still fine)
I see though that Delphi 10.1 Berlin has a very easy to use new option under Project Options | Application. Just tick Enable Administrator Privileges, and the manifest is done for you, so easy!
NB. make sure you only do these kind of changes via a separate setup program, running your application with elevated privileges all the time can cause problems with other things, for example e-mail, where the default mail profile no longer gets picked up.
Edit: Jan 2018: since writing this answer in August 2017, it seems a lot of Windows updates have come out, that now require the user to right-click and Run As Administrator on just about everything, even on installation exe's built with Wise. Even Outlook is no longer installing properly without running as administrator. There is no more automated elevation at all it seems.