ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 6789|回复: 15

[分享] U盘相关

[复制链接]

TA的精华主题

TA的得分主题

发表于 2013-6-7 21:26 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:WMI应用
    做一个小程序,需要实时检测是否插入指定的U盘,用到了WMI时间,想到以前的帖子http://club.excelhome.net/forum.php?mod=viewthread&tid=733445&extra=page%3D1%26filter%3Dauthor%26orderby%3Ddateline%26digest%3D1%26digest%3D1%26orderby%3Ddateline最后没有介绍订阅事件,精简一下移植到vba,简单介绍一下,放到这里权当补充吧。

   声明一下,关于用U盘做钥匙盘及禁用U盘,论坛已经有很多讨论,这里只是演示wmi事件的使用,无意引入以上讨论。两个示例都是禁用U盘,但只对代码运行后插入U盘起作用,之前插入的不起作用。

1、监测是否插入U盘,如果有就自动弹出插入的U盘。
Public WithEvents wmiEvent As SWbemSink
Private Declare Function DeviceIoControl Lib "kernel32.dll" (ByVal hDevice As Long, ByRef dwIoControlCode As Long, _
               ByRef lpInBuffer As Any, ByVal nInBufferSize As Long, ByRef lpOutBuffer As Any, ByVal nOutBufferSize As Long, _
               ByRef lpBytesReturned As Long, ByRef lpOverlapped As Long) As Long
Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, _
               ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As Long, _
               ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const EJECT_MEDIA As Long = &H202
Private Const FILE_DEVICE_MASS_STORAGE As Long = &H2D&
Private Sub CommandButton1_Click()
    Dim wmiLocator As New SWbemLocator
    Dim wmiServices As SWbemServices
    Set wmiEvent = New SWbemSink
    Set wmiServices = wmiLocator.ConnectServer(".", "root\CIMV2")
    wmiServices.ExecNotificationQueryAsync wmiEvent, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'"
End Sub
Private Sub CommandButton2_Click()
    If Not wmiEvent Is Nothing Then
        wmiEvent.Cancel
        Set wmiEvent = Nothing
    End If
End Sub
Private Sub Eject(ByVal sName As String)
    Dim lngFunction As Long
    Dim lngReturn As Long
    Dim lngHand As Long
    Dim lngFlag As Long
    lngFlag = GENERIC_READ Or GENERIC_WRITE
    Do
        lngHand = CreateFile(sName, lngFlag, 0, ByVal CLng(0), 3, ByVal CLng(0), ByVal CLng(0))
        If lngHand <> -1 Then Exit Do
    Loop
    lngFunction = (FILE_DEVICE_MASS_STORAGE * (2 ^ 16)) Or (1 * (2 ^ 14)) Or (EJECT_MEDIA * (2 ^ 2)) Or 0
    DeviceIoControl lngHand, ByVal lngFunction, 0, 0, 0, 0, lngReturn, ByVal 0
End Sub
Private Sub wmiEvent_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    Eject ("\\.\" & objWbemObject.Properties_.Item("TargetInstance").Value.Properties_.Item("Caption").Value)
End Sub



2、这个方法网上资料很多,缺点也不少。修改、监测和U盘有关的注册表项,如果注册表项被改动就自动改回原值,起到禁用的效果。也可以把代码中的注册表项换成其他你需要监测的注册表项。
Public WithEvents wmiEvent As SWbemSink
Const HKEY_LOCAL_MACHINE = &H80000002
Dim wmiLocator As New SWbemLocator
Dim wmiServices As SWbemServices
Dim wMIObject As SWbemObject
Private Sub CommandButton1_Click()
    Set wmiEvent = New SWbemSink
    Set wmiServices = wmiLocator.ConnectServer(".", "root\default")
    Set wMIObject = wmiServices.Get("StdRegProv")
    wMIObject.setDWORDValue HKEY_LOCAL_MACHINE, _
    "SYSTEM\CurrentControlSet\services\USBSTOR\", "Start", 4
    wmiServices.ExecNotificationQueryAsync wmiEvent, "SELECT * FROM RegistryKeyChangeEvent WITHIN 1" & _
    "WHERE Hive='HKEY_LOCAL_MACHINE' AND KeyPath='SYSTEM\\CurrentControlSet\\services\\USBSTOR'"
End Sub
Private Sub CommandButton2_Click()
    If Not wmiEvent Is Nothing Then
        wmiEvent.Cancel
        Set wmiEvent = Nothing
        wMIObject.setDWORDValue HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\services\USBSTOR\", "Start", 3
        Set wMIObject = Nothing
        Set wmiServices = Nothing
        Set wmiLocator = Nothing
    End If
End Sub
Private Sub wmiEvent_OnObjectReady(ByVal objWbemObject As WbemScripting.ISWbemObject, ByVal objWbemAsyncContext As WbemScripting.ISWbemNamedValueSet)
    Dim sValue
    wMIObject.GetDWORDValue HKEY_LOCAL_MACHINE, _
    "SYSTEM\CurrentControlSet\services\USBSTOR\", "Start", sValue
    If sValue <> 4 Then
        wMIObject.setDWORDValue HKEY_LOCAL_MACHINE, _
        "SYSTEM\CurrentControlSet\services\USBSTOR\", "Start", 4
        MsgBox "注册表:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\USBSTOR\" & vbCrLf & _
        "下的键“Start”的值被修改!但是该键值已经强制重新设为4,即禁止U盘。"
    End If
End Sub

Desktop.rar

27.21 KB, 下载次数: 226

评分

2

查看全部评分

TA的精华主题

TA的得分主题

发表于 2013-6-7 23:55 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2013-6-8 12:59 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
下载试试,谢谢版主分享。

TA的精华主题

TA的得分主题

发表于 2015-6-8 21:41 | 显示全部楼层
{:soso_e142:}L太强大了,你的WMI帖子我都观摩过了,再次感谢

TA的精华主题

TA的得分主题

发表于 2015-6-9 09:30 | 显示全部楼层
下载学习 ,谢谢版主分享。

TA的精华主题

TA的得分主题

发表于 2015-6-9 14:39 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-6-9 21:21 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-6-12 21:53 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2015-11-1 11:07 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
感谢版主,学习ing~

TA的精华主题

TA的得分主题

发表于 2018-7-31 16:53 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-22 00:03 , Processed in 0.041004 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表