Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.0 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // SCM.CPP / Tuneup
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1998
  7. // All rights reserved
  8. //
  9. // Contains the Service Control Manager functions used in Tuneup.
  10. //
  11. // 9/98 - Jason Cohen (JCOHEN)
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. // Include files.
  15. //
  16. #include <windows.h>
  17. // Internal function prototypes.
  18. //
  19. static SC_HANDLE ServiceOpen(LPCTSTR);
  20. static DWORD ServicePending(SC_HANDLE);
  21. //
  22. // External function(s).
  23. //
  24. BOOL ServiceStart(LPCTSTR szServiceName)
  25. {
  26. BOOL bReturn = TRUE;
  27. SC_HANDLE hService = ServiceOpen(szServiceName);
  28. if ( hService )
  29. {
  30. // First start the service and wait for it to startup.
  31. //
  32. if ( (ServicePending(hService) != SERVICE_RUNNING) &&
  33. ( (!StartService(hService, 0, NULL)) || (ServicePending(hService) != SERVICE_RUNNING) ) )
  34. bReturn = FALSE;
  35. // If it is started, change it's startup to automatic.
  36. //
  37. if ( bReturn )
  38. ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  39. CloseServiceHandle(hService);
  40. return bReturn;
  41. }
  42. return FALSE;
  43. }
  44. BOOL ServiceRunning(LPCTSTR szServiceName)
  45. {
  46. BOOL bReturn = FALSE;
  47. SC_HANDLE hService = ServiceOpen(szServiceName);
  48. if ( hService )
  49. {
  50. if ( ServicePending(hService) == SERVICE_RUNNING )
  51. bReturn = TRUE;
  52. CloseServiceHandle(hService);
  53. }
  54. return bReturn;
  55. }
  56. //
  57. // Internal function(s).
  58. //
  59. static SC_HANDLE ServiceOpen(LPCTSTR szServiceName)
  60. {
  61. SC_HANDLE hSCManager,
  62. hService;
  63. // Open the Service Control Manager database.
  64. //
  65. if ( (hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL )
  66. return NULL;
  67. // Open the service.
  68. //
  69. hService = OpenService(
  70. hSCManager, // handle to service control manager database
  71. szServiceName, // pointer to name of service to start
  72. SERVICE_ALL_ACCESS // type of access to service
  73. );
  74. // Close the handle to the service Control Manager database.
  75. //
  76. CloseServiceHandle(hSCManager);
  77. // Return the handle to the service, or NULL if it failed.
  78. //
  79. return hService;
  80. }
  81. static DWORD ServicePending(SC_HANDLE schService)
  82. {
  83. SERVICE_STATUS ssStatus;
  84. DWORD dwOldCheckPoint;
  85. BOOL bCheck = TRUE,
  86. bQuery;
  87. ZeroMemory(&ssStatus, sizeof(SERVICE_STATUS));
  88. bQuery = QueryServiceStatus(schService, &ssStatus);
  89. while ( ( bQuery && bCheck ) &&
  90. ( (ssStatus.dwCurrentState == SERVICE_START_PENDING) ||
  91. (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) ||
  92. (ssStatus.dwCurrentState == SERVICE_CONTINUE_PENDING) ||
  93. (ssStatus.dwCurrentState == SERVICE_PAUSE_PENDING)
  94. )
  95. )
  96. {
  97. dwOldCheckPoint = ssStatus.dwCheckPoint;
  98. Sleep(ssStatus.dwWaitHint);
  99. bQuery = QueryServiceStatus(schService, &ssStatus);
  100. bCheck = (dwOldCheckPoint < ssStatus.dwCheckPoint);
  101. dwOldCheckPoint = ssStatus.dwCheckPoint;
  102. }
  103. return ssStatus.dwCurrentState;
  104. }