Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping a windows service 2

Status
Not open for further replies.

w1nn3r

ISP
Sep 12, 2001
179
US
Alright,
I'm using vb6, and following this link:


I made a standard exe project, pasted all the items, made the button and pasted the ServiceStop "", "Schedule" line into the Command1_Click() part. but it simply doesnt work. anyone know if the code is broke or if I'm doing something wrong. All I want is to stop a windows service.
 
So you are tryng to stop the task scheduler? And you are sure that isn't happening? How are you checking? By looking in the service manager? That normally needs to be refreshed to reflect any programmatic changes to status (alternatively the code given under Command4_Click will retrieve the real current status)

Note that the code example you have found is pretty old, and nowadays there are easier ways of manipulating Windows services. Check out Win32_Service for WMI (I can provide an example if necessary)
 
I am actually changing it from task scheduler to print spooler. I'm confirming that it's not working by going to services.msc and refreshing the view after running the sample application. I've also made sure that restart the service isnt selected in the recovery tab for first, second and subsequent failures. Any example codes for an easier way of stopping a windows service would be greatly appreciated.
 
Here's a module:
Code:
[blue]Option Explicit

Public Enum SC_COMMAND
    ServiceStart = 1
    ServiceStop
    ServicePause
    ServiceResume
    ServiceStatus
End Enum
    

Public Sub ServiceControl(ServiceCommand As SC_COMMAND, strServiceName As String, Optional strComputer As String = ".")
    Dim objWMIService As Object
    Dim Services As Object
    Dim Service As Object
    Dim result As Long
    
    If strServiceName = "" Then Err.Raise vbObjectError + 1, "ServiceControl", "No service name provided"
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    If objWMIService Is Nothing Then Err.Raise vbObjectError + 2, "ServiceControl", "No such service: " & strServiceName
    Set Services = objWMIService.ExecQuery("Select * from Win32_Service where Name='" & strServiceName & "'")
    For Each Service In Services ' should only be one
        Select Case ServiceCommand
            Case ServiceStart
                result = Service.StartService
            Case ServiceStop
                result = Service.StopService
            Case ServicePause
                result = Service.PauseService ' Not too many we can do this with
            Case ServiceResume
                result = Service.ResumeService
            Case ServiceStatus
                MsgBox Service.State
            Case Else
                Err.Raise vbObjectError + 3, "ServiceControl", "Unrecognized service command"
        End Select
        If result Then Err.Raise vbObjectError + 4, "ServiceControl", "Could not run this command on " & strServiceName
    Next
End Sub
[/blue]
And here's how you might call it for the print spooler service:
Code:
[blue]Option Explicit

Private Sub Command1_Click()
    ServiceControl ServiceStop, "Spooler"
End Sub

Private Sub Command2_Click()
    ServiceControl ServiceStart, "Spooler"
End Sub

Private Sub Command3_Click()
    'ServiceControl ServicePause, "Spooler"
End Sub

Private Sub Command4_Click()
    'ServiceControl ServiceResume, "Spooler"
End Sub
 
Private Sub Command5_Click()
    ServiceControl ServiceStatus, "Spooler"
End Sub[/blue]
 
that code was awesome! now if I can ask 1 more question, you used "spooler" but in services it's listed as "Print Spooler" which might have been my problem. How can I get the correct name of the service that will allow your code to stop it?
 
Print Spooler" is the DisplayName. Here's another bit of code for you:
Code:
[blue]Public Sub ListServices(Optional strComputer As String = ".")
    Dim objWMIService As Object
    Dim Services As Object
    Dim Service As Object
    
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set Services = objWMIService.ExecQuery("Select * from Win32_Service")
    For Each Service In Services ' should only be one
        Debug.Print Service.Name, Service.DisplayName
    Next
End Sub[/blue]
 
(but ignore the 'should only be one' comment; I forgot to delete it for this second example)
 
SHELL "Net Stop Spooler"

should do the same

-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
I think I'd argue that if you are going to use the command line and want something of around about the same flexibility as the VB solution provided here (and are using XP or later), then sc is probably more apt

SHELL "sc \\. stop spooler"




*sc is available for earlier versions of Windows as part of the applicable resource kit

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top