Tipp 0461 Dienst mit WMI ansteuern
Autor/Einsender:
Datum:
  Frank Grimm
14.09.2005
Entwicklungsumgebung:   VB 6
NT-basierte Windows-Versionen (Win 2000, Win XP) haben unter anderem die Dienste als große Neuerung eingeführt. Mit Hilfe der Windows Management Instrumentation (WMI) können diese angesteuert werden. Dieser Tipp zeigt wie man Dienste starten, stoppen, pausieren und weiterführen kann und fragt grundlegende Eigenschaften des Dienstes wie Anzeigenamen, Starttyp und Status an.
Eine detaillierte Beschreibung der WMI-Klasse Win32_Service mit allen Eigenschaften und Funktionen ist in der Online MSDN zu finden.
Code im Codebereich des Moduls
 
Option Explicit
Private m_objWMI As Object

Public Sub WMI_Init()
  Set m_objWMI = GetObject( _
      "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
End Sub

Public Sub WMI_Stop()
  Set m_objWMI = Nothing
End Sub

Public Sub ListServices(objListControl As Object)
  Dim objService As Object
  Dim colServices As Object

  If Not ((TypeOf objListControl Is ListBox) Or _
      (TypeOf objListControl Is ComboBox)) Then Exit Sub

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service")

  For Each objService In colServices
    objListControl.AddItem objService.Name
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Sub QueryServiceInformation( _
      ByVal strServiceName As String, _
      ByRef strServiceDisplayName As String, _
      ByRef strServicePathName As String, _
      ByRef strServiceType As String, _
      ByRef strServiceStartMode As String, _
      ByRef strServiceState As String)

  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    strServiceDisplayName = objService.DisplayName
    strServicePathName = objService.PathName
    strServiceType = objService.ServiceType
    strServiceStartMode = objService.StartMode
    strServiceState = objService.State
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Sub Service_Start(ByVal strServiceName As String)
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    objService.StartService
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Sub Service_Stop(ByVal strServiceName As String)
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    objService.StopService
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Sub Service_Pause(ByVal strServiceName As String)
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    objService.PauseService
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Sub Service_Resume(ByVal strServiceName As String)
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    objService.ResumeService
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Sub

Public Function Service_CanPause(ByVal strServiceName As String) _
      As Boolean
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    Service_CanPause = objService.AcceptPause
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Function

Public Function Service_CanStop(ByVal strServiceName As String) _
      As Boolean
  Dim objService As Object
  Dim colServices As Object

  Set colServices = m_objWMI.ExecQuery( _
      "Select * from Win32_Service where Name = '" & _
      strServiceName & "'")

  For Each objService In colServices
    Service_CanStop = objService.AcceptStop
  Next

  Set objService = Nothing
  Set colServices = Nothing
End Function
 
Weitere Links zum Thema
CPU-Daten ermitteln (WMI)
Hinweis
Um diesen Tipp ausführen zu können, muss WMI installiert sein. Dies ist bei Windows 2000 und Windows XP standardmäßig der Fall. Für Windows NT 4.0 kann hier ein entsprechendes Installationspaket heruntergeladen werden, das eine vergleichbare Funktionalität bietet.

Windows-Version
95
98
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (6,1 kB) Downloads bisher: [ 738 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Mittwoch, 7. September 2011