Win32Service.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef WIN_32_SERVICE_H__
  2. #define WIN_32_SERVICE_H__
  3. #include <thread>
  4. #include <windows.h>
  5. #ifdef _WIN32
  6. // Internal name of the service
  7. #define SERVICE_NAME "i2pdService"
  8. // Displayed name of the service
  9. #define SERVICE_DISPLAY_NAME "i2pd router service"
  10. // Service start options.
  11. #define SERVICE_START_TYPE SERVICE_DEMAND_START
  12. // List of service dependencies - "dep1\0dep2\0\0"
  13. #define SERVICE_DEPENDENCIES ""
  14. // The name of the account under which the service should run
  15. #define SERVICE_ACCOUNT "NT AUTHORITY\\LocalService"
  16. // The password to the service account name
  17. #define SERVICE_PASSWORD NULL
  18. #endif
  19. class I2PService
  20. {
  21. public:
  22. I2PService(PSTR pszServiceName,
  23. BOOL fCanStop = TRUE,
  24. BOOL fCanShutdown = TRUE,
  25. BOOL fCanPauseContinue = FALSE);
  26. virtual ~I2PService(void);
  27. static BOOL isService();
  28. static BOOL Run(I2PService &service);
  29. void Stop();
  30. protected:
  31. virtual void OnStart(DWORD dwArgc, PSTR *pszArgv);
  32. virtual void OnStop();
  33. virtual void OnPause();
  34. virtual void OnContinue();
  35. virtual void OnShutdown();
  36. void SetServiceStatus(DWORD dwCurrentState,
  37. DWORD dwWin32ExitCode = NO_ERROR,
  38. DWORD dwWaitHint = 0);
  39. private:
  40. static void WINAPI ServiceMain(DWORD dwArgc, LPSTR *lpszArgv);
  41. static void WINAPI ServiceCtrlHandler(DWORD dwCtrl);
  42. void WorkerThread();
  43. void Start(DWORD dwArgc, PSTR *pszArgv);
  44. void Pause();
  45. void Continue();
  46. void Shutdown();
  47. static I2PService* s_service;
  48. PSTR m_name;
  49. SERVICE_STATUS m_status;
  50. SERVICE_STATUS_HANDLE m_statusHandle;
  51. BOOL m_fStopping;
  52. HANDLE m_hStoppedEvent;
  53. std::thread* _worker;
  54. };
  55. void InstallService(
  56. PCSTR pszServiceName,
  57. PCSTR pszDisplayName,
  58. DWORD dwStartType,
  59. PCSTR pszDependencies,
  60. PCSTR pszAccount,
  61. PCSTR pszPassword
  62. );
  63. void UninstallService(PCSTR pszServiceName);
  64. #endif // WIN_32_SERVICE_H__