I found the following code to Stop and Start Windows services but I have run into a problem. This code can only stop a service if all of the services that are dependent on it are stopped first. Is there any way to programtically do this like possibly enumerate them or do I have to code for every possible dependent service?

Any help is appreciated.

Jim

VB Code:
  1. 'API Constants
  2. Public Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
  3. ' Service Control
  4. Public Const SERVICE_CONTROL_STOP = &H1
  5. Public Const SERVICE_CONTROL_PAUSE = &H2
  6. ' Service State - for CurrentState
  7. Public Const SERVICE_STOPPED = &H1
  8. Public Const SERVICE_START_PENDING = &H2
  9. Public Const SERVICE_STOP_PENDING = &H3
  10. Public Const SERVICE_RUNNING = &H4
  11. Public Const SERVICE_CONTINUE_PENDING = &H5
  12. Public Const SERVICE_PAUSE_PENDING = &H6
  13. Public Const SERVICE_PAUSED = &H7
  14. 'Service Control Manager object specific access types
  15. Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
  16. Public Const SC_MANAGER_CONNECT = &H1
  17. Public Const SC_MANAGER_CREATE_SERVICE = &H2
  18. Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
  19. Public Const SC_MANAGER_LOCK = &H8
  20. Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
  21. Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
  22. Public Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or
  23. SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or
  24. SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)
  25. 'Service object specific access types
  26. Public Const SERVICE_QUERY_CONFIG = &H1
  27. Public Const SERVICE_CHANGE_CONFIG = &H2
  28. Public Const SERVICE_QUERY_STATUS = &H4
  29. Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
  30. Public Const SERVICE_START = &H10
  31. Public Const SERVICE_STOP = &H20
  32. Public Const SERVICE_PAUSE_CONTINUE = &H40
  33. Public Const SERVICE_INTERROGATE = &H80
  34. Public Const SERVICE_USER_DEFINED_CONTROL = &H100
  35. Public Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or
  36. SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or
  37. SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or
  38. SERVICE_USER_DEFINED_CONTROL)
  39.  
  40. Type SERVICE_STATUS
  41.     dwServiceType As Long
  42.     dwCurrentState As Long
  43.     dwControlsAccepted As Long
  44.     dwWin32ExitCode As Long
  45.     dwServiceSpecificExitCode As Long
  46.     dwCheckPoint As Long
  47.     dwWaitHint As Long
  48. End Type
  49.  
  50. Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
  51. Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long,
  52. lpServiceStatus As SERVICE_STATUS) As Long
  53. Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As
  54. String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
  55. Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal
  56. lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
  57. Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As
  58. SERVICE_STATUS) As Long
  59. Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal
  60. dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
  61.  
  62. Public Function ServiceStatus(ComputerName As String, ServiceName As String) As String
  63.     Dim ServiceStat As SERVICE_STATUS
  64.     Dim hSManager As Long
  65.     Dim hService As Long
  66.     Dim hServiceStatus As Long
  67.  
  68.     ServiceStatus = ""
  69.     hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
  70. SC_MANAGER_ALL_ACCESS)
  71.     If hSManager <> 0 Then
  72.         hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  73.         If hService <> 0 Then
  74.             hServiceStatus = QueryServiceStatus(hService, ServiceStat)
  75.             If hServiceStatus <> 0 Then
  76.                 Select Case ServiceStat.dwCurrentState
  77.                 Case SERVICE_STOPPED
  78.                     ServiceStatus = "Stopped"
  79.                 Case SERVICE_START_PENDING
  80.                     ServiceStatus = "Start Pending"
  81.                 Case SERVICE_STOP_PENDING
  82.                     ServiceStatus = "Stop Pending"
  83.                 Case SERVICE_RUNNING
  84.                     ServiceStatus = "Running"
  85.                 Case SERVICE_CONTINUE_PENDING
  86.                     ServiceStatus = "Coninue Pending"
  87.                 Case SERVICE_PAUSE_PENDING
  88.                     ServiceStatus = "Pause Pending"
  89.                 Case SERVICE_PAUSED
  90.                     ServiceStatus = "Paused"
  91.                 End Select
  92.             End If
  93.             CloseServiceHandle hService
  94.         End If
  95.         CloseServiceHandle hSManager
  96.     End If
  97. End Function
  98.  
  99. Public Sub ServicePause(ComputerName As String, ServiceName As String)
  100.     Dim ServiceStatus As SERVICE_STATUS
  101.     Dim hSManager As Long
  102.     Dim hService As Long
  103.     Dim res As Long
  104.  
  105.     hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
  106. SC_MANAGER_ALL_ACCESS)
  107.     If hSManager <> 0 Then
  108.         hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  109.         If hService <> 0 Then
  110.             res = ControlService(hService, SERVICE_CONTROL_PAUSE, ServiceStatus)
  111.             CloseServiceHandle hService
  112.         End If
  113.         CloseServiceHandle hSManager
  114.     End If
  115. End Sub
  116.  
  117. Public Sub ServiceStart(ComputerName As String, ServiceName As String)
  118.     Dim ServiceStatus As SERVICE_STATUS
  119.     Dim hSManager As Long
  120.     Dim hService As Long
  121.     Dim res As Long
  122.  
  123.     hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
  124. SC_MANAGER_ALL_ACCESS)
  125.     If hSManager <> 0 Then
  126.         hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  127.         If hService <> 0 Then
  128.             res = StartService(hService, 0, 0)
  129.             CloseServiceHandle hService
  130.         End If
  131.         CloseServiceHandle hSManager
  132.     End If
  133. End Sub
  134.  
  135. Public Sub ServiceStop(ComputerName As String, ServiceName As String)
  136.     Dim ServiceStatus As SERVICE_STATUS
  137.     Dim hSManager As Long
  138.     Dim hService As Long
  139.     Dim res As Long
  140.  
  141.     hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE,
  142. SC_MANAGER_ALL_ACCESS)
  143.     If hSManager <> 0 Then
  144.         hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
  145.         If hService <> 0 Then
  146.             res = ControlService(hService, SERVICE_CONTROL_STOP, ServiceStatus)
  147.             CloseServiceHandle hService
  148.         End If
  149.         CloseServiceHandle hSManager
  150.     End If
  151. End Sub
  152.  
  153.  
  154.  
  155. Put the following code in the form:
  156.  
  157. 'Usage: the first parameter is the remote computer name, if it is "" the computer is the local
  158. computer, the second is the service name.
  159. Private Sub Command1_Click()
  160.     ServiceStop "", "Schedule"
  161. End Sub
  162.  
  163. Private Sub Command2_Click()
  164.     ServiceStart "", "Schedule"
  165. End Sub
  166.  
  167. Private Sub Command3_Click()
  168.     ServicePause "", "Schedule"
  169. End Sub
  170.  
  171. Private Sub Command4_Click()
  172.     MsgBox ServiceStatus("", "Schedule")
  173. End Sub