-
Notifications
You must be signed in to change notification settings - Fork 27
/
ServiceBase.h
122 lines (94 loc) · 4.16 KB
/
ServiceBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/****************************** Module Header ******************************\
* Module Name: ServiceBase.h
* Project: CppWindowsService
* Copyright (c) Microsoft Corporation.
*
* 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
* class.
*
* 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.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
#pragma once
#include <windows.h>
class CServiceBase
{
public:
// 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);
// 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(PWSTR pszServiceName,
BOOL fCanStop = TRUE,
BOOL fCanShutdown = TRUE,
BOOL fCanPauseContinue = FALSE);
// Service object destructor.
virtual ~CServiceBase(void);
// Stop the service.
void Stop();
protected:
// 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, PWSTR *pszArgv);
// 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.
virtual void OnStop();
// 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.
virtual void OnPause();
// 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.
virtual void OnContinue();
// When implemented in a derived class, executes when the system is
// shutting down. Specifies what should occur immediately prior to the
// system shutting down.
virtual void OnShutdown();
// Set the service status and report the status to the SCM.
void SetServiceStatus(DWORD dwCurrentState,
DWORD dwWin32ExitCode = NO_ERROR,
DWORD dwWaitHint = 0);
// Log a message to the Application event log.
void WriteEventLogEntry(PWSTR pszMessage, WORD wType);
// Log an error message to the Application event log.
void WriteErrorLogEntry(PWSTR pszFunction,
DWORD dwError = GetLastError());
private:
// Entry point for the service. It registers the handler function for the
// service and starts the service.
static void WINAPI ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv);
// The function is called by the SCM whenever a control code is sent to
// the service.
static void WINAPI ServiceCtrlHandler(DWORD dwCtrl);
// Start the service.
void Start(DWORD dwArgc, PWSTR *pszArgv);
// Pause the service.
void Pause();
// Resume the service after being paused.
void Continue();
// Execute when the system is shutting down.
void Shutdown();
// The singleton service instance.
static CServiceBase *s_service;
// The name of the service
PWSTR m_name;
// The status of the service
SERVICE_STATUS m_status;
// The service status handle
SERVICE_STATUS_HANDLE m_statusHandle;
};