Solved

How to disable/enable a service in VB.Net

Posted on 2006-07-07
2
3,518 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is about my first experience with programming Arduino.
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…

808 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