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.

150 lines
4.5 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1996-1998 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // StopService.cpp
  7. //
  8. // Abstract:
  9. // The functions in this file are used to stop the services used
  10. // by the Cluster Service.
  11. //
  12. // Author:
  13. // C. Brent Thomas (a-brentt) April 1 1998
  14. //
  15. // Revision History:
  16. //
  17. // Notes:
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <windows.h>
  23. #include <winerror.h>
  24. #include <tchar.h>
  25. #include "SetupCommonLibRes.h"
  26. /////////////////////////////////////////////////////////////////////////////
  27. //++
  28. //
  29. // StopService
  30. //
  31. // Routine Description:
  32. // This function attempts to stop a service. This function will time out
  33. // if the service does not stop soon enough. If this function times out,
  34. // The service may eventually stop, but this function will have exited.
  35. //
  36. // Arguments:
  37. // pwszServiceName - points to a wide character string that contains the
  38. // name of the service to be stopped.
  39. //
  40. // Return Value:
  41. // TRUE - indicates that the service was stopped
  42. // FALSE - indicates that the service had not stopped when this function
  43. // timed out.
  44. //
  45. // Note:
  46. // This function was adapted from a function with the same name in the
  47. // "newsetup" project utils.cpp file.
  48. //--
  49. /////////////////////////////////////////////////////////////////////////////
  50. BOOL StopService( IN LPCWSTR pwszServiceName )
  51. {
  52. SC_HANDLE hscServiceMgr;
  53. SC_HANDLE hscService;
  54. SERVICE_STATUS t_Status;
  55. BOOL fSuccess = (BOOL) FALSE;
  56. DWORD dwTimeoutTime;
  57. //
  58. // Timeout after 20 seconds and keep going. So if the service
  59. // is hung, we won't hang forever.
  60. //
  61. dwTimeoutTime = GetTickCount() + 20 * 1000;
  62. // Connect to the Service Control Manager and open the specified service
  63. // control manager database.
  64. hscServiceMgr = OpenSCManager( NULL, NULL, GENERIC_READ | GENERIC_WRITE );
  65. // Was the service control manager database opened successfully?
  66. if ( hscServiceMgr != NULL )
  67. {
  68. // The service control manager database is open.
  69. // Open a handle to the service to be stopped.
  70. hscService = OpenService( hscServiceMgr,
  71. pwszServiceName,
  72. GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE );
  73. // Was the handle to the service opened?
  74. if ( hscService != NULL )
  75. {
  76. // Request that the service stop.
  77. fSuccess = ControlService( hscService,
  78. SERVICE_CONTROL_STOP,
  79. &t_Status );
  80. // Was the "stop" request sent to the service successfully?
  81. if ( fSuccess == (BOOL) TRUE )
  82. {
  83. // Give the service a chance to stop.
  84. Sleep(1000);
  85. fSuccess = (BOOL) FALSE; // Indicate that the service has not stopped.
  86. // If this function times out fSuccess will
  87. // remain FALSE when StopService exits.
  88. // Check the status of the service to determine whether it has stopped.
  89. while ( QueryServiceStatus( hscService, &t_Status ) &&
  90. (dwTimeoutTime > GetTickCount()) )
  91. {
  92. // Is the "stop" request still pending?
  93. if ( t_Status.dwCurrentState == SERVICE_STOP_PENDING )
  94. {
  95. // The "stop" request is still pending. Give the service a
  96. // little while longer to stop.
  97. Sleep(1000);
  98. }
  99. else
  100. {
  101. // The service has stopped.
  102. fSuccess = (BOOL) TRUE; // Indicate that the service has stoped.
  103. break;
  104. } // Is the "stop" request still pending?
  105. } // end of while loop
  106. } // Was the "stop" request sent to the service successfully?
  107. // Close the handle to the service.
  108. CloseServiceHandle( hscService );
  109. } // Was the handle to the service opened?
  110. // Close connsection to the service control manager and close the service
  111. // control manager database.
  112. CloseServiceHandle( hscServiceMgr );
  113. } // Was the service control manager database opened successfully?
  114. return ( fSuccess );
  115. }