VB 利用WMI进行服务监视

原创 2007年10月03日 11:25:00

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
 

ring3下利用WMI监视进程创建(vc版)

#include "stdafx.h" #define _WIN32_DCOM #include using namespace std; #include #include # pragma...
  • zwfgdlc
  • zwfgdlc
  • 2011年07月18日 11:02
  • 3061

wmi服务修复

  • 2015年09月22日 16:02
  • 607B
  • 下载

vb.net中使用wmi关闭远程计算机上的进程

最近学习了一点wmi知识,感觉还很好用,只是在结束远程计算机上的程序时花费了一些时间,在网上找了几天资料,发现大多数是用C#写的,要不就是脚本写的,其中有很多代码都把停止服务当做是结束进程,也有很多人...

VB中使用WMI获取系统硬件和软件有关信息

原帖地址:http://blog.csdn.net/ericbai/article/details/474308 WMI是英文Windows Management Instrum...

windows系统服务:wmi應用

  • 2008年04月28日 03:13
  • 25KB
  • 下载

【日记】利用VB.NET实现MYSQL和MSSQL进行批量插入记录的方法及比较

最近为了完成将股票交易数据从二进制文件读取添加到数据库中,可谓千辛万苦(以前数据量小,不需要考虑太多嘛) 言归正传:股票交易数据格式为,股票代码,时间,交易数据,时间,交易数据.....开始另一只股...

利用VB.Net 通过Windows Sockets (Winsock)以及多线程编程进行桌面电脑与Pocket PC间的双向通信

利用VB.Net编程实现PC与掌上电脑PPC间的双向通信 [源文件下载]http://www.cnblogs.com/Risen/category/110585.html本文介绍如何利用VB.Net ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB 利用WMI进行服务监视
举报原因:
原因补充:

(最多只允许输入30个字)