VC detection USB Device insertion or removal

Source: Internet
Author: User
Tags define function

VC detection USB device insertion or removal [drink the net of wine to pick]http://blog.hehehehehe.cn/a/412.htm

1. Add a message map

Message map
Begin_message_map (Constnetcndlg, CDialog)
{{Afx_msg_map (CONSTNETCNDLG)
On_wm_paint ()
On_wm_querydragicon ()
...
On_wm_devicechange ()
}}afx_msg_map

To declare a message response function:
afx_msg BOOL Ondevicechange (UINT neventtype, DWORD dwdata);

Define function Contents:


BOOL Constnetcndlg::ondevicechange (UINT neventtype,dword dwdata)
{
dev_broadcast_deviceinterface* dbd = (dev_broadcast_deviceinterface*) dwdata;

Information matching is done here, such as GUIDs, etc.

Processed for each event.
Switch (neventtype)
{
Case Dbt_deviceremovecomplete:
MessageBox ("Remove USB");
Break
Case Dbt_devicearrival:
MessageBox ("Discovery USB");
Break
Default
Break
}

return TRUE;

}

2. Register the device (add to OnInitDialog)

Dev_broadcast_deviceinterface Devint;
memset (&devint,0,sizeof (dev_broadcast_deviceinterface));
devint.dbcc_size = sizeof (dev_broadcast_deviceinterface);
Devint.dbcc_devicetype = Dbt_devtyp_deviceinterface;

Devint.dbcc_classguid = {0XA5DCBF10, 0x6530, 0x11d2, {0x90, 0x1F, 0x00, 0xC0, 0x4f, 0xb9, 0x51, 0xED}};
Registerdevicenotificationthis->getsafehwnd (), &devint,device_notify_window_handle);

PS: Only if the device is registered, Ondevicechange can get detailed information, otherwise the Neventtype parameter received is 0007,dwdata no data. []http://blog.hehehehehe.cn/a/412.htm on the net of drinking small wine

VC detection USB Device insertion or removal

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.