111 lines
3.7 KiB
C++
111 lines
3.7 KiB
C++
#include <service/GenericService.hpp>
|
|
#include <service/ServiceStatus.hpp>
|
|
|
|
GenericService::GenericService(void)
|
|
: mhServiceHandle(0), mState(Stopped)
|
|
{
|
|
}
|
|
|
|
GenericService::GenericService(const GenericService &/*someGenericService*/)
|
|
{ // private implementation
|
|
}
|
|
|
|
GenericService::~GenericService()
|
|
{
|
|
}
|
|
|
|
GenericService &GenericService::operator=(const GenericService &/*someGenericService*/)
|
|
{ // private implementation
|
|
return *this;
|
|
}
|
|
|
|
bool GenericService::startService(const String &strServiceName)
|
|
{
|
|
SERVICE_TABLE_ENTRY serviceEntry[2];
|
|
bool result;
|
|
|
|
::memset(&serviceEntry,0,sizeof(serviceEntry));
|
|
mServiceName=strServiceName;
|
|
mEventLog.open(mServiceName);
|
|
serviceEntry[0].lpServiceName=(char*)mServiceName;
|
|
serviceEntry[0].lpServiceProc=getServiceMainBase();
|
|
result=::StartServiceCtrlDispatcher(&serviceEntry[0])?true:false;
|
|
if(!result){::OutputDebugString("<GenericService::startService> StartServiceCtrlDispatcher(FAILED)\n");return false;}
|
|
::OutputDebugString("<GenericService::startService> StartServiceCtrlDispatcher(OK)\n");
|
|
return true;
|
|
}
|
|
|
|
void GenericService::svcMain(DWORD dwArg,LPTSTR *lpszArg)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_START_PENDING,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,WaitHint);
|
|
mhServiceHandle=::RegisterServiceCtrlHandler(mServiceName,getServiceHandlerBase());
|
|
if(!mhServiceHandle)return;
|
|
serviceMain(dwArg,lpszArg);
|
|
}
|
|
|
|
void GenericService::svcHandler(DWORD dwControl)
|
|
{
|
|
switch(dwControl)
|
|
{
|
|
case SERVICE_CONTROL_STOP :
|
|
::OutputDebugString("<GenericService::serviceHandler> (OK)\n");
|
|
ctrlStop();
|
|
break;
|
|
case SERVICE_CONTROL_PAUSE :
|
|
::OutputDebugString("<GenericService::serviceHandler> (OK)\n");
|
|
ctrlPause();
|
|
break;
|
|
case SERVICE_CONTROL_CONTINUE :
|
|
::OutputDebugString("<GenericService::serviceHandler> (OK)\n");
|
|
ctrlContinue();
|
|
break;
|
|
case SERVICE_CONTROL_INTERROGATE :
|
|
::OutputDebugString("<GenericService::serviceHandler> (OK)\n");
|
|
ctrlInterrogate();
|
|
break;
|
|
case SERVICE_CONTROL_SHUTDOWN :
|
|
::OutputDebugString("<GenericService::serviceHandler> (OK)\n");
|
|
ctrlShutdown();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// virtuals
|
|
void GenericService::serviceMain(DWORD dwArg,LPTSTR *lpszArg)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_RUNNING,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
while(Stopped!=state()){::Sleep(1000);}
|
|
return;
|
|
}
|
|
|
|
void GenericService::ctrlStop(void)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_STOPPED,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
}
|
|
|
|
void GenericService::ctrlPause(void)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_PAUSED,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
}
|
|
|
|
void GenericService::ctrlContinue(void)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_RUNNING,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
}
|
|
|
|
void GenericService::ctrlShutdown(void)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,SERVICE_STOPPED,SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
}
|
|
|
|
void GenericService::ctrlInterrogate(void)
|
|
{
|
|
ServiceStatus serviceStatus(SERVICE_WIN32_OWN_PROCESS,state(),SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN,NO_ERROR,NO_ERROR,0,0);
|
|
setServiceStatus(serviceStatus);
|
|
}
|