赞
踩
#include <Dbt.h>
重写 OnDeviceChange
- BOOL CX3Dlg::OnDeviceChange(UINT nEventType, DWORD dwData)
- {
- char* s;
- DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*)dwData;
- switch (nEventType) {
- case DBT_DEVICEREMOVECOMPLETE:
- if (s = strstr((char*)dbd->dbcc_name, "USB#VID_0483&PID_5740")) {
- X3LOG.Logger_Debug(__FILE__, __LINE__, "USB已移除!\n");
- }else if (s = strstr((char*)dbd->dbcc_name, "BTHENUM#")) {
- if ((s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B10"))
- || (s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B30"))) {
- X3LOG.Logger_Debug(__FILE__, __LINE__, "蓝牙已解绑!\n");
- }
- }
- break;
- case DBT_DEVICEARRIVAL:
- if (s = strstr((char*)dbd->dbcc_name, "USB#VID_0483&PID_5740")) {
- X3LOG.Logger_Debug(__FILE__, __LINE__, "USB已插入!\n");
- }
- else if (s = strstr((char*)dbd->dbcc_name, "BTHENUM#")) {
- if ((s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B10")) || (s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B30"))) {
- X3LOG.Logger_Debug(__FILE__, __LINE__, "蓝牙已配对!\n");
- }
- }
- break;
- default:
- break;
- }
- return TRUE;
- }
添加事件的监听:
- BEGIN_MESSAGE_MAP(CX3Dlg, CDHtmlDialog)
- ON_WM_DEVICECHANGE()
- END_MESSAGE_MAP()
最重要的一步,在加载窗体的时候注册设备通知事件
- void CX3Dlg::InitDBT(void * prama) {
- HDEVNOTIFY m_hDeviceNotify;
- CX3Dlg* dlg = (CX3Dlg*)prama;
- static const GUID GUID_DEVINTERFACE_LIST[] =
- { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
- DEV_BROADCAST_DEVICEINTERFACE Filter;
- ZeroMemory(&Filter, sizeof(Filter));
- Filter.dbcc_size = sizeof(Filter);
- Filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
- for (int i = 0; i < sizeof(GUID_DEVINTERFACE_LIST) / sizeof(GUID); i++) {
- Filter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
- m_hDeviceNotify = RegisterDeviceNotification(dlg->m_hWnd, &Filter, DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
- if (NULL == m_hDeviceNotify) {
- break;
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。