一个创建服务的类
2008-12-26 22:25
351 查看
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;
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dulinbo/EntryImages/20081229/Prop1.JPG)
The second is to set the service to be interactive just by adding the attribute SERVICE_INTERACTIVE_PROCESS when creating the service.
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dulinbo/EntryImages/20081229/Prop2.JPG)
The third is to add the dependency of the service:
/// <summary>
/// 获取配置信息
/// </summary>
BOOL CServiceApp::GetConfigInfoFromFile(TCHAR* pszCfgFile)
{
// //获取配置文件路径
// ......
//
// //读取配置信息
// //读服务依赖的服务
//
// //将依赖的服务名组合成以'/0'分隔的字符串
// .......
m_pszDependencies = _T("SampleService");
return TRUE;
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/dulinbo/EntryImages/20081229/Prop3.JPG)
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.
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.
相关文章推荐
- 一步一个脚印学习WCF之一WCF概要(中)之客户端与服务-WCF服务的创建与调用Demo
- 把 Nginx 创建为 Windows 的一个服务
- SpringCloud微服务系列(2): 创建一个基于Springboot的RESTFul服务
- 创建一个最简单的Linux随机启动服务
- SC Create 创建一个Windows系统服务
- (转)SC Create 创建一个Windows系统服务
- 使用PHP来简单的创建一个RPC服务
- 创建一个简单的代码发布服务
- 为一个包创建消息(.msg)和服务(.srv)
- 一个完整的Windows 服务从创建到安装卸载
- tomcat创建一个windows服务
- 创建一个既可作为普通exe(双击能运行),又可作为服务运行的程序
- 如何为一个服务进程创建监控,即使出core也能自动重启
- SCVMM2012R2 服务模版系列(四)创建一个开箱即用的Web应用程序服务模版 推荐
- Window Service 创建:在后台执行一个服务,可定时做一些操作,如轮询数据库,定时发邮件
- 把 Nginx 创建为 Windows 的一个服务
- 把 Nginx 创建为 Windows 的一个服务
- 创建一个Map服务并在Flex中调用
- 「六」创建一个带 weblogic 服务的基础镜像
- 把 Nginx 创建为 Windows 的一个服务