Solved

How to disable/enable a service in VB.Net

Posted on 2006-07-07
2
3,411 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
linearIn  challenge 23 65
copyEndy  challenge 15 54
create an incrementing variable name AutoHotKey 5 56
word0 challenge 3 55
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now