Mfc usb flash disk detection and mfc Detection

Source: Internet
Author: User

Mfc usb flash disk detection and mfc Detection

  • WM_DEVICECHANGE message

Read MSDN and learn:

      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.

That is, the Framework calls this function to notify the application or device driver that the hardware configuration or computer configuration has been changed.

Therefore, detecting the insertion and removal of a USB flash drive is equivalent to detecting and processing the WM_DEVICECHANGE message.

 

  • How to obtain the WM_DEVICECHANGE message

WM_DEVICECHANGE can be used in two ways: WindowProc () and OnDeviceChange, next we will introduce the first method, and then introduce the second method.

 

Here is a simple Demo to illustrate

The program interface is as follows:

Header files to be included:

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

Rewrite the WindowProc () function to implement USB flash disk detection. The Code is as follows:

1 char FirstDriveFromMask (ULONG unitmask) // returns the Escape Character 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, system message sent from system hardware change 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: // The device detection is complete, you can also use 29 {30 if (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume 31 {32 PDEV_BROADCAST_VOLUME lpdbv = (volume) lpdb; 33 switch (lpdbv-> dbcv_flags) 34 {35 case 0: // U disk 36 {37 CString decDriver; 38 decDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask); 39 detectMsg. format (_ T ("Detection To USB flash drive: [% s] inserted! "), DecDriver. getBuffer (0); 40 m_editControl.SetWindowText (detectMsg); 41} 42 break; 43 case DBTF_MEDIA: // disc 44 break; 45} 46} 47} 48 break; 49 case DBT_DEVICEREMOVECOMPLETE: // device unmount or unplug 50 {51 if (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume 52 {53 PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lpdb; 54 switch (lpdbv-> dbcv_flags) 55 {56 case 0: // U disk 57 {58 CString decDriver; 59 decDriver = FirstDrive FromMask (lpdbv-> dbcv_unitmask); 60 detectMsg. Format (_ T ("detected U Disk: [% s] unplugging! "), DecDriver. getBuffer (0); 61 m_editControl.SetWindowText (detectMsg); 62} 63 break; 64 case DBTF_MEDIA: // disc 65 66 break; 67} 68} 69} 70 break; 71} 72} 73 break; 74} 75 76 return CDialog: WindowProc (message, wParam, lParam); 77}View Code

The program running result is as follows:

When inserting a USB flash drive:

                    

When pulling out the USB flash drive:

                    

 

  

2. Use the OnDeviceChange () method to detect the USB flash disk

First, declare a message function in the header file:

afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);

Message ing:

BEGIN_MESSAGE_MAP(OGrgFrmRepair, CDialog)    ON_WM_DEVICECHANGE()END_MESSAGE_MAP()

Implementation:

1 char FirstDriveFromMask (ULONG unitmask) // returns the Escape Character 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 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 (l Pdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume 24 {25 PDEV_BROADCAST_VOLUME lpdbv = (volume) lpdb; 26 switch (lpdbv-> dbcv_flags) 27 {28 case 0: // U disk 29 {30 CString decDriver; 31 decDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask); 32 detectMsg. format (_ T ("detected USB flash drive: [% s] unplugging! "), DecDriver. getBuffer (0); 33 m_editControl.SetWindowText (detectMsg); 34} 35 break; 36 case DBTF_MEDIA: // disc 37 38 break; 39} 40} 41} 42 break; 43 case DBT_DEVICEARRIVAL: 44 {45 if (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume 46 {47 bytes lpdbv = (bytes) lpdb; 48 switch (lpdbv-> dbcv_flags) 49 {50 case 0: // U disk 51 {52 CString decDriver; 53 decDriver = FirstDriveFromMask (lpdbv -> Dbcv_unitmask); 54 detectMsg. Format (_ T ("detected USB flash disk: [% s] inserted! "), DecDriver. getBuffer (0); 55 m_editControl.SetWindowText (detectMsg); 56} 57 break; 58 case DBTF_MEDIA: // disc 59 break; 60} 61} 62} 63 break; 64} 65 66 return TRUE; 67 68}View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.