Solved

Stopping Windows Service

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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