当前位置:   article > 正文

基于C++的设备自动检测添加_ondevicechange 多设备同时插入

ondevicechange 多设备同时插入

#include <Dbt.h>

重写 OnDeviceChange

  1. BOOL CX3Dlg::OnDeviceChange(UINT nEventType, DWORD dwData)
  2. {
  3. char* s;
  4. DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*)dwData;
  5. switch (nEventType) {
  6. case DBT_DEVICEREMOVECOMPLETE:
  7. if (s = strstr((char*)dbd->dbcc_name, "USB#VID_0483&PID_5740")) {
  8. X3LOG.Logger_Debug(__FILE__, __LINE__, "USB已移除!\n");
  9. }else if (s = strstr((char*)dbd->dbcc_name, "BTHENUM#")) {
  10. if ((s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B10"))
  11. || (s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B30"))) {
  12. X3LOG.Logger_Debug(__FILE__, __LINE__, "蓝牙已解绑!\n");
  13. }
  14. }
  15. break;
  16. case DBT_DEVICEARRIVAL:
  17. if (s = strstr((char*)dbd->dbcc_name, "USB#VID_0483&PID_5740")) {
  18. X3LOG.Logger_Debug(__FILE__, __LINE__, "USB已插入!\n");
  19. }
  20. else if (s = strstr((char*)dbd->dbcc_name, "BTHENUM#")) {
  21. if ((s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B10")) || (s = strstr((char*)dbd->dbcc_name, "BluetoothDevice_001B30"))) {
  22. X3LOG.Logger_Debug(__FILE__, __LINE__, "蓝牙已配对!\n");
  23. }
  24. }
  25. break;
  26. default:
  27. break;
  28. }
  29. return TRUE;
  30. }

 添加事件的监听:

  1. BEGIN_MESSAGE_MAP(CX3Dlg, CDHtmlDialog)
  2. ON_WM_DEVICECHANGE()
  3. END_MESSAGE_MAP()

 最重要的一步,在加载窗体的时候注册设备通知事件

  1. void CX3Dlg::InitDBT(void * prama) {
  2. HDEVNOTIFY m_hDeviceNotify;
  3. CX3Dlg* dlg = (CX3Dlg*)prama;
  4. static const GUID GUID_DEVINTERFACE_LIST[] =
  5. { 0x4d36e978, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
  6. DEV_BROADCAST_DEVICEINTERFACE Filter;
  7. ZeroMemory(&Filter, sizeof(Filter));
  8. Filter.dbcc_size = sizeof(Filter);
  9. Filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  10. for (int i = 0; i < sizeof(GUID_DEVINTERFACE_LIST) / sizeof(GUID); i++) {
  11. Filter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
  12. m_hDeviceNotify = RegisterDeviceNotification(dlg->m_hWnd, &Filter, DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  13. if (NULL == m_hDeviceNotify) {
  14. break;
  15. }
  16. }
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/484005
推荐阅读
相关标签
  

闽ICP备14008679号