[英]How can I configure my windows service in the code to access the desktop?
I have created an windows service. 我创建了一个Windows服务。 I want to open some windows based application from this service.
我想从这个服务打开一些基于Windows的应用程序。
But my windows service is unable to start desktop applications. 但我的Windows服务无法启动桌面应用程序。 To enable the access I had to do the following steps:
要启用访问权限,我必须执行以下步骤:
Opened the administrative tool "Services" 打开管理工具“服务”
Right clicked on my service and had to select "properties" 右键单击我的服务,不得不选择“属性”
Then in the "Log On" tab, selected "Allow service to interact with desktop". 然后在“登录”选项卡上,选择“允许服务与桌面交互”。
After that my service can open desired windows based processes. 之后我的服务可以打开所需的基于Windows的进程。
Can I configure my windows service in the code (C#) to access the desktop so that I won't have to change the access permission manually after installation? 我可以在代码(C#)中配置我的Windows服务来访问桌面,这样我就不必在安装后手动更改访问权限了吗?
In .NET you can override the OnCommited
method of the service installer class to configure the service to access the desktop. 在.NET中,您可以覆盖服务安装程序类的
OnCommited
方法,以配置访问桌面的服务。 The code will look as follows: 代码如下所示:
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceProcessInstaller serviceProcessInstaller;
private ServiceInstaller serviceInstaller;
public ProjectInstaller()
{
InitializeComponent();
// adjust configuration to whatever is needed
serviceInstaller = new ServiceInstaller();
serviceInstaller.ServiceName = "My Service";
serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Manual;
this.Installers.Add(serviceInstaller);
serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
serviceProcessInstaller.Password = null;
serviceProcessInstaller.Username = null;
this.Installers.Add(serviceProcessInstaller);
}
protected override void OnCommitted(IDictionary savedState)
{
base.OnCommitted(savedState);
// The following code sets the flag to allow desktop interaction
// for the service
//
using (RegistryKey ckey =
Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CurrentControlSet\Services\My Service", true))
{
if (ckey != null && ckey.GetValue("Type") != null)
{
ckey.SetValue("Type", (((int)ckey.GetValue("Type")) | 256));
}
}
}
}
Just... don't. 只是......不要。 That isn't the job of a service.
这不是服务的工作。 For this job you should be using a user app (perhaps in their startup) that (if necessary) talks to a service via IPC.
对于这个工作,你应该使用一个用户的应用程序(或许在他们的启动)通过IPC是(如有必要) 会谈的服务。 I'm believe the plan is to make the UI unavailable from services at some point (Vista onwards? I stopped doing service<=>desktop a long time ago).
我相信计划是在某些时候使用户界面无法使用UI(从Vista开始?我很久以前就停止了服务<=>桌面)。
For considerations: 考虑因素:
What you are proposing only really scales to 1, and possibly not event that if you consider that "session 0" is reserved for admin use on some systems (so the interactive user isn't necessarily on session 0). 你提议的只是真正扩展为1,并且可能不是如果你认为“会话0”被保留用于某些系统上的管理员使用的事件(因此交互式用户不一定在会话0上)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.