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.

182 lines
3.7 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. 1998 Seagate Software, Inc. All rights reserved
  4. Module Name:
  5. WsbSvc.cpp
  6. Abstract:
  7. This is the implementation of service helper functions.
  8. Author:
  9. Art Bragg 5/29/97
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "ntsecapi.h"
  14. HRESULT
  15. WsbCheckService(
  16. IN const OLECHAR * Computer,
  17. IN GUID GuidApp
  18. )
  19. /*++
  20. Routine Description:
  21. Arguments:
  22. computer - NULL if local computer
  23. guidApp - app id of the service to check.
  24. Return Value:
  25. S_OK - Success - service is running
  26. S_FALSE - Success - service is not running
  27. E_* - Problem occured, error passed down.
  28. --*/
  29. {
  30. HRESULT hr = S_OK;
  31. try {
  32. //
  33. // Get the service status
  34. //
  35. DWORD serviceState;
  36. WsbAffirmHr( WsbGetServiceStatus( Computer, GuidApp, &serviceState ) );
  37. //
  38. // Is the service running?
  39. //
  40. if( SERVICE_RUNNING != serviceState ) WsbThrow( S_FALSE );
  41. } WsbCatch( hr );
  42. return( hr );
  43. }
  44. HRESULT
  45. WsbGetServiceStatus(
  46. IN const OLECHAR *Computer,
  47. IN GUID GuidApp,
  48. OUT DWORD *ServiceStatus
  49. )
  50. /*++
  51. Routine Description:
  52. Arguments:
  53. Computer - NULL if local computer
  54. GuidApp - app id of the service to check.
  55. ServiceStatus - status of the service
  56. Return Value:
  57. S_OK - Success - service is running
  58. S_FALSE - Success - service is not running
  59. E_* - Problem occured, error passed down.
  60. --*/
  61. {
  62. HRESULT hr = S_OK;
  63. SC_HANDLE hSCM = 0;
  64. SC_HANDLE hService = 0;
  65. SERVICE_STATUS serviceStatusStruct;
  66. try {
  67. //
  68. // Find the service in the registry
  69. //
  70. CWsbStringPtr regPath = L"SOFTWARE\\Classes\\AppID\\";
  71. regPath.Append( CWsbStringPtr( GuidApp ) );
  72. //
  73. // Get the name of the service
  74. //
  75. OLECHAR serviceName[WSB_MAX_SERVICE_NAME];
  76. WsbAffirmHr( WsbGetRegistryValueString( Computer, regPath, L"LocalService", serviceName, WSB_MAX_SERVICE_NAME, 0 ) );
  77. //
  78. // Setup the service to run under the account
  79. //
  80. hSCM = OpenSCManager( Computer, 0, GENERIC_READ );
  81. WsbAffirmStatus( 0 != hSCM );
  82. hService = OpenService( hSCM, serviceName, SERVICE_QUERY_STATUS );
  83. WsbAffirmStatus( 0 != hService );
  84. // Get the service status
  85. WsbAffirmStatus( QueryServiceStatus( hService, &serviceStatusStruct ) );
  86. *ServiceStatus = serviceStatusStruct.dwCurrentState;
  87. } WsbCatch( hr );
  88. if( hSCM ) CloseServiceHandle( hSCM );
  89. if( hService ) CloseServiceHandle( hService );
  90. return( hr );
  91. }
  92. HRESULT
  93. WsbGetServiceName(
  94. IN const OLECHAR *computer,
  95. IN GUID guidApp,
  96. IN DWORD cSize,
  97. OUT OLECHAR *serviceName
  98. )
  99. /*++
  100. Routine Description:
  101. Arguments:
  102. computer - NULL if local computer
  103. guidApp - app id of the service whose name to get.
  104. Return Value:
  105. S_OK - Success
  106. E_* - Problem occured, error passed down.
  107. --*/
  108. {
  109. HRESULT hr = S_OK;
  110. try {
  111. //
  112. // Find the service in the registry
  113. //
  114. CWsbStringPtr regPath = L"SOFTWARE\\Classes\\AppID\\";
  115. regPath.Append( CWsbStringPtr( guidApp ) );
  116. //
  117. // Get the name of the service
  118. //
  119. WsbAffirmHr( WsbGetRegistryValueString( computer, regPath, L"LocalService", serviceName, cSize, 0 ) );
  120. } WsbCatch( hr );
  121. return( hr );
  122. }