#include #include 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(" StartServiceCtrlDispatcher(FAILED)\n");return false;} ::OutputDebugString(" 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(" (OK)\n"); ctrlStop(); break; case SERVICE_CONTROL_PAUSE : ::OutputDebugString(" (OK)\n"); ctrlPause(); break; case SERVICE_CONTROL_CONTINUE : ::OutputDebugString(" (OK)\n"); ctrlContinue(); break; case SERVICE_CONTROL_INTERROGATE : ::OutputDebugString(" (OK)\n"); ctrlInterrogate(); break; case SERVICE_CONTROL_SHUTDOWN : ::OutputDebugString(" (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); }