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