简体   繁体   English

如何在代码中配置我的Windows服务以访问桌面?

[英]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: 要启用访问权限,我必须执行以下步骤:

  1. Opened the administrative tool "Services" 打开管理工具“服务”

  2. Right clicked on my service and had to select "properties" 右键单击我的服务,不得不选择“属性”

  3. 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 if you have multiple users logged in (fast user switching)? 如果您有多个用户登录(快速用户切换)怎么办?
  • what if you have multiple RDP sessions? 如果您有多个RDP会话怎么办?

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.

相关问题 如何从Windows CE移动设备访问文件到我的桌面应用程序? - How Can I access File from Windows CE mobile to my desktop application? 如何在从Windows Phone 7调用的WCF服务中配置会话? - How can I configure session in my WCF service called from windows phone 7? 如何实现 Windows 桌面服务和 Windows 桌面应用程序之间的通信? - How can I achieve communication between a windows desktop service and a windows desktop application? 如何为 Windows Phone 模拟器配置网络访问权限? - How can I configure network access for the Windows Phone emulator? 如何以最简单的方式将现有的 C# Windows 桌面窗体项目转换为 Windows 服务。 (我想完全废弃的表单 UI 部分) - How can I convert my existing C# Windows Desktop Form project in the easiest way to a Windows service. (The form UI part I want to scrap completely) 如何切换到Windows服务的桌面 - How to switch to the Desktop of Windows Service 如何让我的Windows服务以特定间隔运行? - How can I get my windows service to run at a specific interval? 我怎么知道我的Windows服务何时无法运行 - how i can know when my Windows service is not working 如何在运行时获取Windows服务的名称? - How can I get the name of my Windows Service at runtime? 如何检测何时可以安全升级Windows服务? - How can I detect when it is safe to upgrade my windows service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM