通过编程来让企业服务以服务程序方式运行
2007-08-24 11:02
387 查看
我们要在这篇文章中将实现的功能:
如上图荧光笔画的地方所示:
我们需要通过编写服务程序的安装程序,把企业服务上面 Run application as NT Service 选择框的进行选中操作。
阅读本文基础:
如果你对企业服务不是很了解,可以阅读我之前写的一系列跟企业服务有关的博客。我书写的跟企业服务有关的系列文章可以访问以下链接。
http://blog.joycode.com/ghj/category/1320.aspx
如果你对编程控制企业服务根本不了解,需要首先阅读我之前写的这篇博客:
编程控制企业服务的行为
本文是在上述文章基础上的加深。
正文
编程控制企业服务的行为 这篇博客中我讲到,我们可以通过遍历 COMAdminCatalogCollection 来寻找到我们需要操作企业服务的某个属性,然后更新这个属性。这种做法适用于大多数编程操作企业服务属性。但是我们上面这个需求就无法用这种方式来作了。
我们遍历 COMAdminCatalogCollection 可以更新的属性时候,我们在微软提供的可操作属性列表(如下链接可以看到)中并没有找到可以设置 Run application as NT Service 的选项。
http://msdn2.microsoft.com/en-us/library/ms686107.aspx
只能看到一个可能跟这个需求有关的属性:
ServiceName
The service name corresponding to the application configured to run as an NT service. If this value is NULL, the application is not configured to run as an NT service. Otherwise, the configuration information for the service can be found by using the service name.
Access: ReadOnly
Type: String
Default: ""
Platform Requirements: Windows XP, Windows Server 2003
这个属性是只读的,我们又没法设置。
如何解决这个问题呢?
很简单,这个需求不是通过属性来指定的,而是通过 COMAdminCatalog 类的 CreateServiceForApplication 方法。
简单来说,我们就是要安装代码中有以下代码:
COMAdmin.COMAdminCatalog ca = new COMAdmin.COMAdminCatalogClass();
ca.CreateServiceForApplication(ApplicationName, NTServerName, "SERVICE_DEMAND_START", "SERVICE_ERROR_NORMAL", "", null, null, false);
CreateServiceForApplication 函数的定义如下:
HRESULT CreateServiceForApplication(
BSTR bstrApplicationIDOrName,
BSTR bstrServiceName,
BSTR bstrStartType,
BSTR bstrErrorControl,
BSTR bstrDependencies,
BSTR bstrRunAs,
BSTR bstrPassword,
VARIANT_BOOL bDesktopOk);
每个参数介绍如下:
bstrApplicationIDOrName
应用ID或者应用名字,我们安装这个企业服务后,在服务程序列表中,这个值就是我们看到的 NT Service 的Name 就是这个值。
bstrServiceName
服务名字,我们安装这个企业服务后,在服务程序列表中,这个值就是我们看到的 NT Service 的 Description 就是这个值。
bstrStartType
服务开始的几种情况,这里可以是下面几个值
SERVICE_BOOT_START, SERVICE_SYSTEM_START, SERVICE_AUTO_START, SERVICE_DEMAND_START, and SERVICE_DISABLED.
bstrErrorControl
服务错误发生时的情况,可以是以下几个值
SERVICE_ERROR_IGNORE, SERVICE_ERROR_NORMAL, SERVICE_ERROR_SEVERE, and SERVICE_ERROR_CRITICAL.
其他几个参数一般都比较固定,我直接Copy MSDN的说明。
bstrDependencies [in] A list of dependencies for the service. There are two possible formats for the string: a standard null-delimited, double-null-terminated string (exactly as documented for CreateService); or a script-friendly list of service names separated by "/" (an invalid character to have in a service name). The rpcss service is implicit in this parameter and does not need to be specified. bstrRunAs [in] The user name to run this service as. This may be NULL to indicate that it should run as Local Service. bstrPassword [in] The password for the system user account. This must be NULL if the service is configured to run as Local Service. bDesktopOk [in] Indicates whether or not the service should be allowed to interact with the desktop. This parameter is valid only when the service is marked as Local Service and must be FALSE otherwise.
参考资料:
Register your Enterprise Service App as NT Service
如上图荧光笔画的地方所示:
我们需要通过编写服务程序的安装程序,把企业服务上面 Run application as NT Service 选择框的进行选中操作。
阅读本文基础:
如果你对企业服务不是很了解,可以阅读我之前写的一系列跟企业服务有关的博客。我书写的跟企业服务有关的系列文章可以访问以下链接。
http://blog.joycode.com/ghj/category/1320.aspx
如果你对编程控制企业服务根本不了解,需要首先阅读我之前写的这篇博客:
编程控制企业服务的行为
本文是在上述文章基础上的加深。
正文
编程控制企业服务的行为 这篇博客中我讲到,我们可以通过遍历 COMAdminCatalogCollection 来寻找到我们需要操作企业服务的某个属性,然后更新这个属性。这种做法适用于大多数编程操作企业服务属性。但是我们上面这个需求就无法用这种方式来作了。
我们遍历 COMAdminCatalogCollection 可以更新的属性时候,我们在微软提供的可操作属性列表(如下链接可以看到)中并没有找到可以设置 Run application as NT Service 的选项。
http://msdn2.microsoft.com/en-us/library/ms686107.aspx
只能看到一个可能跟这个需求有关的属性:
ServiceName
The service name corresponding to the application configured to run as an NT service. If this value is NULL, the application is not configured to run as an NT service. Otherwise, the configuration information for the service can be found by using the service name.
Access: ReadOnly
Type: String
Default: ""
Platform Requirements: Windows XP, Windows Server 2003
这个属性是只读的,我们又没法设置。
如何解决这个问题呢?
很简单,这个需求不是通过属性来指定的,而是通过 COMAdminCatalog 类的 CreateServiceForApplication 方法。
简单来说,我们就是要安装代码中有以下代码:
COMAdmin.COMAdminCatalog ca = new COMAdmin.COMAdminCatalogClass();
ca.CreateServiceForApplication(ApplicationName, NTServerName, "SERVICE_DEMAND_START", "SERVICE_ERROR_NORMAL", "", null, null, false);
CreateServiceForApplication 函数的定义如下:
HRESULT CreateServiceForApplication(
BSTR bstrApplicationIDOrName,
BSTR bstrServiceName,
BSTR bstrStartType,
BSTR bstrErrorControl,
BSTR bstrDependencies,
BSTR bstrRunAs,
BSTR bstrPassword,
VARIANT_BOOL bDesktopOk);
每个参数介绍如下:
bstrApplicationIDOrName
应用ID或者应用名字,我们安装这个企业服务后,在服务程序列表中,这个值就是我们看到的 NT Service 的Name 就是这个值。
bstrServiceName
服务名字,我们安装这个企业服务后,在服务程序列表中,这个值就是我们看到的 NT Service 的 Description 就是这个值。
bstrStartType
服务开始的几种情况,这里可以是下面几个值
SERVICE_BOOT_START, SERVICE_SYSTEM_START, SERVICE_AUTO_START, SERVICE_DEMAND_START, and SERVICE_DISABLED.
bstrErrorControl
服务错误发生时的情况,可以是以下几个值
SERVICE_ERROR_IGNORE, SERVICE_ERROR_NORMAL, SERVICE_ERROR_SEVERE, and SERVICE_ERROR_CRITICAL.
其他几个参数一般都比较固定,我直接Copy MSDN的说明。
bstrDependencies [in] A list of dependencies for the service. There are two possible formats for the string: a standard null-delimited, double-null-terminated string (exactly as documented for CreateService); or a script-friendly list of service names separated by "/" (an invalid character to have in a service name). The rpcss service is implicit in this parameter and does not need to be specified. bstrRunAs [in] The user name to run this service as. This may be NULL to indicate that it should run as Local Service. bstrPassword [in] The password for the system user account. This must be NULL if the service is configured to run as Local Service. bDesktopOk [in] Indicates whether or not the service should be allowed to interact with the desktop. This parameter is valid only when the service is marked as Local Service and must be FALSE otherwise.
参考资料:
Register your Enterprise Service App as NT Service
ICOMAdminCatalog2::CreateServiceForApplication
相关文章推荐
- 通过编程来让企业服务以服务程序方式运行
- 通过编程来让企业服务以服务程序方式运行
- 通过编程来让企业服务以服务程序方式运行
- 通过编程来让企业服务以服务程序方式运行
- 通过编程来让企业服务以服务程序方式运行
- 通过编程来让企业服务以服务程序方式运行
- 在Ubuntu上以服务方式运行Java程序
- 使用apache daemon让java程序在unix系统上以服务方式运行
- 以Windows服务方式运行ASP.NET Core程序【转载】
- 使用apache daemon让java程序在unix系统上以服务方式运行
- 使用apache daemon让java程序在unix系统上以服务方式运行
- [VB.NET]VB.net有窗体程序如何以系统服务方式运行?
- 使用AutoIt通过桌面图标点击的方式运行程序
- Q:我怎样通过编程的方式来退出我的iOS程序呢?
- 初学OpenCV3,运行编程入门上读取并播放视频的原版程序,编译通过但是没有任何输出,已按网上博主各种方法试,还是没有结果,大家看怎么解决?
- 让程序 exe 以系统服务的方式运行
- 判断 php 程序是通过什么方式运行的 (浏览器,还是命令行)
- 只允许允许一个程序实例,即使是通过虚拟桌面方式连接过来的,也是只允许一个人运行。
- VS 通过 附加进程的方式来调试 C#服务程序
- <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序