130 lines
3.8 KiB
C++

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2007 - 2008.
//
// File: BaseService.h
//
// Contents: Provides a base class for a service that will exist as
// part of a service application. BaseService must be
// derived when creating a new service class.
//
// Notes: To use this class you should derived this class and
// implement the following methods:
// ServiceInitialization - initialization of the service.
// _ServiceCtrlHandler - take care of calling the callback
// function specified for each
// control operations.
// ServiceMainLoop - Service main application
//
//----------------------------------------------------------------------------
#ifndef _BASE_SERVICE_H
#define _BASE_SERVICE_H
#include <ace/String_Base.h>
#include <ace/OS.h>
#include <ace/Synch.h>
#include <windows.h>
#include <winsvc.h>
#include <crtdbg.h>
#include <string>
//------------------
// FW declaration
//------------------
class BaseEventLog;
using namespace std;
#define APPLICATION "Application"
#define EVENT_LOG_CATEGORY_COUNT 7
class BaseService
{
public:
// Constructors & Destructors.
// BaseService(void);
BaseService(LPCTSTR pszServiceName = APPLICATION);
virtual ~BaseService(void) {
delete eventLogger;
};
public:
// Functions:
// ----------
// Initialize code for the service.
// Note: This function has to be called first after the declaration of the
// derived class.
int InitializeService( DWORD dwServiceType,
DWORD dwControlsAccepted,
DWORD dwWaitHint);
// Starts the service control dispatch.
DWORD StartDispatcher();
// IMPLEMENT THIS: pure virtual method for initialization of the service.
virtual DWORD ServiceInitialization(DWORD argc,
LPTSTR *argv,
DWORD *specificError) = 0;
// Performs all the operations for the service start.
void WINAPI _ServiceMain(DWORD argc, LPTSTR *argv);
// ServiceMain is the function registeref in the service register function.
// It uses a static variable that contains the "this" pointer of the class
// and calls the _ServiceMain function.
static void WINAPI ServiceMain(DWORD argc,LPTSTR *argv);
// Handles the control code passed in the Opcode parameter and then calls the
// SetServiceStatus function to update the service's status.
static DWORD WINAPI ServiceCtrlHandler(
DWORD dwControl,
DWORD dwEventType,
LPVOID lpEventData,
LPVOID lpContext);
// IMPLEMENT THIS: Handler function - take care of calling the callback function
// specified for each control operations.
virtual DWORD WINAPI _ServiceCtrlHandler(
DWORD dwControl,
DWORD dwEventType,
LPVOID lpEventData,
LPVOID lpContext) = 0;
// This is where the service does its work.
virtual DWORD ServiceMainLoop() = 0;
// Set the service name. Use this method if the service name is not set using the
// constructors.
void SetServicerName(char *pszServiceName);
// Update the service status.
BOOL UpdateService(DWORD dwCurrentState);
// Write an error message to the Windows Event log
void WriteToErrorLog(const char *message);
// Gets the windows system error description from a windows system
// error code
void getFormatMessage(DWORD errorCode, string &errorBuf);
protected:
// Data members.
std::basic_string<TCHAR> m_ServiceName; // Service name
SERVICE_STATUS_HANDLE m_ServiceHandle; // Handle to the registered service.
SERVICE_TABLE_ENTRY m_ServiceDispatcher[2]; // Service table entry handler.
SERVICE_STATUS m_ServiceStatus; // The status of the service.
static BaseService *m_pService; // Store 'this' pointer for use in the static functions.
bool m_InitFlag; // Is initialized?
SERVICE_DESCRIPTION desc;
BaseEventLog * eventLogger;
ACE_Mutex mutex;
};
#endif //_BASE_SERVICE_H