一个创建服务的类

本文介绍了一个用于创建Windows服务的实用类,该类不仅作为示例展示了如何使用相关API来创建服务,而且可以直接应用于实际项目中。通过继承并覆盖Run()和Stop纯虚函数,开发者可以轻松实现自己的服务逻辑。此外,文章还详细介绍了如何修改服务描述、设置服务为交互模式及添加服务依赖等高级功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

One of my projects was to develop a data server running as a service. I used a class downloaded from CodeGuru. After using it for some months, I found it very stable, so I want to share it with you.

The class is not just an example to show how to use those functions to create a service. Instead, it can be used in your formal project. And with this class it is very simple to create an NT service. Just derive your own class and override the "Run()" and "Stop" pure virtual members.The class accepts a set of command-line parameters. You can see those parameters in the function CNTService :: RegisterService.
Based on the original sample, I add some new features which are used to set the properties of the service.The first is to change the description of the service:
/// <summary>
///   修改服务描述
/// </summary>
BOOL CServiceApp::ConfigureService(SC_HANDLE schService)
{
 m_pszDescription = _T("MFC test");
 SERVICE_DESCRIPTION Dscrp;
 Dscrp.lpDescription = m_pszDescription;
 ChangeServiceConfig2(schService,
  SERVICE_CONFIG_DESCRIPTION,
  &(Dscrp));
 
 return TRUE;
}

 

The second is to set the service to be interactive just by adding the attribute SERVICE_INTERACTIVE_PROCESS when creating the service.

 

The third is to add the dependency of the service:
/// <summary>
///  获取配置信息
/// </summary>
BOOL CServiceApp::GetConfigInfoFromFile(TCHAR* pszCfgFile)
{
//  //获取配置文件路径
//  ......
//
//  //读取配置信息
//  //读服务依赖的服务
//  
//  //将依赖的服务名组合成以'/0'分隔的字符串
//  .......   
  m_pszDependencies = _T("SampleService");
 return TRUE;
}

In gerneral, the class encapsulates the detail of creating a service well.You only have to override the "Run()" and "Stop" pure virtual members to complete your task.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值