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.

103 lines
3.9 KiB

  1. // ServiceUtil.cpp: implementation of the CServiceUtil class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <tchar.h>
  8. #include "ServiceUtil.h"
  9. #ifndef _ASSERT
  10. #define _ASSERT assert // servutil.h uses _ASSERT
  11. #endif
  12. #include <servutil.h>
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CServiceUtil::CServiceUtil() :
  17. m_pstServiceStatus(NULL), m_dwNumServices(0), m_dwCurrentState(SERVICE_STOPPED)
  18. {
  19. }
  20. CServiceUtil::~CServiceUtil()
  21. {
  22. if ( NULL != m_pstServiceStatus )
  23. free (m_pstServiceStatus);
  24. }
  25. //////////////////////////////////////////////////////////////////////
  26. // Implementation: public
  27. //////////////////////////////////////////////////////////////////////
  28. HRESULT CServiceUtil::RestoreServiceState( LPCTSTR szServiceName )
  29. {
  30. HRESULT hr = S_OK;
  31. if ( SERVICE_STOP_PENDING != m_dwCurrentState && SERVICE_STOPPED != m_dwCurrentState )
  32. {
  33. // Let's leave the machine how we found it, restart service
  34. // and the dependent services
  35. hr = _StartService( const_cast<LPTSTR>( szServiceName )); // Start the Service first
  36. if ( NULL != m_pstServiceStatus )
  37. {
  38. for ( DWORD i = 0; i < m_dwNumServices && S_OK == hr; i++ )
  39. {
  40. if ( SERVICE_RUNNING == m_pstServiceStatus[i].ServiceStatus.dwCurrentState || SERVICE_START_PENDING == m_pstServiceStatus[i].ServiceStatus.dwCurrentState || SERVICE_CONTINUE_PENDING == m_pstServiceStatus[i].ServiceStatus.dwCurrentState )
  41. hr = _StartService ( m_pstServiceStatus[i].lpServiceName );
  42. if ( S_OK != hr )
  43. {
  44. if ( SERVICE_RUNNING == _GetServiceStatus ( m_pstServiceStatus[i].lpServiceName ))
  45. hr = S_OK;
  46. }
  47. }
  48. free ( m_pstServiceStatus );
  49. m_pstServiceStatus = NULL;
  50. m_dwCurrentState = SERVICE_STOPPED;
  51. m_dwNumServices = 0;
  52. }
  53. }
  54. return hr;
  55. }
  56. HRESULT CServiceUtil::StopService( LPCTSTR szServiceName )
  57. {
  58. HRESULT hr = S_OK;
  59. DWORD dwBufSize = 1;
  60. SC_HANDLE hManager, hService;
  61. m_dwCurrentState = _GetServiceStatus( const_cast<LPTSTR>( szServiceName ));
  62. if ( SERVICE_STOP_PENDING != m_dwCurrentState && SERVICE_STOPPED != m_dwCurrentState )
  63. { // Need to stop the service
  64. // Build a list of dependent services first
  65. hManager = OpenSCManager( NULL, NULL, STANDARD_RIGHTS_REQUIRED );
  66. if ( NULL != hManager )
  67. {
  68. hService = OpenService (hManager, szServiceName, SERVICE_ALL_ACCESS);
  69. if (!EnumDependentServices (hService, SERVICE_ACTIVE, (LPENUM_SERVICE_STATUS)&dwBufSize, dwBufSize, &dwBufSize, &m_dwNumServices))
  70. { // this should fail with ERROR_MORE_DATA, unless there are no dependent services
  71. hr = GetLastError ();
  72. if (hr == ERROR_MORE_DATA)
  73. {
  74. m_pstServiceStatus = (ENUM_SERVICE_STATUS *) malloc (dwBufSize);
  75. if ( NULL == m_pstServiceStatus )
  76. hr = E_OUTOFMEMORY;
  77. else {
  78. if (!EnumDependentServices (hService, SERVICE_ACTIVE, m_pstServiceStatus, dwBufSize, &dwBufSize, &m_dwNumServices))
  79. hr = GetLastError(); // shouldn't happen!!!
  80. else
  81. hr = S_OK;
  82. }
  83. }
  84. CloseServiceHandle( hService );
  85. }
  86. CloseServiceHandle( hManager );
  87. }
  88. if ( S_OK == hr )
  89. hr = _StopService( const_cast<LPTSTR>( szServiceName ), TRUE ); // Stop the Service first
  90. }
  91. return hr;
  92. }