ServiceBase.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /****************************** Module Header ******************************\
  2. * Module Name: ServiceBase.h
  3. * Project: CppWindowsService
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Provides a base class for a service that will exist as part of a service
  7. * application. CServiceBase must be derived from when creating a new service
  8. * class.
  9. *
  10. * This source is subject to the Microsoft Public License.
  11. * See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
  12. * All other rights reserved.
  13. *
  14. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  15. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  17. \***************************************************************************/
  18. #pragma once
  19. #include <WinSock2.h>
  20. #include <windows.h>
  21. class CServiceBase
  22. {
  23. public:
  24. // Register the executable for a service with the Service Control Manager
  25. // (SCM). After you call Run(ServiceBase), the SCM issues a Start command,
  26. // which results in a call to the OnStart method in the service. This
  27. // method blocks until the service has stopped.
  28. static BOOL Run(CServiceBase &service);
  29. // Service object constructor. The optional parameters (fCanStop,
  30. // fCanShutdown and fCanPauseContinue) allow you to specify whether the
  31. // service can be stopped, paused and continued, or be notified when
  32. // system shutdown occurs.
  33. CServiceBase(LPSTR pszServiceName,
  34. BOOL fCanStop = TRUE,
  35. BOOL fCanShutdown = TRUE,
  36. BOOL fCanPauseContinue = FALSE);
  37. // Service object destructor.
  38. virtual ~CServiceBase(void);
  39. // Stop the service.
  40. void Stop();
  41. protected:
  42. // When implemented in a derived class, executes when a Start command is
  43. // sent to the service by the SCM or when the operating system starts
  44. // (for a service that starts automatically). Specifies actions to take
  45. // when the service starts.
  46. virtual void OnStart(DWORD dwArgc, PSTR *pszArgv);
  47. // When implemented in a derived class, executes when a Stop command is
  48. // sent to the service by the SCM. Specifies actions to take when a
  49. // service stops running.
  50. virtual void OnStop();
  51. // When implemented in a derived class, executes when a Pause command is
  52. // sent to the service by the SCM. Specifies actions to take when a
  53. // service pauses.
  54. virtual void OnPause();
  55. // When implemented in a derived class, OnContinue runs when a Continue
  56. // command is sent to the service by the SCM. Specifies actions to take
  57. // when a service resumes normal functioning after being paused.
  58. virtual void OnContinue();
  59. // When implemented in a derived class, executes when the system is
  60. // shutting down. Specifies what should occur immediately prior to the
  61. // system shutting down.
  62. virtual void OnShutdown();
  63. // Set the service status and report the status to the SCM.
  64. void SetServiceStatus(DWORD dwCurrentState,
  65. DWORD dwWin32ExitCode = NO_ERROR,
  66. DWORD dwWaitHint = 0);
  67. // Log a message to the Application event log.
  68. void WriteEventLogEntry(PSTR pszMessage, WORD wType);
  69. // Log an error message to the Application event log.
  70. void WriteErrorLogEntry(PSTR pszFunction,
  71. DWORD dwError = GetLastError());
  72. private:
  73. // Entry point for the service. It registers the handler function for the
  74. // service and starts the service.
  75. static void WINAPI ServiceMain(DWORD dwArgc, LPSTR *lpszArgv);
  76. // The function is called by the SCM whenever a control code is sent to
  77. // the service.
  78. static void WINAPI ServiceCtrlHandler(DWORD dwCtrl);
  79. // Start the service.
  80. void Start(DWORD dwArgc, PSTR *pszArgv);
  81. // Pause the service.
  82. void Pause();
  83. // Resume the service after being paused.
  84. void Continue();
  85. // Execute when the system is shutting down.
  86. void Shutdown();
  87. // The singleton service instance.
  88. static CServiceBase *s_service;
  89. // The name of the service
  90. LPSTR m_name;
  91. // The status of the service
  92. SERVICE_STATUS m_status;
  93. // The service status handle
  94. SERVICE_STATUS_HANDLE m_statusHandle;
  95. };