ServiceUtils.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // https://learn.microsoft.com/en-us/windows/win32/services/installing-a-service
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <strsafe.h>
  6. bool MyCreateServiceW(LPCWSTR serviceName, LPCWSTR displayName, LPCWSTR binaryPath)
  7. {
  8. SC_HANDLE schSCManager;
  9. SC_HANDLE schService;
  10. // Get a handle to the SCM database.
  11. schSCManager = OpenSCManagerW(
  12. NULL, // local computer
  13. NULL, // ServicesActive database
  14. SC_MANAGER_ALL_ACCESS); // full access rights
  15. if (NULL == schSCManager)
  16. {
  17. WcaLog(LOGMSG_STANDARD, "OpenSCManager failed (%d)\n", GetLastError());
  18. return false;
  19. }
  20. // Create the service
  21. schService = CreateServiceW(
  22. schSCManager, // SCM database
  23. serviceName, // name of service
  24. displayName, // service name to display
  25. SERVICE_ALL_ACCESS, // desired access
  26. SERVICE_WIN32_OWN_PROCESS, // service type
  27. SERVICE_AUTO_START, // start type
  28. SERVICE_ERROR_NORMAL, // error control type
  29. binaryPath, // path to service's binary
  30. NULL, // no load ordering group
  31. NULL, // no tag identifier
  32. NULL, // no dependencies
  33. NULL, // LocalSystem account
  34. NULL); // no password
  35. if (schService == NULL)
  36. {
  37. WcaLog(LOGMSG_STANDARD, "CreateService failed (%d)\n", GetLastError());
  38. CloseServiceHandle(schSCManager);
  39. return false;
  40. }
  41. else
  42. {
  43. WcaLog(LOGMSG_STANDARD, "Service installed successfully\n");
  44. }
  45. CloseServiceHandle(schService);
  46. CloseServiceHandle(schSCManager);
  47. return true;
  48. }
  49. bool MyDeleteServiceW(LPCWSTR serviceName)
  50. {
  51. SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
  52. if (hSCManager == NULL) {
  53. WcaLog(LOGMSG_STANDARD, "Failed to open Service Control Manager, error: 0x%02X", GetLastError());
  54. return false;
  55. }
  56. SC_HANDLE hService = OpenServiceW(hSCManager, serviceName, SERVICE_STOP | DELETE);
  57. if (hService == NULL) {
  58. WcaLog(LOGMSG_STANDARD, "Failed to open service: %ls, error: 0x%02X", serviceName, GetLastError());
  59. CloseServiceHandle(hSCManager);
  60. return false;
  61. }
  62. SERVICE_STATUS serviceStatus;
  63. if (ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus)) {
  64. WcaLog(LOGMSG_STANDARD, "Stopping service: %ls", serviceName);
  65. }
  66. bool success = DeleteService(hService);
  67. if (!success) {
  68. WcaLog(LOGMSG_STANDARD, "Failed to delete service: %ls, error: 0x%02X", serviceName, GetLastError());
  69. }
  70. CloseServiceHandle(hService);
  71. CloseServiceHandle(hSCManager);
  72. return success;
  73. }
  74. bool MyStartServiceW(LPCWSTR serviceName)
  75. {
  76. SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
  77. if (hSCManager == NULL) {
  78. WcaLog(LOGMSG_STANDARD, "Failed to open Service Control Manager, error: 0x%02X", GetLastError());
  79. return false;
  80. }
  81. SC_HANDLE hService = OpenServiceW(hSCManager, serviceName, SERVICE_START);
  82. if (hService == NULL) {
  83. WcaLog(LOGMSG_STANDARD, "Failed to open service: %ls, error: 0x%02X", serviceName, GetLastError());
  84. CloseServiceHandle(hSCManager);
  85. return false;
  86. }
  87. bool success = StartServiceW(hService, 0, NULL);
  88. if (!success) {
  89. WcaLog(LOGMSG_STANDARD, "Failed to start service: %ls, error: 0x%02X", serviceName, GetLastError());
  90. }
  91. CloseServiceHandle(hService);
  92. CloseServiceHandle(hSCManager);
  93. return success;
  94. }
  95. bool MyStopServiceW(LPCWSTR serviceName)
  96. {
  97. SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
  98. if (hSCManager == NULL) {
  99. WcaLog(LOGMSG_STANDARD, "Failed to open Service Control Manager");
  100. return false;
  101. }
  102. SC_HANDLE hService = OpenServiceW(hSCManager, serviceName, SERVICE_STOP);
  103. if (hService == NULL) {
  104. WcaLog(LOGMSG_STANDARD, "Failed to open service: %ls", serviceName);
  105. CloseServiceHandle(hSCManager);
  106. return false;
  107. }
  108. SERVICE_STATUS serviceStatus;
  109. if (!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus)) {
  110. WcaLog(LOGMSG_STANDARD, "Failed to stop service: %ls", serviceName);
  111. CloseServiceHandle(hService);
  112. CloseServiceHandle(hSCManager);
  113. return false;
  114. }
  115. CloseServiceHandle(hService);
  116. CloseServiceHandle(hSCManager);
  117. return true;
  118. }
  119. bool QueryServiceStatusExW(LPCWSTR serviceName, SERVICE_STATUS_PROCESS* status)
  120. {
  121. SC_HANDLE hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
  122. if (hSCManager == NULL) {
  123. WcaLog(LOGMSG_STANDARD, "Failed to open Service Control Manager");
  124. return false;
  125. }
  126. SC_HANDLE hService = OpenServiceW(hSCManager, serviceName, SERVICE_QUERY_STATUS);
  127. if (hService == NULL) {
  128. WcaLog(LOGMSG_STANDARD, "Failed to open service: %ls", serviceName);
  129. CloseServiceHandle(hSCManager);
  130. return false;
  131. }
  132. DWORD bytesNeeded;
  133. BOOL success = QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, reinterpret_cast<LPBYTE>(status), sizeof(*status), &bytesNeeded);
  134. if (!success) {
  135. WcaLog(LOGMSG_STANDARD, "Failed to query service: %ls", serviceName);
  136. }
  137. CloseServiceHandle(hService);
  138. CloseServiceHandle(hSCManager);
  139. return success;
  140. }
  141. bool IsServiceRunningW(LPCWSTR serviceName)
  142. {
  143. SERVICE_STATUS_PROCESS serviceStatus;
  144. QueryServiceStatusExW(serviceName, &serviceStatus);
  145. return (serviceStatus.dwCurrentState == SERVICE_RUNNING);
  146. }