Solved

Stopping Windows Service

Posted on 2006-11-20
5
316 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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