[MFC] USB flash disk Detection

Source: Internet
Author: User

Preface

This article mainly describes how to use message queue in MFC and capture the USB flash drive insertion and uninstallation events in the message.

 

Body

1. Use message queue in MFC

1.1 statement

Virtual LRESULT WindowProc (UINT message, WPARAM wParam, LPARAM lParam );

1.2 Implementation

LRESULT CTestDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
{
Return CDialog: WindowProc (message, wParam, lParam );
}

This example inherits CDialog.

 

Ii. Capture USB flash drive insertion and uninstallation events

LRESULT CTestDlg: WindowProc (UINT message, WPARAM wParam, LPARAM lParam)
{
Switch (message)
{
// WM_DEVICECHANGE: system message sent from system hardware change
Case WM_DEVICECHANGE:
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lParam;
Switch (wParam)
{
Case WM_DEVICECHANGE:
Break;
Case DBT_DEVICEARRIVAL: // DBT_DEVICEARRIVAL. The device detection is complete and can be used.
{
If (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lpdb;
Switch (lpdbv-> dbcv_flags)
{
Case 0: // U Disk
{
CString decDriver;
DecDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask );
TRACE1 ("detected USB flash drive: [% s] inserted! \ N ", decDriver. GetBuffer (0 ));
}
Break;
Case DBTF_MEDIA: // CD
TRACE1 ("detected disc: [% c] inserted! \ N ", FirstDriveFromMask (lpdbv-> dbcv_unitmask ));
Break;
}
}
}
Break;
Case DBT_DEVICEREMOVECOMPLETE: // DBT_DEVICEREMOVECOMPLETE. Uninstall or unplug the device.
{
If (lpdb-> dbch_devicetype = DBT_DEVTYP_VOLUME) // logical volume
{
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lpdb;
Switch (lpdbv-> dbcv_flags)
{
Case 0: // U Disk
{
CString decDriver;
DecDriver = FirstDriveFromMask (lpdbv-> dbcv_unitmask );
TRACE1 ("detected USB flash drive: [% s] unplugging! \ N ", decDriver. GetBuffer (0 ));
}
Break;
Case DBTF_MEDIA: // CD

Break;
}
}
}
Break;
}
}
Break;
}
Return CDialog: WindowProc (message, wParam, lParam );
}

III,

 

IV. Article Modification

4.1 do not use SetWindowLong to retrieve message queues in MFC, otherwise the window cannot be closed.

4.2 thank you for reminding us that after CString getBuffer, you should call ReleaseBuffer

 

 

Conclusion

Continue anti-spoofing basics...

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.