- File-New-Project
![vs2010 <wbr>编写windows <wbr>service <wbr>服务程序 vs2010 <wbr>编写windows <wbr>service <wbr>服务程序](https://i-blog.csdnimg.cn/blog_migrate/7670bba361044134bb09afaa22436d06.jpeg)
- 选择alt工程模板,输入名字TestService
![vs2010 <wbr>编写windows <wbr>service <wbr>服务程序 vs2010 <wbr>编写windows <wbr>service <wbr>服务程序](https://i-blog.csdnimg.cn/blog_migrate/d3f79b92eaee7d7b88c9335b1220116b.jpeg)
- 点ok后,出现下面界面
![vs2010 <wbr>编写windows <wbr>service <wbr>服务程序 vs2010 <wbr>编写windows <wbr>service <wbr>服务程序](https://i-blog.csdnimg.cn/blog_migrate/78286f554e3850a7b95108caf2eeb68f.jpeg)
- 直接点Next,出现下一界面选Service。
![vs2010 <wbr>编写windows <wbr>service <wbr>服务程序 vs2010 <wbr>编写windows <wbr>service <wbr>服务程序](https://i-blog.csdnimg.cn/blog_migrate/5e226544b47056da84a06731bb9756f6.jpeg)
到此,创建windows service工程完成。在TestService.cpp中增加下面的代码(红色)
// TestService.cpp : Implementation of WinMain
#include "stdafx.h"
#include "resource.h"
#include "TestService_i.h"
#include <stdio.h>
using namespace ATL;
class CTestServiceModule : publicATL::CAtlServiceModuleT< CTestServiceModule,IDS_SERVICENAME >
{
public :
DECLARE_LIBID(LIBID_TestServiceLib)
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_TESTSERVICE,"{25AF9FF6-DEA2-4E6C-8EBC-B3BB81B74226}")
HRESULT InitializeSecurity()throw()
{
// TODO : CallCoInitializeSecurity and provide the appropriate security settingsfor your service
// Suggested - PKT LevelAuthentication,
// Impersonation Level ofRPC_C_IMP_LEVEL_IDENTIFY
// and an appropiate Non NULLSecurity Descriptor.
return S_OK;
}
HRESULTRegisterAppId(bool bService = false) throw ();
HRESULT PreMessageLoop(intnShowCmd) throw();
HRESULT PostMessageLoop()throw();
void OnStop() throw();
void OnPause() throw();
void OnContinue()throw();
};
HRESULTCTestServiceModule::RegisterAppId(bool bService ) throw ()
{
HRESULT hr = S_OK;
BOOL res = __super::RegisterAppId(bService);
if (bService)
{
if (IsInstalled())
{
SC_HANDLEhSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
SC_HANDLEhService = NULL;
if (hSCM ==NULL)
{
hr= AtlHresultFromLastError();
}
else
{
hService= ::OpenService(hSCM, m_szServiceName,SERVICE_CHANGE_CONFIG);
if(hService != NULL)
{
::ChangeServiceConfig(hService,SERVICE_NO_CHANGE,
SERVICE_AUTO_START,//修改服务为自动启动
NULL,NULL, NULL, NULL, NULL, NULL, NULL,
m_szServiceName);// 通过修改资源IDS_SERVICENAME 修改服务的显示名字
SERVICE_DESCRIPTIONDescription;
TCHARszDescription[1024];
ZeroMemory(szDescription,1024);
ZeroMemory(&Description,sizeof (SERVICE_DESCRIPTION));
lstrcpy(szDescription,_T("测试服务描述信息" ));
Description.lpDescription= szDescription;
::ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION, &Description);
::CloseServiceHandle(hService);
}
else
{
hr= AtlHresultFromLastError();
}
::CloseServiceHandle(hSCM);
}
}
}
return hr;
}
HRESULTCTestServiceModule::PreMessageLoop(int nShowCmd) throw()
{
// 让暂停继续按钮可以使用
m_status.dwControlsAccepted =m_status.dwControlsAccepted |SERVICE_ACCEPT_PAUSE_CONTINUE;
HRESULT hr =__super::PreMessageLoop(nShowCmd);
// 微软Bug
if (hr == S_FALSE)
hr = S_OK;
//这里添加自己的初始化代码...
if (SUCCEEDED(hr))
{
// 这个状态一定要修改,否则会出现1053错误,
// 这个错误我花了很多时间才搞定
SetServiceStatus(SERVICE_RUNNING);
}
return hr;
}
HRESULTCTestServiceModule::PostMessageLoop() throw()
{
HRESULT hr = __super::PostMessageLoop();
if (FAILED(hr))
return hr;
//这里添加自己的清除代码
return hr;
}
void CTestServiceModule::OnStop()throw()
{
__super::OnStop();
SetServiceStatus(SERVICE_STOPPED);
}
void CTestServiceModule::OnPause()throw()
{
__super::OnPause();
SetServiceStatus(SERVICE_PAUSED);
}
void CTestServiceModule::OnContinue()throw()
{
__super::OnContinue();
SetServiceStatus(SERVICE_RUNNING);
}
CTestServiceModule _AtlModule;
//
extern "C" int WINAPI _tWinMain(HINSTANCE , HINSTANCE ,
LPTSTR, int nShowCmd)
{
return _AtlModule.WinMain(nShowCmd);
}
编译,打开cmd窗口,进入生成的TestService.exe目录执行
即可启动服务