VB 查找U盘是否插入

VB 查找U盘是否插入,第1张

概述Option Explicit‘子类化窗体消息处理函数时需要使用的API,很常见,不作过多说明。Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd  As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare F

Option Explicit‘子类化窗体消息处理函数时需要使用的API,很常见,不作过多说明。Declare Function SetwindowLong lib "user32" Alias "SetwindowLongA" (ByVal hwnd As Long,ByVal nIndex As Long,ByVal DWNewLong As Long) As LongDeclare Function CallWindowProc lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long,ByVal hwnd As Long,ByVal Msg As Long,ByVal wParam As Long,ByVal lParam As Long) As LongDeclare Sub copyMemory lib "kernel32" Alias "RtlMoveMemory" (pDst As Any,pSrc As Any,ByVal ByteLen As Long)Const GWL_WNDPROC = -4Const WM_DEVICECHANGE As Long = &H219Const DBT_DEVICEARRIVAL As Long = &H8000&Const DBT_DEVICEREMOVECOMPLETE As Long = &H8004&'设备类型:逻辑卷标Const DBT_DEVTYP_VolUME As Long = &H2'与WM_DEVICECHANGE消息相关联的结构体头部信息Private Type DEV_broADCAST_HDRlSize As LonglDevicetype As Long '设备类型lReserved As LongEnd Type'设备为逻辑卷时对应的结构体信息Private Type DEV_broADCAST_VolUMElSize As LonglDevicetype As Longlreserved As LonglunitMask As Long '和逻辑卷标对应的掩码iFlag As Integer End TypePublic info As DEV_broADCAST_HDRPublic info_volume As DEV_broADCAST_VolUMEPublic PrevProc As Long ‘原来的窗体消息处理函数地址Public Sub HookForm(F As Form) PrevProc = SetwindowLong(F.hwnd,GWL_WNDPROC,AddressOf WindowProc)End SubPublic Sub UnHookForm(F As Form) SetwindowLong F.hwnd,PrevProcEnd SubPublic Function WindowProc(ByVal hwnd As Long,ByVal uMsg As Long,ByVal lParam As Long) As Long Select Case uMsg '插入USB disK 则接收到此消息 Case WM_DEVICECHANGE If wParam = DBT_DEVICEARRIVAL Then '若插入USBdisK或者映射网络盘等则 'info.lDevicetype =2 '即DBT_DEVTYP_VolUME ‘利用参数lParam获取结构体头部信息 copyMemory info,ByVal lParam,Len(info) If info.lDevicetype = DBT_DEVTYP_VolUME Then copyMemory info_volume,Len(info_volume) '检测到有逻辑卷添加到系统中,则显示该设备根目录下全部文件名 Listfiles Chr(GetDrivename(info_volume.lUnitMask)) & ":/",Form1.List1 End If End If If wParam = DBT_DEVICEREMOVECOMPLETE Then '若移走USBdisK或者映射网络盘等则 'info.lDevicetype =2 '即DBT_DEVTYP_VolUME ‘利用参数lParam获取结构体头部信息 copyMemory info,Len(info) If info.lDevicetype = DBT_DEVTYP_VolUME Then copyMemory info_volume,Len(info_volume) '清除List中的内容 Form1.List1.Clear End If End If End Select ' 调用原来的窗体消息处理函数 WindowProc = CallWindowProc(PrevProc,hwnd,uMsg,wParam,lParam) End Function'根据输入的32位LONG型数据(只有一位为1)返回对应的卷标的ASCII数值'规则是1:A、2:B、4:C等等Function GetDrivename(ByVal lUnitMask As Long) As ByteDim i As Longi = 0While lUnitMask Mod 2 <> 1 lUnitMask = lUnitMask / 2 i = i + 1WendGetDrivename = Asc("A") + IEnd Function'显示插入逻辑卷根目录的文件名列表,需要在工程里引用Microsoft Scripting Runtime库。Function Listfiles(strPath As String,ByRef List As ListBox) Dim fso As New Scripting.fileSystemObject Dim objFolder As Folder Dim objfile As file Set objFolder = fso.GetFolder(strPath) For Each objfile In objFolder.files List.AddItem objfile.name NextEnd Function窗体Form1代码:Option ExplicitPrivate Sub Form_Load()'子类化窗体的消息处理函数HookForm MeEnd SubPrivate Sub Form_Unload(Cancel As Integer)'程序退出时恢复原窗体处理函数UnHookForm MeEnd Sub效果图:备注:本示例程序不仅仅能检测U盘的插入,对CDROM、网络映射盘等设备也会作出同样的反应,如果需要只检测U盘,则需要在If info.lDevicetype = DBT_DEVTYP_VolUME 处再对iFlag结构成员作检测,其数值为0时表示设备为U盘。另外根据微软的解释,软盘的插拔是不会有引发该消息的,原因是只有支持软d出技术的设备才会引发该消息。(原文:Messages for media arrival and removal are sent only for media in devices that support a soft-eject mechanism. )本演示程序在windows98、XP系统下调试通过。

总结

以上是内存溢出为你收集整理的VB 查找U盘是否插入全部内容,希望文章能够帮你解决VB 查找U盘是否插入所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294140.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存