Windows Service Monitoring Problem On Vista
I've built a small utility to monitor the status of SQL Server Express and start/stop it and it works nicely on Windows XP SP2 and fuctioning great but when I've tested it on windows vista it monitors the service ok but I can't start/stop the service any more unless I use the Run as administrator command to start my tool
I've used the folowing code to test my rights on windows
My
.User.IsInRole(Microsoft.VisualBasic.ApplicationServices.BuiltInRole.Administrator)but it gives me false - note that I've logged as an administrator to windows -
is there is any way programtically make the program have administrator rights on windows so I can use all functionality of my little tool
You could try using WMI to stop and start services. You can stop/start/pause as well as watch for a change in status for any or specific services with WMI.
Example using Spooler service:
Imports System.Management
Private StopSpooler()
Dim searcher As New ManagementObjectSearcher("\\.\root\CIMV2", "SELECT * FROM Win32_Service WHERE Name = 'Spooler'")
Dim queryObj As ManagementObject In searcher.Get()
If queryObj("Started").ToString = "True" Then
Dim classInstance As New ManagementObject("root\CIMV2", Win32_Service.Name='Spooler'", Nothing)
Dim outParams As ManagementBaseObject = classInstance.InvokeMethod("StopService", Nothing, Nothing)
If outParams("ReturnValue") <> 0 Then
... Failed , use try catch to get details...
EndIf
End If
searcher.Dispose
End Sub
To start the spooler change StopService to StartService and Stop to Start in the IF. You can also create and event handler to manage most events on the PC one of which is service state changes so you would know when the event happened.
To insert a handler to pickup an event change:
Imports System.Management
Private WithEvents m_SpoolerWatcher As ManagementEventWatcher
Private SpoolerState As String
Private Sub SpoolerServiceEventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_SpoolerWatcher.EventArrived
Dim mbo, obj As ManagementBaseObject
mbo = CType(e.NewEvent, ManagementBaseObject)
'
' Get a copy of the instance that fired the event
'
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
'
' If it was Spooler, inform user and save state
'
If obj("Name").ToString = "Spooler" Then
If obj("Started").ToString = "True" Then
MsgBox("Spooler Started Running", MsgBoxStyle.Information)
Else
MsgBox("Spooler Stopped Running", MsgBoxStyle.Exclamation)
End If
SpoolerState= queryObj("Started").ToString
End If
End Sub
Private Sub InitialiseWatcher()
'
' Get current state of Spooler
'
Dim searcher As New ManagementObjectSearcher("\\.\root\CIMV2", "SELECT * FROM Win32_Service WHERE Name='Spooler'")
'
' Following will iterate once due to WHERE clause
'
For Each queryObj As ManagementObject In searcher.Get()
SpoolerState= queryObj("Started").ToString
Next
'
' Setup Event Handler for Service events
'
Dim query1 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_Service'")
m_SpoolerWatcher = New ManagementEventWatcher
m_SpoolerWatcher.Query = query1
m_SpoolerWatcher.Start()
End Sub
BEFORE YOUR APPLICATION EXITS:
m_SpoolerWatcher.Stop()