带有GUI的Windows服务 [英] Windows service with gui

查看:155
本文介绍了带有GUI的Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了Windows服务,现在我正尝试为服务创建GUI,通过它我可以启动,停止,重新启动和暂停服务.我的要求是我想在服务中添加一个带有四个按钮的表单".
如何在Windows服务中添加表单以及如何在表单和服务之间进行交互?

I was created a windows service, now I am trying to create a GUI for a service through which i am able to start, stop, restart and pause service. My requirement is "I want to add a form in service,with four buttons."
How to add form in windows service and how to interact between form and service?
Is it possible to add GUI to windows service?

推荐答案

如Abhinav所述,您无法将UI添加到Windows Service.

您可以做的是开发一个单独的Windows应用程序以与您的服务进行交互.要实现所需的操作,此应用程序应使用类System.ServiceProcess.ServiceController,请参见:
http://msdn.microsoft.com/en-us/library/system.serviceprocess. servicecontroller.aspx [ ^ ].

—SA
As Abhinav explained, you can not add UI to Windows Service.

What you can do is developing a separate Windows application to interact with your Service. To implement the operations you need, this application should use the class System.ServiceProcess.ServiceController, please see:
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx[^].

—SA


默认情况下,服务使用非交互式窗口站,并且无法与用户进行交互.但是,交互式服务可以显示用户界面并接收用户输入.

警告:在较高安全性上下文中运行的服务(例如LocalSystem帐户)不应在交互式桌面上创建窗口,因为在交互式桌面上运行的任何其他应用程序都可以与此窗口进行交互.这会将服务公开给登录用户执行的任何应用程序.另外,作为LocalSystem运行的服务不应通过调用OpenWindowStation或GetThreadDesktop函数来访问交互式桌面.

要创建交互式服务,请在调用CreateService函数时执行以下操作:
1.为lpServiceStartName参数指定NULL,以在LocalSystem帐户的上下文中运行服务.

2.指定SERVICE_INTERACTIVE_PROCESS标志.

若要确定服务是否正在作为交互式服务运行,请调用GetProcessWindowStation函数以检索窗口站的句柄,并调用GetUserObjectInformation函数以测试窗口站是否具有WSF_VISIBLE属性.
但是,请注意,以下注册表项包含一个值NoInteractiveServices,该值控制SERVICE_INTERACTIVE_PROCESS的效果:

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Windows

NoInteractiveServices值默认为0,这意味着带有SERVICE_INTERACTIVE_PROCESS的服务被允许交互式运行.当NoInteractiveServices设置为非零值时,此后启动的任何服务都不允许以交互方式运行,无论它是否具有SERVICE_INTERACTIVE_PROCESS.

重要说明:所有服务都在终端服务会话0中运行.因此,如果交互式服务显示用户界面,则只有连接到会话0的用户才能看到它.因为无法保证交互式用户已连接到会话0,请勿将服务配置为在终端服务下或在支持快速用户切换的系统上作为交互式服务运行(使用终端服务实现快速用户切换).

您只需要在一个线程中执行Application.Run.

因此可以创建一个交互式服务,但通常不值得付出努力.

最好的问候
Espen Harlinn
By default, services use a noninteractive window station and cannot interact with the user. However, an interactive service can display a user interface and receive user input.

Caution: Services running in an elevated security context, such as the LocalSystem account, should not create a window on the interactive desktop because any other application that is running on the interactive desktop can interact with this window. This exposes the service to any application that a logged-on user executes. Also, services that are running as LocalSystem should not access the interactive desktop by calling the OpenWindowStation or GetThreadDesktop function.

To create an interactive service, do the following when calling the CreateService function:
1.Specify NULL for the lpServiceStartName parameter to run the service in the context of the LocalSystem account.

2.Specify the SERVICE_INTERACTIVE_PROCESS flag.

To determine whether a service is running as an interactive service, call the GetProcessWindowStation function to retrieve a handle to the window station, and the GetUserObjectInformation function to test whether the window station has the WSF_VISIBLE attribute.

However, note that the following registry key contains a value, NoInteractiveServices, that controls the effect of SERVICE_INTERACTIVE_PROCESS:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows

The NoInteractiveServices value defaults to 0, which means that services with SERVICE_INTERACTIVE_PROCESS are allowed to run interactively. When NoInteractiveServices is set to a nonzero value, no service started thereafter is allowed to run interactively, regardless of whether it has SERVICE_INTERACTIVE_PROCESS.

Important: All services run in Terminal Services session 0. Therefore, if an interactive service displays a user interface, it is visible only to the user who connected to session 0. Because there is no way to guarantee that the interactive user is connected to session 0, do not configure a service to run as an interactive service under Terminal Services or on a system that supports fast user switching (fast user switching is implemented using Terminal Services).

You need to execute the Application.Run in one thread only.

So it''s possible to create an interactive service, but usually not worth the effort.

Best regards
Espen Harlinn


无法将GUI添加到Windows服务.
但是,您可以允许UI与现有服务进行通信.

您始终可以动态添加控件-因此,要添加Windows窗体,请参见此处 [ ^ ].
It is not possible to add GUI to windows service.
However, you can allow the UI to communicate with the existing service.

You can always add a control to dynamically - so for adding a Windows Form, see here[^].


这篇关于带有GUI的Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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