|
VB 利用WMI进行USB监视收藏
VERSION 5.00
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = Usb监视
ClientHeight = 4350
ClientLeft = 45
ClientTop = 435
ClientWidth = 6990
Icon = frmMain.frx0000
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 = HttpChenhui.ylmf.cn
ForeColor = &H00FF0000&
Height = 255
Left = 1920
MouseIcon = frmMain.frx058A
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 = rootcimv2
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 , httpblog.csdn.netchenhui530, vbNormalFocus
End If
UsbAdd.Cancel
UsbLost.Cancel
UsbExit.Cancel
End Sub
Private Sub lLink_Click()
Shell Explorer s , httpblog.csdn.netchenhui530, 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
'
本文来自CSDN博客,转载请标明出处:httpblog.csdn.netchenhui530archive200710031810252.aspx |
|