使用ATL编写服务程序比较简单,ATL生成向导会自动生成,编译后运行命令:
注册:aaa.exe /Service
卸载服务:停止后运行命令aaa.exe /UnRegServer
重写Run函数,OnStop,OnShutDown,Install,RegisterAppId等函数
可以在Run个函数里面来实现自己的功能
HRESULT Run(int nShowCmd = SW_HIDE) throw()
{
HRESULT hr = S_OK;
m_pThread = NULL;
m_ThreadHandle = 0;
hr = __super::PreMessageLoop(nShowCmd);
if (hr == S_OK)
{
if (m_bService)
{
SetServiceStatus(SERVICE_RUNNING);
//Sleep(30000);
#ifdef DEBUG
//Sleep(50000);
#endif
m_pThread = AfxBeginThread(RUNTIME_CLASS(CDeviceTherad), 0, 0, CREATE_SUSPENDED, NULL);
if ( NULL != m_pThread )
{
m_ThreadHandle = m_pThread->m_hThread;
m_pThread->ResumeThread();
Sleep(1000);
}
m_pThread->PostThreadMessage(WM_START_MONITOR, WPARAM(m_pAppLog), NULL);
}
//进入消息循环,不停的处理消息,可能最后分发到Handler去处理,调用了OnShutdown等函数的。
__super::RunMessageLoop();
}
if (SUCCEED

通过ATL创建服务程序十分便捷,ATL向导能自动生成代码。注册服务命令为:aaa.exe /Service,卸载则用:aaa.exe /UnRegServer。在Run、OnStop、OnShutDown等函数中定制服务行为,确保在停止服务时释放资源,调整Install和RegisterAppId以控制服务启动方式。
最低0.47元/天 解锁文章
1193

被折叠的 条评论
为什么被折叠?



