Solved

Stopping Windows Service

Posted on 2006-11-20
5
308 Views
Last Modified: 2012-08-13
What would be the best way to stop a windows service via code?  Thanks.
0
Comment
Question by:zordao
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17985520
0
 
LVL 19

Expert Comment

by:Desp
ID: 17985585
0
 
LVL 8

Expert Comment

by:plq
ID: 17985602
Here's some vb6 code that uses wmi to start the sql server service. The equivalent function is StopService instead of StartService as I remember

Public Function CheckSqlInstance(sInstance As String) As Boolean
   
    Dim sComputer As String
    Dim sNamespace As String
    Dim sClass As String
    Dim sKey As String
    Dim sKeyValue As String
    Dim objSWbemServices As Object
    Dim colSWbemObjectSet As Object
    Dim objSWbemObject As Object
   
    sComputer = "."
    sNamespace = "\root\cimv2"
    sClass = "Win32_Service"
    sKey = "Name"
    sKeyValue = "MSSQL$" & sInstance
   
    Set objSWbemServices = GetObject("winmgmts:\\" & sComputer & sNamespace)
    Set colSWbemObjectSet = objSWbemServices.ExecQuery _
        ("SELECT * FROM " & sClass & " WHERE " & sKey & "='" & sKeyValue & "'")

    For Each objSWbemObject In colSWbemObjectSet
        CheckSqlInstance = True
        Exit Function
    Next
   
    CheckSqlInstance = False
   
End Function


Public Function StartSql(sInstance As String)
   
    Dim sComputer As String
    Dim sNamespace As String
    Dim sClass As String
    Dim sKey As String
    Dim sKeyValue As String
    Dim objSWbemServices As Object
    Dim colSWbemObjectSet As Object
    Dim objSWbemObject As Object
   
    sComputer = "."
    sNamespace = "\root\cimv2"
    sClass = "Win32_Service"
    sKey = "Name"
    sKeyValue = "MSSQL$" & sInstance
   
    Set objSWbemServices = GetObject("winmgmts:\\" & sComputer & sNamespace)
    Set colSWbemObjectSet = objSWbemServices.ExecQuery _
        ("SELECT * FROM " & sClass & " WHERE " & sKey & "='" & sKeyValue & "'")

    For Each objSWbemObject In colSWbemObjectSet
        If objSWbemObject.State <> "Running" Then
            objSWbemObject.StartService
        End If
    Next
   
    On Error Resume Next
    ' removed 5.0.12
    'Shell "sqlmangr.exe", vbMinimizedNoFocus
   
'    RestartService sKeyValue

End Function
0
 
LVL 5

Expert Comment

by:DEEPESH
ID: 17985872
Imports System.ServiceProcess
Public Class EmptyService
    Inherits System.ServiceProcess.ServiceBase
    Private worker As New worker()
    Protected Overrides Sub OnStart(ByVal args() As String)
        System.IO.Directory.SetCurrentDirectory(GetMyDir)
        Dim wt As System.Threading.Thread
        Dim ts As System.Threading.ThreadStart
        ts = AddressOf worker.DoWork
        wt = New System.Threading.Thread(ts)
        wt.Start()
    End Sub
    Protected Overrides Sub OnStop()
        worker.StopWork()
    End Sub
    Function GetMyDir() As String
        Dim fi As System.IO.FileInfo
        Dim di As System.IO.DirectoryInfo
        Dim pc As System.Diagnostics.Process
        Try
            pc = System.Diagnostics.Process.GetCurrentProcess
            fi = New System.IO.FileInfo(pc.MainModule.FileName)
            di = fi.Directory
            GetMyDir = di.FullName
        Finally
            fi = Nothing
            di = Nothing
            pc = Nothing
        End Try
    End Function
End Class
0
 
LVL 9

Accepted Solution

by:
DjDezmond earned 500 total points
ID: 17985887
Or you could use a vbscript... nice and simple:

---------------------------------------------------------------------
Option Explicit
Dim objWMIService, objService, strStartupType
Dim colListOfServices, strService

if WScript.Arguments.count <> 2 then Wscript.Quit

strService = "'" & WScript.Arguments.Item(0) & "'"
strStartupType = WScript.Arguments.Item(1)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name =" & strService)
For Each objService in colListOfServices
  objService.Change ,,,,strStartupType
  objService.StopService()
Next
WScript.Quit
---------------------------------------------------------------------

If you copy that to a text document and rename it .vbs (eg. ChangeService.vbs), you can call it like this:

ChangeService.vbs "ServiceName" "StartupType"

It will stop the specified service and chage the startup type (Automatic, Manual or Disabled)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now