1. Add message ing
Message ing
Begin_message_map (constnetcndlg, cdialog)
// {Afx_msg_map (constnetcndlg)
On_wm_paint ()
On_wm_querydragicon ()
...
On_wm_devicechange ()
//} Afx_msg_map
Declare the message response function:
Afx_msg bool ondevicechange (uint neventtype, DWORD dwdata );
Define function content:
Bool constnetcndlg: ondevicechange (uint neventtype, DWORD dwdata)
{
Dev_broadcast_deviceinterface * DBD = (dev_broadcast_deviceinterface *) dwdata;
// Information matching, such as guid
// Process events.
Switch (neventtype)
{
Case dbt_deviceremovecomplete:
MessageBox ("Remove USB ");
Break;
Case dbt_devicearrival:
MessageBox ("USB found ");
Break;
Default:
Break;
}
Return true;
}
2. register the device (add it 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_policy_window_handle );
PS: only when the device is registered can ondevicechange obtain detailed information. Otherwise, all the received neventtype parameters are 0007, and dwdata has no data. [Drinking wine] http://blog.const.net.cn/a/412.htm