?
Solved

Stopping Windows Service

Posted on 2006-11-20
5
Medium Priority
?
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17985520
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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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