Leaked source code of windows server 2003
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.

86 lines
2.0 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. SERVUTIL.CPP
  5. Abstract:
  6. Defines various service utilities.
  7. History:
  8. a-davj 04-Mar-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "servutil.h"
  12. //***************************************************************************
  13. //
  14. // BOOL StopService
  15. //
  16. // DESCRIPTION:
  17. //
  18. // Stops and then removes the service.
  19. //
  20. // PARAMETERS:
  21. //
  22. // pServiceName short service name
  23. // dwMaxWait max time in seconds to wait
  24. //
  25. // RETURN VALUE:
  26. //
  27. // TRUE if it worked
  28. //
  29. //***************************************************************************
  30. BOOL StopService(
  31. IN LPCTSTR pServiceName,
  32. IN DWORD dwMaxWait)
  33. {
  34. BOOL bRet = FALSE;
  35. SC_HANDLE schService;
  36. SC_HANDLE schSCManager;
  37. DWORD dwCnt;
  38. SERVICE_STATUS ssStatus; // current status of the service
  39. schSCManager = OpenSCManager(
  40. NULL, // machine (NULL == local)
  41. NULL, // database (NULL == default)
  42. SC_MANAGER_ALL_ACCESS // access required
  43. );
  44. if ( schSCManager )
  45. {
  46. schService = OpenService(schSCManager, pServiceName, SERVICE_ALL_ACCESS);
  47. if (schService)
  48. {
  49. // try to stop the service
  50. if ( bRet = ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
  51. {
  52. for(dwCnt=0; dwCnt < dwMaxWait &&
  53. QueryServiceStatus( schService, &ssStatus ); dwCnt++)
  54. {
  55. if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
  56. Sleep( 1000 );
  57. else
  58. break;
  59. }
  60. }
  61. CloseServiceHandle(schService);
  62. }
  63. CloseServiceHandle(schSCManager);
  64. }
  65. return bRet;
  66. }