Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stopping Windows Service

Posted on 2006-11-20
5
Medium Priority
?
321 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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