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.

64 lines
1.9 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2001-2001.
  5. //
  6. // File: svcutil.hxx
  7. //
  8. // Contents: Service control manager helper code.
  9. //
  10. // History: 10-Oct-2001 dlee Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #pragma once
  14. class CServiceHandle
  15. {
  16. public :
  17. CServiceHandle() { _h = 0; }
  18. CServiceHandle( SC_HANDLE hSC ) : _h( hSC ) {}
  19. ~CServiceHandle() { Free(); }
  20. void Set( SC_HANDLE h ) { _h = h; }
  21. SC_HANDLE Get() { return _h; }
  22. BOOL IsNull() { return ( 0 == _h ); }
  23. void Free() { if ( 0 != _h ) CloseServiceHandle( _h ); _h = 0; }
  24. private:
  25. SC_HANDLE _h;
  26. };
  27. //+-------------------------------------------------------------------------
  28. //
  29. // Function: IsServiceRunning
  30. //
  31. // Synopsis: Determines if a service is running
  32. //
  33. // Arguments: pwcServiceName -- The name (short or long) of the service
  34. //
  35. // Returns: TRUE if the service is running, FALSE otherwise or if the
  36. // system is low on resources or the status can't be queried.
  37. //
  38. // History: 10-Oct-2001 dlee Created
  39. //
  40. //--------------------------------------------------------------------------
  41. __inline BOOL IsServiceRunning( WCHAR const * pwcServiceName )
  42. {
  43. CServiceHandle xhSC( OpenSCManager( 0, 0, SC_MANAGER_ALL_ACCESS ) );
  44. if ( xhSC.IsNull() )
  45. return FALSE;
  46. CServiceHandle xhService( OpenService( xhSC.Get(),
  47. pwcServiceName,
  48. SERVICE_QUERY_STATUS ) );
  49. if ( xhSC.IsNull() )
  50. return FALSE;
  51. SERVICE_STATUS svcStatus;
  52. if ( QueryServiceStatus( xhService.Get(), &svcStatus ) )
  53. return ( SERVICE_RUNNING == svcStatus.dwCurrentState );
  54. return FALSE;
  55. } //IsServiceRunning