关闭

VB 利用WMI进行外界设备插入分配盘符监视

1511人阅读 评论(2) 收藏 举报

VERSION 5.00
Begin VB.Form frmMain
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private WithEvents CreateDriveEvent As SWbemSink
Attribute CreateDriveEvent.VB_VarHelpID = -1
Private WithEvents DeleteDriveEvent As SWbemSink
Attribute DeleteDriveEvent.VB_VarHelpID = -1
Private WithEvents ModificationDriveEvent As SWbemSink
Attribute ModificationDriveEvent.VB_VarHelpID = -1
Private objSWbemServices As SWbemServices

Private Sub Form_Load()
    StartMonitorCreateDriveEvent
    StartMonitorDeleteDriveEvent
'    StartMonitorModificationDriveEvent
End Sub


Private Sub Form_Unload(Cancel As Integer)
    CreateDriveEvent.Cancel
    DeleteDriveEvent.Cancel
'    ModificationDriveEvent.Cancel
End Sub

'驱动器创建事件
Private Sub CreateDriveEvent_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)

    MsgBox objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value

End Sub
'dim ss as WbemScripting.
 

'驱动器删除事件
Private Sub DeleteDriveEvent_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    MsgBox objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value
End Sub

'驱动器属性变更事件
Private Sub ModificationDriveEvent_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    MsgBox "修改了" & objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Name").Value
End Sub


Private Sub StartMonitorCreateDriveEvent()
    Set CreateDriveEvent = New SWbemSink
    Set objSWbemServices = GetObject("winmgmts://./root/cimv2")
    objSWbemServices.ExecNotificationQueryAsync CreateDriveEvent, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"
End Sub

Private Sub StartMonitorDeleteDriveEvent()
    Set DeleteDriveEvent = New SWbemSink
    Set objSWbemServices = GetObject("winmgmts://./root/cimv2")
    objSWbemServices.ExecNotificationQueryAsync DeleteDriveEvent, "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"
End Sub

Private Sub StartMonitorModificationDriveEvent()
    Set ModificationDriveEvent = New SWbemSink
    Set objSWbemServices = GetObject("winmgmts://./root/cimv2")
    objSWbemServices.ExecNotificationQueryAsync ModificationDriveEvent, "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"
End Sub
 

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:321759次
    • 积分:4425
    • 等级:
    • 排名:第6694名
    • 原创:81篇
    • 转载:0篇
    • 译文:2篇
    • 评论:431条
    文章分类
    最新评论
    chenhui530新浪博客