UAC 以编程方式请求许可
我有一个使用 COM 文件的应用程序。有时这个文件还没有被 regsvr32 注册,我想编写一个函数,如果无法加载该文件,它会尝试注册它并重试。为此,我需要管理员权限,所以我想询问是否有一种方法可以在这些时候向用户显示 UAC“以管理员身份运行”问题,以便有权注册该文件并将其关闭(如果可能,否则它将在下一次应用程序执行时“正常”运行)。 现在我只是计划启动另一个程序,其清单设置为以管理员身份运行,但我已经看到一些安装无需询问管理员权限即可运行,并且当进度条开始时,它们会向用户显示 UAC 问题,所以我想做类似的事情。 是否有 Win32 API 函数或库可以做到这一点?
谢谢
I have an application that uses an COM file. Sometimes this file has not been registered by regsvr32 and i'd like to program a function that if the file cannot be loaded it tries to register it and retry. To do so i'd need admin privileges, so i'd like to ask if there's a way to show the UAC "run as administrator" question to the user at those times to have the rights to register the file and than switch it off (if possible, if not it'd be run "normal" at the next application execution).
Now i'm just planning to launch another program with a manifest set to run it as admin but i've seen some installations that run without asking admin permission and when the progress bar is starting they show the UAC question to the user, so i'd like to do something like that.
Is there a Win32 API function or library to do that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的做法是正确的。无法提升进程。你要么被提升,要么没有,并且在整个过程的生命周期中你都会保持这种状态。为了实现你想要的,你必须启动一个不同的进程,并以提升的方式启动它。您的主线应用程序保持非提升状态。当第二个进程启动时,将出现 UAC 提示。为了获得良好的用户体验,您应该有一个按钮或菜单项来进行启动,并且该项目上应该有一个盾牌。这样用户就不会感到惊讶。如果您以编程方式检测到需要启动,至少先显示一个消息框,显示“我检测到您需要安装 XYZ,您同意吗?”并在“确定”上加一个盾牌,在“取消”上不加盾牌,如果他们单击“确定”,则启动所显示的应用程序。这将触发 UAC 提示,并且用户会期待它。
Your approach is correct. It is not possible to elevate a process. You are launched elevated or you are not, and you stay that way for the lifetime of the process. To achieve what you want, you must launch a different process, and launch it as elevated. Your mainline app stays non-elevated. The UAC prompt will appear when the second process launches. For the sake of a good user experience you should have a button or menu item to do the launching, and that item should have a shield on it. That way the user is not surprised. If you detect programmatically that you need to launch, at least do a message box first saying "I've detected that you need to install XYZ, are you ok with that?" and put a shield on OK, no shield on Cancel, and if they click OK then launch the manifested application. This will trigger a UAC prompt and the user will be expecting it.