鸿 网 互 联 www.68idc.cn

MFC U盘检测

来源:互联网 作者:佚名 时间:2018-02-10 22:53
WM_DEVICECHANGE消息 查阅MSDN得知: The framework calls this member function to notify an application or device driver of a change to the hardware configuration of a device or the computer. 即:框架调用这个函数来通知应用程序或者设备驱动程序
  •  WM_DEVICECHANGE消息

    查阅MSDN得知:

      The framework calls this member function to notify an application or device driver of a change to the hardware configuration of a device or the computer.

    即:框架调用这个函数来通知应用程序或者设备驱动程序设备硬件配置或者电脑的配置被改变了。

    所以,检测U盘的插入和拔出也就相当于侦测处理WM_DEVICECHANGE消息。

 

  •  获取WM_DEVICECHANGE消息的方法

    WM_DEVICECHANGE的使用方式有两种,一种是通过WindowProc()的方式,另一种是通过OnDeviceChange()消息响应函数的方式,底下就先介紹第一種方式,再介绍第二种。

 

  1. 通过WindowProc()的方式检测U盘

          这里通过一个简单的Demo来说明

          程序界面如下:

                                                      

          需要包含的头文件:

#include <dbt.h>
#include <winioctl.h> 

    重写WindowProc()函数实现U盘检测,代码如下:

 1 char FirstDriveFromMask(ULONG unitmask) //獲取盤符
 2 {
 3     char i;
 4     for (i = 0; i < 26; ++i)
 5     {
 6         if (unitmask & 0x1)
 7             break;
 8         unitmask = unitmask >> 1;
 9     }
10     return (i + 'A');
11 }
12 
13 
14 LRESULT CUpanDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
15 {
16     CString detectMsg;
17     
18     switch (message)
19     {
20         //WM_DEVICECHANGE,系统硬件改变发出的系统消息
21     case WM_DEVICECHANGE:
22         {
23             PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)lParam;
24             switch(wParam)
25             {
26             case WM_DEVICECHANGE:
27                 break;
28             case DBT_DEVICEARRIVAL://设备检测结束,并且可以使用
29                 {
30                     if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
31                     {
32                         PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
33                         switch(lpdbv->dbcv_flags)
34                         {
35                         case 0:                //U盘
36                             {
37                                 CString decDriver;
38                                 decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
39                                 detectMsg.Format(_T("检测到U盘:[%s]插入!"), decDriver.GetBuffer(0)); 
40                                 m_editControl.SetWindowText(detectMsg);
41                             }
42                             break;
43                         case DBTF_MEDIA:    //光盘
44                             break;
45                         }
46                     }
47                 }
48                 break;
49             case DBT_DEVICEREMOVECOMPLETE://设备卸载或者拔出
50                 {
51                     if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
52                     {
53                         PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
54                         switch(lpdbv->dbcv_flags)
55                         {
56                         case 0:                //U盘
57                             {
58                                 CString decDriver;
59                                 decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
60                                 detectMsg.Format(_T("检测到U盘:[%s]拔出!"), decDriver.GetBuffer(0)); 
61                                 m_editControl.SetWindowText(detectMsg);
62                             }
63                             break;
64                         case DBTF_MEDIA:    //光盘
65 
66                             break;
67                         }
68                     }
69                 }
70                 break;
71             }
72         }
73         break;
74     }
75 
76     return CDialog::WindowProc(message, wParam, lParam);
77 }
View Code

         程序运行结果如下:

      插入U盘时:

                    

      拔出U盘时:

                    

 

  

  2.  通过OnDeviceChange()的方式检测U盘

首先在头文件声明一个消息函数:

afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);

消息映射:

BEGIN_MESSAGE_MAP(OGrgFrmRepair, CDialog)

    ON_WM_DEVICECHANGE()

END_MESSAGE_MAP()

实现:

 1 char FirstDriveFromMask(ULONG unitmask) //獲取盤符
 2 {
 3     char i;
 4     for (i = 0; i < 26; ++i)
 5     {
 6         if (unitmask & 0x1)
 7             break;
 8         unitmask = unitmask >> 1;
 9     }
10     return (i + 'A');
11 }
12 
13 
14 BOOL CUpanDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
15 {
16     CString detectMsg;
17     PDEV_BROADCAST_HDR lpdb=(PDEV_BROADCAST_HDR)dwData;
18 
19     switch (nEventType)
20     {
21     case DBT_DEVICEREMOVECOMPLETE:
22         {
23             if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
24             {
25                 PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
26                 switch(lpdbv->dbcv_flags)
27                 {
28                 case 0:                //U盘
29                     {
30                         CString decDriver;
31                         decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
32                         detectMsg.Format(_T("检测到U盘:[%s]拔出!"), decDriver.GetBuffer(0)); 
33                         m_editControl.SetWindowText(detectMsg);
34                     }
35                     break;
36                 case DBTF_MEDIA:    //光盘
37 
38                     break;
39                 }
40             }
41         }
42         break;
43     case DBT_DEVICEARRIVAL:
44         {
45             if(lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)//逻辑卷
46             {
47                 PDEV_BROADCAST_VOLUME lpdbv =  (PDEV_BROADCAST_VOLUME)lpdb;
48                 switch(lpdbv->dbcv_flags)
49                 {
50                 case 0:                //U盘
51                     {
52                         CString decDriver;
53                         decDriver = FirstDriveFromMask(lpdbv ->dbcv_unitmask);
54                         detectMsg.Format(_T("检测到U盘:[%s]插入!"), decDriver.GetBuffer(0)); 
55                         m_editControl.SetWindowText(detectMsg);
56                     }
57                     break;
58                 case DBTF_MEDIA:    //光盘
59                     break;
60                 }
61             }
62         }
63         break;
64     }
65 
66     return TRUE;
67 
68 }
View Code

 

网友评论
<