以编程方式提升进程权限?
我正在尝试使用 InstallUtil.exe 安装服务,但通过 Process.Start
调用。 代码如下:
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
System.Diagnostics.Process.Start (startInfo);
其中 m_strInstallUtil
是“InstallUtil.exe”的完全限定路径和 exe,strExePath
是我的服务的完全限定路径/名称。
从提升的命令提示符运行命令行语法是有效的; 从我的应用程序运行(使用上面的代码)不会。 我假设我正在处理一些进程提升问题,那么我将如何在提升状态下运行我的进程? 我是否需要查看 ShellExecute
来实现此目的?
这一切都发生在 Windows Vista 上。 我正在提升到管理员权限的 VS2008 调试器中运行该进程。
我也尝试设置 startInfo.Verb = "runas";
但它似乎没有解决问题。
I'm trying to install a service using InstallUtil.exe but invoked through Process.Start
. Here's the code:
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
System.Diagnostics.Process.Start (startInfo);
where m_strInstallUtil
is the fully qualified path and exe to "InstallUtil.exe" and strExePath
is the fully qualified path/name to my service.
Running the command line syntax from an elevated command prompt works; running from my app (using the above code) does not. I assume I'm dealing with some process elevation issue, so how would I run my process in an elevated state? Do I need to look at ShellExecute
for this?
This is all on Windows Vista. I am running the process in the VS2008 debugger elevated to admin privilege.
I also tried setting startInfo.Verb = "runas";
but it didn't seem to solve the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
还有另一种方法可以启动需要提升的进程,而无需使用 Windows 任务计划程序在父进程中进行提升。 基本上,您手动创建一个任务并在
常规
选项卡中选中以最高权限运行
。 可以使用任务计划程序库以编程方式运行此任务。注意:无法动态创建此类任务,因为这需要提升您的进程。
使用 TaskScheduler 库查找并运行任务如下所示:
或者,您不必手动创建任务可以以编程方式创建它(需要提升一次),然后无需启动它。
There is an alternative way of starting a process that requires elevation without having to have elevation in the parent process using the windows
Task Scheduler
. Basically you manually create a task and checkRun with highest privileges
in theGeneral
tab. This task can be run programmatically using a Task Scheduler library.Note: It is not possible to dynamically create such a task as this would require your process to be elevated.
Using the TaskScheduler library finding and running the task looks like this:
Or, instead of creating the task manually you can create it programmatically (requires elevation once) and later start it without.
您应该使用模拟来提升状态。
完成后,不要忘记撤消模拟的上下文。
You should use Impersonation to elevate the state.
Don't forget to undo the impersonated context when you are done.
我知道这是一篇非常老的帖子,但我只是想分享我的解决方案:
注意:如果 VisualStudio 已经运行提升,那么 UAC 对话框将不会显示,要测试它,请运行 exe bin 文件夹。
i know this is a very old post, but i just wanted to share my solution:
NOTE: If VisualStudio is already running Elevated then the UAC dialog won't show up, to test it run the exe from the bin folder.
这将在没有 UAC 的情况下完成 - 无需启动新进程。 如果运行用户是管理员组的成员,就我的情况而言。
This will do it without UAC - no need to start a new process. If the running user is member of Admin group as for my case.
根据文章 < em>Chris Corio:教您的应用程序与 Windows Vista 用户帐户控制完美配合,MSDN 杂志,2007 年 1 月,仅
ShellExecute
检查嵌入的清单并提示用户如果需要的话,可以进行提升,而CreateProcess
和其他 API 则不需要。 希望能帮助到你。另请参阅:同一篇文章作为.chm。
According to the article Chris Corio: Teach Your Apps To Play Nicely With Windows Vista User Account Control, MSDN Magazine, Jan. 2007, only
ShellExecute
checks the embedded manifest and prompts the user for elevation if needed, whileCreateProcess
and other APIs don't. Hope it helps.See also: same article as .chm.
您可以通过将 startInfo 对象的 Verb 属性设置为“runas”来指示应使用提升的权限启动新进程,如下所示:
这将导致 Windows 表现得好像该进程是通过“以管理员身份运行”从资源管理器启动的” 菜单命令。
这确实意味着 UAC 提示将会出现,并且需要用户确认:如果这是不可取的(例如,因为它会发生在一个漫长的过程中),您将需要运行整个主机进程通过创建并嵌入应用程序清单 (UAC) 提升权限以要求“highestAvailable”执行级别:这将导致应用程序启动后立即出现 UAC 提示,并导致所有子进程以提升的权限运行,而无需额外提示。
You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:
This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.
This does mean the UAC prompt will come up and will need to be acknowledged by the user: if this is undesirable (for example because it would happen in the middle of a lengthy process), you'll need to run your entire host process with elevated permissions by Create and Embed an Application Manifest (UAC) to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.
此代码将以上所有内容放在一起,并使用管理员权限重新启动当前的 wpf 应用程序:
更新:应用程序清单方式是首选:
右键单击 Visual Studio 中的项目,添加新的应用程序清单文件,更改文件,以便您设置 requireAdministrator,如上所示。
原始方式的问题:如果将重启代码放在app.xaml.cs OnStartup中,即使调用了Shutdown,它仍然可能会短暂启动主窗口。 如果 app.xaml.cs init 未运行,我的主窗口就会爆炸,并且在某些竞争条件下它会执行此操作。
This code puts the above all together and restarts the current wpf app with admin privs:
Update: The app manifest way is preferred:
Right click project in visual studio, add, new application manifest file, change the file so you have requireAdministrator set as shown in the above.
A problem with the original way: If you put the restart code in app.xaml.cs OnStartup, it still may start the main window briefly even though Shutdown was called. My main window blew up if app.xaml.cs init was not run and in certain race conditions it would do this.