首页 > 代码库 > vc检测USB设备插入或移除
vc检测USB设备插入或移除
vc检测USB设备插入或移除[喝小酒的网摘]http://blog.hehehehehe.cn/a/412.htm
1.添加消息映射
消息映射
BEGIN_MESSAGE_MAP(CONSTNETCNDlg, CDialog)
//{{AFX_MSG_MAP(CONSTNETCNDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
...
ON_WM_DEVICECHANGE()
//}}AFX_MSG_MAP
声明消息响应函数:
afx_msg BOOL OnDeviceChange(UINT nEventType, DWORD dwData);
定义函数内容:
BOOL CONSTNETCNDlg::OnDeviceChange(UINT nEventType,DWORD dwData)
{
DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*) dwData;
//这里进行信息匹配,比如guid等
//针对各个事件进行处理.
switch (nEventType)
{
case DBT_DEVICEREMOVECOMPLETE:
MessageBox("移除USB");
break;
case DBT_DEVICEARRIVAL:
MessageBox("发现USB");
break;
default:
break;
}
return TRUE;
}
2.注册设备(添加到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:只有注册了该设备,OnDeviceChange才能获得详细的信息,否则收到的nEventType参数都是0007,dwData无数据。[喝小酒的网摘]http://blog.hehehehehe.cn/a/412.htm
vc检测USB设备插入或移除