WinService.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // WinService.h
  3. //
  4. // Library: Util
  5. // Package: Windows
  6. // Module: WinService
  7. //
  8. // Definition of the WinService class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_WinService_INCLUDED
  16. #define Util_WinService_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/UnWindows.h"
  19. #if defined(POCO_WIN32_UTF8)
  20. #define POCO_LPQUERY_SERVICE_CONFIG LPQUERY_SERVICE_CONFIGW
  21. #else
  22. #define POCO_LPQUERY_SERVICE_CONFIG LPQUERY_SERVICE_CONFIGA
  23. #endif
  24. namespace Poco {
  25. namespace Util {
  26. class Util_API WinService
  27. /// This class provides an object-oriented interface to
  28. /// the Windows Service Control Manager for registering,
  29. /// unregistering, configuring, starting and stopping
  30. /// services.
  31. ///
  32. /// This class is only available on Windows platforms.
  33. {
  34. public:
  35. enum Startup
  36. {
  37. SVC_AUTO_START,
  38. SVC_MANUAL_START,
  39. SVC_DISABLED
  40. };
  41. WinService(const std::string& name);
  42. /// Creates the WinService, using the given service name.
  43. ~WinService();
  44. /// Destroys the WinService.
  45. const std::string& name() const;
  46. /// Returns the service name.
  47. std::string displayName() const;
  48. /// Returns the service's display name.
  49. std::string path() const;
  50. /// Returns the path to the service executable.
  51. ///
  52. /// Throws a NotFoundException if the service has not been registered.
  53. void registerService(const std::string& path, const std::string& displayName);
  54. /// Creates a Windows service with the executable specified by path
  55. /// and the given displayName.
  56. ///
  57. /// Throws a ExistsException if the service has already been registered.
  58. void registerService(const std::string& path);
  59. /// Creates a Windows service with the executable specified by path
  60. /// and the given displayName. The service name is used as display name.
  61. ///
  62. /// Throws a ExistsException if the service has already been registered.
  63. void unregisterService();
  64. /// Deletes the Windows service.
  65. ///
  66. /// Throws a NotFoundException if the service has not been registered.
  67. bool isRegistered() const;
  68. /// Returns true if the service has been registered with the Service Control Manager.
  69. bool isRunning() const;
  70. /// Returns true if the service is currently running.
  71. void start();
  72. /// Starts the service.
  73. /// Does nothing if the service is already running.
  74. ///
  75. /// Throws a NotFoundException if the service has not been registered.
  76. void stop();
  77. /// Stops the service.
  78. /// Does nothing if the service is not running.
  79. ///
  80. /// Throws a NotFoundException if the service has not been registered.
  81. void setStartup(Startup startup);
  82. /// Sets the startup mode for the service.
  83. Startup getStartup() const;
  84. /// Returns the startup mode for the service.
  85. void setDescription(const std::string& description);
  86. /// Sets the service description in the registry.
  87. std::string getDescription() const;
  88. /// Returns the service description from the registry.
  89. static const int STARTUP_TIMEOUT;
  90. protected:
  91. static const std::string REGISTRY_KEY;
  92. static const std::string REGISTRY_DESCRIPTION;
  93. private:
  94. void open() const;
  95. bool tryOpen() const;
  96. void close() const;
  97. POCO_LPQUERY_SERVICE_CONFIG config() const;
  98. WinService();
  99. WinService(const WinService&);
  100. WinService& operator = (const WinService&);
  101. std::string _name;
  102. SC_HANDLE _scmHandle;
  103. mutable SC_HANDLE _svcHandle;
  104. };
  105. } } // namespace Poco::Util
  106. #endif // Util_WinService_INCLUDED