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

[840 byte] By [SamerSelo] at [2007-12-28]
# 1

Yes. You may need to have simple file sharing turned off in Explorer for this.

Find the excutable assembly with Explorer. Examine the file's properties and look at the select the security tab. You may then assign execution to the adminstrators group.

ReneeC at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thnak you

I did that but I want a programatically way so I won't have to alter security on every PC I want to run my Tool On it

SamerSelo at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

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()

Rabtok at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Thank You

The Same Problem

SamerSelo at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Btw, this does work on Vista:

My.User.IsInRole("Administrators")

ReneeC at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6

Btw, this does work on Vista:

My.User.IsInRole("Administrators")

Yes this work on Vista but the problem as I wrote in the first this return False even when I login to windows as an administrator

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

SamerSelo at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7
Use an application manifest to always have your program run as Administrator. Then you won't have to do 'Run as Adminstrator.'

http://msdn.microsoft.com/msdnmag/issues/07/01/UAC/default.aspx#S8

twotoed at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

Thank You Alot

But I Hoped of programatically solition

SamerSelo at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...