如何在Windows服务程序中以Admin身份创建进程? [英] How to create a process as Admin in my windows service program?

查看:142
本文介绍了如何在Windows服务程序中以Admin身份创建进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有

我已经在win7上编写了一个服务程序,并且需要在该服务中以Admin身份创建一个进程.这是我所做的:
1.获取登录用户的访问令牌
2.调用CreateProcessAsUser()函数创建该进程.

现在我可以获得正确的访问令牌,但是当我调用createprocessasuser时,它将失败,并显示错误消息请求的操作需要提升".

无论如何,如何在Windows服务程序中以Admin身份创建进程?

谢谢.

all

I''ve wrote a service program on win7, and I need to create a process as Admin in that service. Here is what I did:
1.Get the access token of the log on user
2.Call CreateProcessAsUser() function to create that process.

Now I can get the right access token, but when I call createprocessasuser, it fails with error message "Requested action requires elevated"。

How can I create a process as Admin in my windows service program anyway?

Thanks.

推荐答案

ProcessStartInfo processStartInfo = new ProcessStartInfo
                                        { FileName = "calc.exe" };

if (Environment.OSVersion.Version.Major >= 6)
    processStartInfo.Verb = "runas"; // Windows Server 2008, Vista or higher;
                                     // User will be prompted

processStartInfo.Arguments = "";
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = true;

Process process = null;
try
{
    process = Process.Start(processStartInfo);
}
catch (Exception ex)
{
    // Exception handling here
}
finally
{
    if (process != null)
        process.Dispose();
}



但是,除非您的服务具有管理权限,否则我不建议您这样做,因为系统将提示用户允许该应用程序在Windows Server 2008,Vista和7上以提升的权限运行.



However I would not suggest you do this, unless your service runs with administrative rights, as the user will be prompted to allow the application to be run with elevated permissions on Windows Server 2008, Vista and 7.


您可以模拟管理员用户执行某些管理任务.
you can impersonate the admin user to do Some Admin Task.


这篇关于如何在Windows服务程序中以Admin身份创建进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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