Solved

How to disable/enable a service in VB.Net

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

734 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