以编程方式提升流程特权? [英] Elevating process privilege programmatically?

查看:93
本文介绍了以编程方式提升流程特权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用InstallUtil.exe安装服务,但通过 Process.Start 进行了调用。代码如下:

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);

其中 m_strInstallUtil 是完全限定的路径, exe到 InstallUtil.exe和 strExePath 是我服务的完全限定路径/名称。

where m_strInstallUtil is the fully qualified path and exe to "InstallUtil.exe" and strExePath is the fully qualified path/name to my service.

来自提升的命令提示符的命令行语法有效;从我的应用程序运行(使用上面的代码)没有。我假设我正在处理一些流程提升问题,那么如何在提升状态下运行流程?我需要查看 ShellExecute 吗?

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?

这一切都在Windows Vista上实现。我正在VS2008调试器中运行提升为管理员权限的进程。

This is all on Windows Vista. I am running the process in the VS2008 debugger elevated to admin privilege.

我还尝试设置 startInfo.Verb = runas; ,但似乎并不能解决问题。

I also tried setting startInfo.Verb = "runas"; but it didn't seem to solve the problem.

推荐答案

您可以指示新流程应以通过将startInfo对象的Verb属性设置为'runas'来提高权限,如下所示:

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:

startInfo.Verb = "runas";

这将导致Windows表现为该进程已通过以管理员身份运行从资源管理器启动菜单命令。

This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.

这确实意味着将出现UAC提示,并且需要用户确认:如果不希望这样做(例如,因为它会在一个漫长的过程),您需要通过创建并嵌入应用程序清单(UAC)以要求 highestAvailable执行级别:这将导致UAC提示在您的应用程序启动后立即出现,并导致所有子进程以提升的权限运行权限,无需其他提示。

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.

编辑:我看到您刚刚编辑了问题,指出 runas不适用于您。确实很奇怪,确实如此(对我来说,在多个生产应用程序中也是如此)。但是,通过嵌入清单来要求父进程以提升的权限运行,当然应该可以。

I see you just edited your question to state that "runas" didn't work for you. That's really strange, as it should (and does for me in several production apps). Requiring the parent process to run with elevated rights by embedding the manifest should definitely work, though.

这篇关于以编程方式提升流程特权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆