VERSION 5.00Begin VB.Form frmMain BorderStyle = 1 'Fixed Single Caption = "PNP监视" 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://blog.csdn.net/chenhui530/" 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 = "PNP设备退出:" Height = 255 Left = 4440 TabIndex = 5 Top = 120 Width = 1095 End Begin VB.Label Label1 Caption = "PNP状态改变:" Height = 255 Left = 2280 TabIndex = 4 Top = 120 Width = 1095 End Begin VB.Label lUsbAdd Caption = "PNP插入:" Height = 255 Left = 120 TabIndex = 3 Top = 120 Width = 855 EndEndAttribute VB_Name = "frmMain"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalseOption Explicit
Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
Private WithEvents UsbAdd As SWbemSinkAttribute UsbAdd.VB_VarHelpID = -1Private WithEvents UsbLost As SWbemSinkAttribute UsbLost.VB_VarHelpID = -1Private WithEvents UsbExit As SWbemSinkAttribute UsbExit.VB_VarHelpID = -1
Private Sub Form_Initialize() InitCommonControlsEnd Sub
Private Sub cmdExit_Click() Unload MeEnd 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_PnPEntity'" objSWbemServices.ExecNotificationQueryAsync UsbLost, "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'" objSWbemServices.ExecNotificationQueryAsync UsbExit, "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'"End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) lLink.ForeColor = &HFF0000End 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.CancelEnd Sub
Private Sub lLink_Click() Shell "Explorer /s , Http://Chenhui.ylmf.cn", vbNormalFocusEnd Sub
Private Sub lLink_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) lLink.ForeColor = &HFF00FFEnd Sub
Private Sub UsbAdd_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet) listUsbAdd.AddItem objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("DeviceID").ValueEnd Sub
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 NextEnd Sub
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").ValueEnd Sub
'**************************************************'包括的方法:1,SetPowerState;2,Reset;3,GetDesciptor'**************************************************