关闭

VB 利用WMI进行服务监视

1424人阅读 评论(1) 收藏 举报

VERSION 5.00
Begin VB.Form frmMain
   Caption         =   "服务监视"
   ClientHeight    =   2730
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   3990
   LinkTopic       =   "Form1"
   ScaleHeight     =   2730
   ScaleWidth      =   3990
   StartUpPosition =   2  '屏幕中心
   Begin VB.CommandButton cmdAdd
      Caption         =   "添加"
      Height          =   375
      Left            =   2880
      TabIndex        =   2
      Top             =   120
      Width           =   975
   End
   Begin VB.TextBox textInput
      Height          =   375
      Left            =   120
      TabIndex        =   1
      Text            =   "请输入监视的服务名"
      Top             =   120
      Width           =   2415
   End
   Begin VB.ListBox listServices
      Height          =   1860
      Left            =   120
      TabIndex        =   0
      Top             =   720
      Width           =   3735
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private objSWbemServices As SWbemServices
Private WithEvents objSWbemSink As SWbemSink
Attribute objSWbemSink.VB_VarHelpID = -1

Private Sub cmdAdd_Click()
    If textInput.Text <> "" And textInput.Text <> "请输入监视的服务名" Then
        listServices.AddItem textInput.Text
        textInput.Text = ""
    Else
        MsgBox "请输入需要监视的服务名!!", vbCritical, "提示"
        textInput.Text = ""
        textInput.SetFocus
    End If
End Sub

Private Sub Form_Load()
    Dim strNameSpace As String
    strNameSpace = "root/cimv2" '指定命名空间为root/cimv2
    Set objSWbemSink = New SWbemSink
    Set objSWbemServices = GetObject("winmgmts://" & "." & "/" & strNameSpace)  '建立指定计算机、命名空间的WMI的SWbemServices 对象的引用
    objSWbemServices.ExecNotificationQueryAsync objSWbemSink, "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Service'" & " AND TargetInstance.State='Stopped'"
End Sub                                                                      '__InstanceDeletionEvent

Private Sub Form_Unload(Cancel As Integer)
    objSWbemSink.Cancel
End Sub

'进程创建事件
Private Sub objSWbemSink_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    'MsgBox objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value & "服务已经停止"
    Dim i As Integer, flag As Boolean
    For i = 0 To listServices.ListCount - 1
        If LCase(listServices.List(i)) = LCase(objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value) Then
            flag = True
            Exit For
        End If
    Next
    If flag = True Then
        Dim swMethods As SWbemMethodSet, swMethod As SWbemMethod, swServers As SWbemServices, swObjSet As SWbemObjectSet, swObj As SWbemObject
        'Dim swProperty As SWbemProperty, swPropertys As SWbemPropertySet
        Set swServers = GetObject("winmgmts://" & "." & "/" & "root/cimv2")
        Set swObjSet = swServers.ExecQuery("Select * From Win32_Service WHERE Name='" & objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value & "'")
        For Each swObj In swObjSet
            Set swMethods = swObj.Methods_
            swObj.ExecMethod_ ("StartService")
    '        For Each swMethod In swMethods
    ''            If LCase(swMethod.Name) = "startservice" Then
    '                Set swPropertys = swObj.Properties_
    '                For Each swProperty In swPropertys
    '                    MsgBox swProperty.Value
    '                Next
    ''            End If
    '            'MsgBox swMethod.Name
    '        Next
        Next
    End If
End Sub

Private Sub textInput_GotFocus()
    textInput.SelStart = 0
    textInput.SelLength = Len(textInput.Text)
End Sub
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:322166次
    • 积分:4429
    • 等级:
    • 排名:第6728名
    • 原创:81篇
    • 转载:0篇
    • 译文:2篇
    • 评论:431条
    文章分类
    最新评论
    chenhui530新浪博客