您的位置:首页 > 其它

一个创建服务的类

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;
}



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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: