Solved

How to disable/enable a service in VB.Net

Posted on 2006-07-07
2
3,494 Views
Last Modified: 2012-05-05
Hello, I need a way to enable or disable an windows service programmatically, with .Net if possible.

Actually, I found a way to do it with sc.exe (from the resource kit):
sc config [ServiceName] start= demand (for manual or auto for automatic)

So I can just run that command.

But I would prefer finding another way than runing an external command.
It's already possible to control the services via the ServiceController class, but apparently not changing the startupType.
A call to an API would be ok if there's no such class.

I have also tried by editing the registry, but it seem the changes are not completely taken into account

HKEY_LOCAL_MACHINE System\CurrentControlSet\Services\<servicename>\Start
(0x00000000 = Boot
0x00000001 = System
0x00000002 = Automatic
0x00000003 = Manual
0x00000004 = Disable

but altough it seem to work when looking in the services manager, I still could not start the service after I changed those values.

(To check, disable a service via the service manager, change the value with regedit, if you refresh the manager, the startup type seem changed, but the service wont start as if still disabled)

So maybe if someone knows why the change is not immediate, and how to force it, it would also be ok.

Thanks for any help.
0
Comment
Question by:dapcom
2 Comments
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 17058361
Oddly enough, the ServiceController class can start/stop a service, but it cannot change the "StartMode".  So, I'd recommend that you use Windows Management Instrumentation (WMI) to disable a service.  Here is a chunk of code that demonstrates the concepts:

' add a reference to System.Management and System.ServiceProcess.dll
Imports System.ServiceProcess
Imports System.management

    Public Sub StartService(ByVal PC As String)
        Dim obj As ManagementObject
        Dim inParams, outParams As ManagementBaseObject
        Dim Result As Integer
        Dim sc As ServiceController

        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='TermService'")

        ' Change the Start Mode to Manual
        If obj("StartMode") = "Disabled" Then
            ' Get an input parameters object for this method
            inParams = obj.GetMethodParameters("ChangeStartMode")
            inParams("StartMode") = "Manual"

            ' do it!
            outParams = obj.InvokeMethod("ChangeStartMode", inParams, Nothing)
            Result = Convert.ToInt32(outParams("returnValue"))
            If Result <> 0 Then
                Throw New Exception("ChangeStartMode method error code " & Result)
            End If
        End If

        ' Start the service
        If obj("State") <> "Running" Then
            ' now start the service
            sc = New ServiceController("TermService", PC)
            sc.Start()
            sc.WaitForStatus(ServiceControllerStatus.Running)
        End If
    End Sub
0
 
LVL 1

Author Comment

by:dapcom
ID: 17098541
Thanks,

Very good, in addition to solving my problem elegantly, this opens a way to access the system I di not know.

Sorry if I took so long to accept, I'm on vacation right now:)
I'm eager to look closer at this management namespace..

dapcom
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

831 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