I am trying to detect USB arrival and removal when a USB device is plugged in or unplugged. At this point, I am working with a simple USB flash drive to detect the events and respond to it. The problem is I do get WM_DEVICECHANGE message when USB is plugged in but never its sub message DBT_DEVICEARRIVAL. My code is below, what am I possibly doing wrong?

Qt Code:
  1. /*******************************************
  2. * WINDOWS EVENTS
  3. ********************************************/
  4. /*We use the first WM_PAINT event to get the handle of main window
  5.   and pass it to RegisterDeviceNotification function.
  6.   It not possible to do this in the contructor because the
  7.   main window does not exist yet.
  8.   WM_DEVICECHANGE event notify us that a device is attached or detached */
  9. bool USBexample::nativeEvent(const QByteArray & eventType, void * message, long * result)
  10. {
  11. MSG * msg = static_cast< MSG * > (message);
  12. int msgType = msg->message;
  13. if(msgType == WM_PAINT)
  14. {
  15. if(!msgp) //Only the first WM_PAINT
  16. {
  17. GUID InterfaceClassGuid = HID_CLASSGUID;
  18. DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
  19. ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
  20. NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  21. NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  22. NotificationFilter.dbcc_classguid = InterfaceClassGuid;
  23. HWND hw = (HWND) this->effectiveWinId(); //Main window handle
  24. hDevNotify = RegisterDeviceNotification(hw,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
  25. msgp = true;
  26. }
  27. }
  28. if(msgType == WM_DEVICECHANGE)
  29. {
  30. qDebug() << "WM_DEVICECHANGE recieved";
  31. PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
  32. switch(msg->wParam)
  33. {
  34. case DBT_DEVICEARRIVAL: // never comes here!
  35. if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
  36. {
  37.  
  38. qDebug() << "DBT_DEVICEARRIVAL case";
  39.  
  40. PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
  41. int i = 0;
  42. //to find a better way for this...
  43. while(lpdbv->dbcc_name[i] != 0)
  44. {
  45. s.append(lpdbv->dbcc_name[i]);
  46. i++;
  47. }
  48. s = s.toUpper();
  49. if(s.contains(MY_DEVICE_VIDPID))
  50. emit USB_Arrived();
  51. }
  52. break;
  53. case DBT_DEVICEREMOVECOMPLETE:
  54. if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
  55. {
  56. qDebug() << "DBT_DEVICEREMOVECOMPLETE case";
  57.  
  58. PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
  59. int i = 0;
  60. //to find a better way for this...
  61. while(lpdbv->dbcc_name[i] != 0)
  62. {
  63. s.append(lpdbv->dbcc_name[i]);
  64. i++;
  65. }
  66. s = s.toUpper();
  67. if(s.contains(MY_DEVICE_VIDPID))
  68. emit USB_Removed();
  69. }
  70. break;
  71. case DBT_DEVICEREMOVEPENDING :
  72. {
  73. qDebug() << "DBT_DEVICEREMOVEPENDING case";
  74. }
  75. break;
  76. default:
  77. {
  78. qDebug() << "Went to Default case";
  79. }
  80.  
  81.  
  82. }
  83. }
  84. return false;
  85. }
To copy to clipboard, switch view to plain text mode