[英]How to run a separate process as a domain user with a 'local system' service?
我有以下簡單的服務程序:
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):在Basic.Service1的System.Diagnostics.Process.Start(ProcessStartInfo startInfo)上的System.Diagnostics.Process.Start()處的System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒絕訪問C:\\ BasicService \\ BasicService \\ Service1.cs中的.OnStart(String [] args):System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)中的第24行
有任何想法嗎?
從Windows Vista開始,服務無法簡單地顯示ui或與用戶交互。
http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx
因此,要從您的服務運行GUI應用程序,您需要使用CreateProcessAsUser,它不能直接在.NET中使用。 所以你必須依賴Pinvoke,有點類似於這里所描述的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.