头文件:
#ifndef _KServiceHost_H_
#define _KServiceHost_H_
#include <Windows.h>
#include <string>
#define KSST_STOP 0x0001
#define KSST_PAUSE 0x0002
#define KSST_SHUTDOWN 0x0004
#define KSST_SESSIONCHANGED 0x0008
class KServiceHost
{
public:
KServiceHost();
virtual ~KServiceHost();
int Create(LPCWSTR szServiceName, unsigned int uServiceSupportType);
virtual int OnServiceStart(){return 0;};
virtual int OnServiceStop(){return 0;};
virtual int OnServicePause(){return 0;};
virtual int OnServiceResume(){return 0;};
virtual void OnServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
virtual void OnServiceHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
public:
static void __stdcall KServiceHost::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
static void __stdcall KServiceHost::ServiceHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
private:
static KServiceHost *m_sServiceHost;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
SERVICE_STATUS m_serviceStatus;
unsigned int m_uServiceSupportType;
std::wstring m_strServiceName;
};
#endif
CPP文件:
#include "stdafx.h"
#include "KServiceHost.h"
KServiceHost* KServiceHost::m_sServiceHost = NULL;
KServiceHost::KServiceHost()
{
m_sServiceHost = this;
m_ServiceStatusHandle = NULL;
m_uServiceSupportType = 0;
memset(&m_serviceStatus, 0, sizeof(m_serviceStatus));
}
KServiceHost::~KServiceHost()
{
m_sServiceHost = NULL;
}
int KServiceHost::Create(LPCWSTR szServiceName, unsigned int uServiceSupportType)
{
int nReturn = 0;
m_strServiceName = szServiceName;
m_uServiceSupportType = uServiceSupportType;
m_serviceStatus.dwServiceType = SERVICE_WIN32;
m_serviceStatus.dwCurrentState= SERVICE_RUNNING;
if (uServiceSupportType & KSST_STOP)
m_serviceStatus.dwControlsAccepted |= SERVICE_ACCEPT_STOP;
if (uServiceSupportType & KSST_PAUSE)
m_serviceStatus.dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE;
if (uServiceSupportType & KSST_SHUTDOWN)
m_serviceStatus.dwControlsAccepted |= SERVICE_ACCEPT_SHUTDOWN;
if (uServiceSupportType & KSST_SESSIONCHANGED)
m_serviceStatus.dwControlsAccepted |= SERVICE_ACCEPT_SESSIONCHANGE;
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{L"KServiceHost", KServiceHost::ServiceMain},
{NULL, NULL}
};
dispatchTable[0].lpServiceName = (LPWSTR)m_strServiceName.c_str();
if (!StartServiceCtrlDispatcher(dispatchTable))
nReturn = ::GetLastError();
return nReturn;
}
void KServiceHost::OnServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
m_ServiceStatusHandle = RegisterServiceCtrlHandlerEx(m_strServiceName.c_str(), (LPHANDLER_FUNCTION_EX)KServiceHost::ServiceHandler, NULL);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
m_serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
return;
}
m_serviceStatus.dwCurrentState = SERVICE_RUNNING;
if (!SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus))
{
m_serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
return;
}
OnServiceStart();
}
void KServiceHost::OnServiceHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
{
m_serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
OnServiceStop();
break;
}
case SERVICE_CONTROL_SHUTDOWN:
{
m_serviceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
OnServiceStop();
break;
}
case SERVICE_CONTROL_PAUSE:
{
m_serviceStatus.dwCurrentState = SERVICE_PAUSED;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
OnServicePause();
break;
}
case SERVICE_CONTROL_CONTINUE:
{
m_serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(m_ServiceStatusHandle, &m_serviceStatus);
OnServiceResume();
break;
}
case SERVICE_CONTROL_SESSIONCHANGE:
break;
default:
break;
};
}
void __stdcall KServiceHost::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
m_sServiceHost->OnServiceMain(dwArgc, lpszArgv);
}
void __stdcall KServiceHost::ServiceHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
m_sServiceHost->OnServiceHandler(dwControl, dwEventType, lpEventData, lpContext);
}
使用示例代码:
#include "stdafx.h"
#include "KServiceHost.h"
KServiceHost g_serviceHost;
int _tmain(int argc, _TCHAR* argv[])
{
g_serviceHost.Create(L"KServiceHost", KSST_STOP | KSST_PAUSE | KSST_SHUTDOWN);
return 0;
}