Link to home
Start Free TrialLog in
Avatar of shawl01
shawl01

asked on

Problem with services 500 pts!

I have a windows application and a windows service.  I need my windows application to be able to ensure that my windows service is always running.  I already have code to be able to start and stop the service but I need code to be able to change the startup type.  eg. if the user changes the service startup type to manual or disabled then my windows program changes it back to automatic and starts the service if its not running.
Avatar of bchoor
bchoor
Flag of United States of America image

There are some other methods/properties of Service Controller that you can dabble with.

Imports System.ServiceProcess

Module TestService

    Public serviceController As New ServiceController("Messenger")

    Public Sub Service_Start()
        If Service_Status() = ServiceControllerStatus.Stopped Then
            serviceController.Start()
            Debug.Print("Service started...")
        Else
            Debug.Print("Service is not stopped. Action cancelled.")
        End If
    End Sub

    Public Sub Service_Stop()
        If Service_Status() = ServiceControllerStatus.Running Then
            serviceController.Stop()
            Debug.Print("Service stopping...")
        Else
            Debug.Print("Service is not running. Action canlled.")
        End If
    End Sub

    Public Function Service_Status() As ServiceControllerStatus
        Return serviceController.Status
    End Function

End Module

Immediate Window Output:
?service_status
Stopped {1}
service_start
Service is starting...
?service_status
Running {4}
service_stop
Service stopping...
Avatar of shawl01
shawl01

ASKER

Hi bachoor,

Like I said, I already have the code for starting and stopping the service, but I need to be able to change the startup type.  My situation is that my software is being installed on computers that I have no control over and that are not locked down.  Users being users will always try and break stuff and so the only control I have is from functions within my program.  Hence if the users decides to change my service to disabled and need my program to be able to change it back to automatic and that's the bit that I am having trouble with.
Avatar of shawl01

ASKER

I have found the solution myself!!

The only way you can do this is to change a registry key manually

The Subkey you need to alter is:
Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\" + ServiceName)

The Key value you need to alter is:
key.GetValue("Start")

Appropiate values for this key are:
   Boot = 0
   System = 1
   Automatic = 2
   Manual = 3
   Disabled = 4


I have added this in the hope that it will help others looking for the same information.  I will now request that this question be closed without awarding points

ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial