Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to disable/enable a service in VB.Net

Posted on 2006-07-07
2
Medium Priority
?
3,727 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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
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 …
Introduction to Processes

705 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