CSDN专家博客精华版

为人民服务!
  首页  :: 新随笔  :: 管理

通过编程来让企业服务以服务程序方式运行

Posted on 2007-12-17 11:19  csdnexpert  阅读(118)  评论(0编辑  收藏  举报

我们要在这篇文章中将实现的功能:

企业服务配置作为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

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1757182