我有以下简单的服务程序:
using System.Diagnostics;
using System.ServiceProcess;
namespace BasicService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
Verb = "runas",
UserName = "jdoe",
Password = "XXXXXXX".ConvertToSecureString(),
Domain = "abc.com",
UseShellExecute =false,
FileName = "notepad.exe"
};
Process.Start(processStartInfo);
}
protected override void OnStop()
{
}
}
}
我正在使用它作为我的服务安装程序:
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace BasicService
{
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
private readonly ServiceProcessInstaller _process;
private readonly ServiceInstaller _service;
public ProjectInstaller()
{
_process = new ServiceProcessInstaller {Account = ServiceAccount.LocalSystem};
_service = new ServiceInstaller
{
ServiceName = "BasicService",
Description = "Just a testing service.",
StartType = ServiceStartMode.Automatic,
};
Installers.Add(_process);
Installers.Add(_service);
}
}
}
如果我在没有动词,用户名,密码,域和useshellexecute指定的情况下运行此服务,一切都运行得很花哨。只要我如上所述指定这些值,我就会得到以下结果:
无法启动服务。 System.ComponentModel.Win32Exception (0x80004005):访问被拒绝 System.Diagnostics.Process.StartWithCreateProcess(的ProcessStartInfo startInfo)在System.Diagnostics.Process.Start()at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)at BasicService.Service1.OnStart(String [] args)in C:\ BasicService \ BasicService \ Service1.cs:第24行at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(对象 状态)
有什么想法吗?
答案 0 :(得分:2)
自Windows Vista起,服务无法简单地显示ui或与用户交互 http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
因此,要从您的服务运行GUI应用程序,您需要使用CreateProcessAsUser,它不能直接在.NET中使用。所以你必须依赖Pinvoke,有点类似于这里描述的