Detect USB insertion and removal events

Source: Internet
Author: User

Question: sumoyu () reputation: Level 100: user1 Date: 10:12:51

USB insertion or removal is monitored to obtain the drive letter. The event wm_devicechange can monitor insertion and removal, how to determine whether to insert or remove the drive letter, and how to obtain the drive letter.

Reply:

Respondent: ffsb (for petty assets) Credit: 100 level: user2 Date: 11:07:49 ID: 37208771

Form1::OnCreate    //================Dev Notfi============================    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );    NotificationFilter.dbcc_size =        sizeof(DEV_BROADCAST_DEVICEINTERFACE);    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;    NotificationFilter.dbcc_classguid = g_UsbioID;    DevNotify = RegisterDeviceNotification( Handle,        &NotificationFilter,        DEVICE_NOTIFY_WINDOW_HANDLE    );==========================//---------------------------------------------------------------------------void __fastcall TDCForm::WMDeviceChange(TMessage & Msg){    if(Msg.WParam == DBT_DEVICEARRIVAL)    {        PDEV_BROADCAST_HDR hDev = (PDEV_BROADCAST_HDR)Msg.LParam;        if(hDev->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)        {// Do nothing!        }    }    else if(Msg.WParam == DBT_DEVICEREMOVECOMPLETE)    {        PDEV_BROADCAST_HDR hDev = (PDEV_BROADCAST_HDR)Msg.LParam;        if(hDev->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)        {if(DisconnBtn->Enabled)        {DisconnBtn->Click();        }        }    }}

Respondent: ffsb (for petty assets) Credit: 100 level: user2 Date: 11:09:09 ID: 37208826

This is a bit messy.

Respondent: sumoyu () reputation: 100 level: user1 Date: 11:12:45 ID: 37208961

Thank you! Try me

Respondent: sumoyu () reputation: 100 level: user1 Date: 11:21:04 ID: 37209265

What is g_usbioid?

Respondent: ffsb (for petty assets) Credit: 100 level: user2 Date: 11:51:19 ID: 37210214

USB hardware ID,

Respondent: sumoyu () reputation: 100 level: user1 Date: 12:42:52 ID: 37211248

How to obtain the hardware ID? During debugging, I tracked and found that MSG. wparam = dbt_devnodes_changed

Respondent: ffsb (for petty assets) Credit: 100 level: user2 Date: 14:00:41 ID: 37213378

Device_policy_window_handle ==> device_policy_window_handle | device_policy_all_interface_classes to track all USB devices.

Respondent: cczlp (not confused) reputation: 117 level: star2 Date: 14:01:41 ID: 37213414

When msg. wparam = dbt_devnodes_changed, take the number of specified devices, increase is insert, decrease is remove.

Respondent: sumoyu () reputation: 100 level: user1 Date: 15:05:05 ID: 37215482

After USB is removed, an event msg. wparam = dbt_deviceremovecomplete is generated, and an event msg. wparam = dbt_devnodes_changed is generated. After the USB is removed, the code in both events is executed (as if msg. wparam = dbt_devnodes_changed was executed first .) Also, you can use a function to obtain all USB guids.

Respondent: ffsb (for petty assets) Credit: 100 level: user2 Date: 15:31:02 ID: 37216498

Setupdixxxxx: I forgot the title. Check it.

 

Well, the above are all nonsense. Let's look at the masterworks:

Http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal

Http://www.codeproject.com/Articles/119168/Hardware-Change-Detection

Generally, the imported registerdevicenotification-> GUID is not as elegant as you think. Read this blog:

Http://blogs.msdn.com/ B /doronh/archive/2006/02/15/532679.aspx

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.