如何在需要时只提升权限?权限、如何在

2023-09-02 11:29:37 作者:Resign (认命)

此问题适用于Windows Vista的!

This question applies to Windows Vista!

我有正常工作没有管理权限的应用程序。有一个活动这确实需要管理权限,但我不希望启动应用程序本身具有更高的权限,当我知道大部分时候用户甚至不会使用该功能。

I have an application which normally works without administrative privileges. There is one activity which does need administrative privilege but I don't want to start the application itself with higher privileges when I know most of time user wont even be using that feature.

我的想法一定的方法,通过它我可以提高应用程序的权限对某些事件(如按钮的preSS)。例如:

I am thinking about certain method by which I can elevate the privileges of application on some event (such as press of a button). Example:

如果用户点击该按钮,那么他会被提示UAC对话框或同意。我怎样才能做到这一点?

If user clicks this button then he is prompted with UAC dialog or consent. How can I do this?

推荐答案

我不相信这是可以提升当前运行的进程。它是Windows Vista内置的管理员权限在启动时给一个过程,我明白了。如果你看一下,利用UAC的各种程序,你应该看到,他们其实每一个行政行为需要执行的时间推出一个单独的进程(任务管理器是其中之一,Paint.NET是另一种,后者实际上是一个.NET应用程序)。

I don't believe that it is possible to elevate the currently running process. It is built into Windows Vista that administrator privileges are given to a process upon startup, as I understand. If you look at various programs that utilise UAC, you should see that they actually launch a separate process each time an administrative action needs to be performed (Task Manager is one, Paint.NET is another, the latter being a .NET application in fact).

典型的解决这个问题是启动提升的进程的时候指定命令行参数(abatishchev的建议是这样做的一种方式),从而使启动过程只知道要显示一定的对话框,然后在此之后退出动作已经完成。因此,它应该很难被察觉到一个新的进程已经启动,然后退出用户,宁可看起来好像在同一个应用程序,一个新的对话框已被打开(特别是如果你一定两轮牛车做的主窗口提升的进程父进程的孩子)。如果您不需要用户界面高架接入,甚至更好。

The typical solution to this problem is to specify command line arguments when launching an elevated process (abatishchev's suggestion is one way to do this), so that the launched process knows only to display a certain dialog box, and then quit after this action has been completed. Thus it should hardly be noticeable to the user that a new process has been launched and then exited, and would rather appear as if a new dialog box within the same app has been opened (especially if you some hackery to make the main window of the elevated process a child of the parent process). If you don't need UI for the elevated access, even better.

有关UAC的Vista上进行充分的讨论,我建议你看this极通关于这个问题(code的例子是在C ++中,但我怀疑你需要使用WinAPI的和P / Invoke来完成大部分的东西在C#反正)文章。希望你现在至少可以看到正确的方法服用,但设计一个UAC兼容的程序是远离琐碎......

For a full discussion of UAC on Vista, I recommend you see this very through article on the subject (code examples are in C++, but I suspect you'll need to use the WinAPI and P/Invoke to do most of the things in C# anyway). Hopefully you now at least see the right approach to take, though designing a UAC compliant program is far from trivial...