2017-05-26 04:48:44 +08:00
|
|
|
/****************************** Module Header ******************************\
|
|
|
|
* Module Name: ServiceBase.h
|
|
|
|
* Project: CppWindowsService
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
2021-03-11 02:06:03 +08:00
|
|
|
*
|
|
|
|
* Provides a base class for a service that will exist as part of a service
|
|
|
|
* application. CServiceBase must be derived from when creating a new service
|
2017-05-26 04:48:44 +08:00
|
|
|
* class.
|
2021-03-11 02:06:03 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* This source is subject to the Microsoft Public License.
|
|
|
|
* See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
|
|
|
|
* All other rights reserved.
|
2021-03-11 02:06:03 +08:00
|
|
|
*
|
|
|
|
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
|
2017-05-26 04:48:44 +08:00
|
|
|
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
\***************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <string>
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
class CServiceBase {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
// Register the executable for a service with the Service Control Manager
|
|
|
|
// (SCM). After you call Run(ServiceBase), the SCM issues a Start command,
|
|
|
|
// which results in a call to the OnStart method in the service. This
|
|
|
|
// method blocks until the service has stopped.
|
|
|
|
static bool Run(CServiceBase& service);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Service object constructor. The optional parameters (fCanStop,
|
|
|
|
// fCanShutdown and fCanPauseContinue) allow you to specify whether the
|
|
|
|
// service can be stopped, paused and continued, or be notified when
|
|
|
|
// system shutdown occurs.
|
|
|
|
CServiceBase(char* pszServiceName, bool fCanStop = true, bool fCanShutdown = true, bool fCanPauseContinue = false);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Service object destructor.
|
|
|
|
virtual ~CServiceBase(void);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Stop the service.
|
|
|
|
void Stop();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
protected:
|
2021-03-11 02:06:03 +08:00
|
|
|
// When implemented in a derived class, executes when a Start command is
|
|
|
|
// sent to the service by the SCM or when the operating system starts
|
|
|
|
// (for a service that starts automatically). Specifies actions to take
|
|
|
|
// when the service starts.
|
|
|
|
virtual void OnStart(DWORD dwArgc, LPSTR* lpszArgv) {}
|
|
|
|
|
|
|
|
// When implemented in a derived class, executes when a Stop command is
|
|
|
|
// sent to the service by the SCM. Specifies actions to take when a
|
|
|
|
// service stops running.
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void OnStop() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// When implemented in a derived class, executes when a Pause command is
|
|
|
|
// sent to the service by the SCM. Specifies actions to take when a
|
|
|
|
// service pauses.
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void OnPause() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// When implemented in a derived class, OnContinue runs when a Continue
|
|
|
|
// command is sent to the service by the SCM. Specifies actions to take
|
|
|
|
// when a service resumes normal functioning after being paused.
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void OnContinue() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// When implemented in a derived class, executes when the system is
|
|
|
|
// shutting down. Specifies what should occur immediately prior to the
|
|
|
|
// system shutting down.
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void OnShutdown() {}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Set the service status and report the status to the SCM.
|
|
|
|
void SetServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode = NO_ERROR, DWORD dwWaitHint = 0);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Log a message to the Application event log.
|
|
|
|
void WriteEventLogEntry(const char* message, int wType);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Log an error message to the Application event log.
|
|
|
|
void WriteErrorLogEntry(const char* message, int dwError = GetLastError());
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
2021-03-11 02:06:03 +08:00
|
|
|
// Entry point for the service. It registers the handler function for the
|
|
|
|
// service and starts the service.
|
|
|
|
static void WINAPI ServiceMain(DWORD argc, LPSTR* argv);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The function is called by the SCM whenever a control code is sent to
|
|
|
|
// the service.
|
|
|
|
static void WINAPI ServiceCtrlHandler(DWORD code);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Start the service.
|
|
|
|
void Start(DWORD argc, LPSTR* argv);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Pause the service.
|
|
|
|
void Pause();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Resume the service after being paused.
|
|
|
|
void Continue();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Execute when the system is shutting down.
|
|
|
|
void Shutdown();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The singleton service instance.
|
|
|
|
static CServiceBase* s_service;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The name of the service
|
|
|
|
char* m_name;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The status of the service
|
|
|
|
SERVICE_STATUS m_status;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The service status handle
|
|
|
|
SERVICE_STATUS_HANDLE m_statusHandle;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|