VB 利用WMI进行USB监视

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

VERSION 5.00
Begin VB.Form frmMain
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Usb监视"
   ClientHeight    =   4350
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   6990
   Icon            =   "frmMain.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4350
   ScaleWidth      =   6990
   StartUpPosition =   2  '屏幕中心
   Begin VB.CommandButton cmdExit
      Cancel          =   -1  'True
      Caption         =   "退出(&X)"
      Height          =   375
      Left            =   5880
      TabIndex        =   6
      Top             =   3720
      Width           =   975
   End
   Begin VB.ListBox listUsbExit
      Height          =   3120
      Left            =   2400
      TabIndex        =   2
      Top             =   360
      Width           =   2175
   End
   Begin VB.ListBox listUsbLost
      Height          =   3120
      Left            =   4680
      TabIndex        =   1
      Top             =   360
      Width           =   2175
   End
   Begin VB.ListBox listUsbAdd
      Height          =   3120
      Left            =   120
      TabIndex        =   0
      Top             =   360
      Width           =   2175
   End
   Begin VB.Label lLink
      Caption         =   "Http://Chenhui.ylmf.cn"
      ForeColor       =   &H00FF0000&
      Height          =   255
      Left            =   1920
      MouseIcon       =   "frmMain.frx":058A
      MousePointer    =   99  'Custom
      TabIndex        =   8
      Top             =   3840
      Width           =   2295
   End
   Begin VB.Label lWelCome
      Caption         =   "欢迎访问我的博客:"
      Height          =   255
      Left            =   120
      TabIndex        =   7
      Top             =   3860
      Width           =   1695
   End
   Begin VB.Label Label2
      Caption         =   "Usb非法退出:"
      Height          =   255
      Left            =   4440
      TabIndex        =   5
      Top             =   120
      Width           =   1095
   End
   Begin VB.Label Label1
      Caption         =   "Usb正常退出:"
      Height          =   255
      Left            =   2280
      TabIndex        =   4
      Top             =   120
      Width           =   1095
   End
   Begin VB.Label lUsbAdd
      Caption         =   "Usb插入:"
      Height          =   255
      Left            =   120
      TabIndex        =   3
      Top             =   120
      Width           =   855
   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 Declare Sub InitCommonControls Lib "comctl32.dll" ()

Private WithEvents UsbAdd As SWbemSink
Attribute UsbAdd.VB_VarHelpID = -1
Private WithEvents UsbLost As SWbemSink
Attribute UsbLost.VB_VarHelpID = -1
Private WithEvents UsbExit As SWbemSink
Attribute UsbExit.VB_VarHelpID = -1

Private Sub Form_Initialize()
    InitCommonControls
End Sub

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub Form_Load()
    Dim objSWbemServices As SWbemServices
    Dim strNameSpace As String
    strNameSpace = "root/cimv2"
    Set UsbAdd = New SWbemSink
    Set UsbLost = New SWbemSink
    Set UsbExit = New SWbemSink
    Set objSWbemServices = GetObject("winmgmts://" & "." & "/" & strNameSpace)
    objSWbemServices.ExecNotificationQueryAsync UsbAdd, "SELECT * FROM __instancecreationevent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBHub'"
    objSWbemServices.ExecNotificationQueryAsync UsbLost, "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBHub'"
    objSWbemServices.ExecNotificationQueryAsync UsbExit, "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBHub'"
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lLink.ForeColor = &HFF0000
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If MsgBox("访问作者博客吗??", vbQuestion + vbYesNo, "提示") = vbYes Then
        Shell "Explorer /s , http://blog.csdn.net/chenhui530/", vbNormalFocus
    End If
    UsbAdd.Cancel
    UsbLost.Cancel
    UsbExit.Cancel
End Sub

Private Sub lLink_Click()
    Shell "Explorer /s , http://blog.csdn.net/chenhui530/", vbNormalFocus
End Sub

Private Sub lLink_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    lLink.ForeColor = &HFF00FF
End Sub

'USB被插入的事件
Private Sub UsbAdd_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    listUsbAdd.AddItem objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").Value
End Sub

'USB被非法拔出的事件
Private Sub UsbLost_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    listUsbLost.AddItem objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").Value
    Dim i As Integer
    For i = 0 To listUsbAdd.ListCount - 1
        If listUsbAdd.List(i) = objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").Value Then
            listUsbAdd.RemoveItem i
            Exit For
        End If
    Next
End Sub

'USB正常退出的事件
Private Sub UsbExit_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    Dim i As Integer
    For i = 0 To listUsbAdd.ListCount - 1
        If listUsbAdd.List(i) = objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").Value Then
            listUsbAdd.RemoveItem i
            Exit For
        End If
    Next
    listUsbExit.AddItem objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").Value
End Sub

'**************************************************
'包括的方法:1,SetPowerState;2,Reset;3,GetDesciptor
'**************************************************
 

HDFS数据快速拷贝方案:FastCopy

前言 我们在使用HDFS的时候,往往有的时候需要做一些临时的数据拷贝操作,如果是在同一个集群中的,我们直接用HDFS内部自带的cp这些命令即可;如果是跨集群的时候或者说待拷贝数据量规模非常大的时...

C#: WMI 获取远程 Windows 主机信息

起步文档:WMI 基本介绍WMI调用基本步骤一个简单的远程访问例子:(参考自MSDN: How To: Connect to a Remote Computer)using System; using...

C#:基于WMI监视USB插拔

参考资料: USB Port Insert / Remove detection using WMI USB Port Insert / Remove detection using WMI (S...
  • dz45693
  • dz45693
  • 2011年08月18日 15:43
  • 4444

C#:基于WMI监视USB插拔

参考资料: USB Port Insert / Remove detection using WMI USB Port Insert / Remove detection using WMI ...
  • jhqin
  • jhqin
  • 2011年08月18日 15:20
  • 7796

C#:基于WMI监视USB插拔

作者:Splash 转自:http://blog.csdn.net/jhqin/article/details/6698411 参考资料: USB Port Insert / Remove ...
  • whw8007
  • whw8007
  • 2014年02月28日 08:30
  • 1111

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

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

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

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

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

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

C#:基于WMI查询USB设备

/* ---------------------------------------------------------- 文件名称:WMIUsbQuery.cs 作者:秦建辉 MSN:splas...
  • jhqin
  • jhqin
  • 2011年08月31日 09:46
  • 13227

C#:基于WMI查询USB设备

作者:Splash 转自:http://blog.csdn.net/jhqin/article/details/6734673 [csharp] view plaincopy ...
  • whw8007
  • whw8007
  • 2014年02月28日 08:31
  • 876
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB 利用WMI进行USB监视
举报原因:
原因补充:

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