Windows 服务(六)
ATL写个例子
创建ATL项目,选择Service(Exe)
代码引用自:http://blog.163.com/charlie-lei@126/blog/static/11374392420109238175788/
修改代码,加入
//修改服务名称和描述
HRESULT RegisterAppId(bool bService = false) throw()
{
HRESULT hr = S_OK;
BOOL res = __super::RegisterAppId(bService);
if( !bService )
return hr;
if( !IsInstalled() )
return hr;
SC_HANDLE hSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
SC_HANDLE hService = NULL;
if (hSCM == NULL)
{
hr = AtlHresultFromLastError();
return hr;
}
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_DESCRIPTION Description;
TCHAR szDescription[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;
}
//初始化代码
HRESULT PreMessageLoop(int nShowCmd) throw()
{
LogEvent( _T("进入 PreMessageLoop") );
HRESULT hr = __super::PreMessageLoop(nShowCmd);
if (SUCCEEDED(hr))
{
//启动主线程
DWORD dwId = 0;
m_hMainThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)CMainServiceThread::_main_service_thread, &_mainServiceThread, 0, &dwId );
}
return hr;
}
//消息循环
void RunMessageLoop( ) throw()
{
LogEvent( _T("进入 RunMessageLoop") );
SetServiceStatus(SERVICE_RUNNING);
__super::RunMessageLoop();
}
//卸载服务
HRESULT PostMessageLoop() throw()
{
LogEvent( _T("进入 PostMessageLoop") );
HRESULT hr = __super::PostMessageLoop();
if (SUCCEEDED(hr))
{
//停止服务线程
TerminateThread( m_hMainThread, 0 );
CloseHandle(m_hMainThread);
}
return hr;
}
private:
HANDLE m_hMainThread;
特别注意的是:
在 stdafx.h 中,要加入 #define _ATL_NO_COM_SUPPORT
可以看出服务主要是由CAtlServiceModuleT来完成的,PreMessageLoop和PostMessageLoop是服务开始和结束的时候回调,而run中启动了一个消息循环,
上面的代码没有利用这个循环,另外启动了一个线程来执行服务内容
ATL写个例子
创建ATL项目,选择Service(Exe)
代码引用自:http://blog.163.com/charlie-lei@126/blog/static/11374392420109238175788/
修改代码,加入
//修改服务名称和描述
HRESULT RegisterAppId(bool bService = false) throw()
{
HRESULT hr = S_OK;
BOOL res = __super::RegisterAppId(bService);
if( !bService )
return hr;
if( !IsInstalled() )
return hr;
SC_HANDLE hSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
SC_HANDLE hService = NULL;
if (hSCM == NULL)
{
hr = AtlHresultFromLastError();
return hr;
}
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_DESCRIPTION Description;
TCHAR szDescription[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;
}
//初始化代码
HRESULT PreMessageLoop(int nShowCmd) throw()
{
LogEvent( _T("进入 PreMessageLoop") );
HRESULT hr = __super::PreMessageLoop(nShowCmd);
if (SUCCEEDED(hr))
{
//启动主线程
DWORD dwId = 0;
m_hMainThread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)CMainServiceThread::_main_service_thread, &_mainServiceThread, 0, &dwId );
}
return hr;
}
//消息循环
void RunMessageLoop( ) throw()
{
LogEvent( _T("进入 RunMessageLoop") );
SetServiceStatus(SERVICE_RUNNING);
__super::RunMessageLoop();
}
//卸载服务
HRESULT PostMessageLoop() throw()
{
LogEvent( _T("进入 PostMessageLoop") );
HRESULT hr = __super::PostMessageLoop();
if (SUCCEEDED(hr))
{
//停止服务线程
TerminateThread( m_hMainThread, 0 );
CloseHandle(m_hMainThread);
}
return hr;
}
private:
HANDLE m_hMainThread;
特别注意的是:
在 stdafx.h 中,要加入 #define _ATL_NO_COM_SUPPORT
可以看出服务主要是由CAtlServiceModuleT来完成的,PreMessageLoop和PostMessageLoop是服务开始和结束的时候回调,而run中启动了一个消息循环,
上面的代码没有利用这个循环,另外启动了一个线程来执行服务内容