使用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);
}
//进入消息循环,不停的处理消息&