Detecting USB notification in Qt on windows

后端 未结 2 1682
天涯浪人
天涯浪人 2021-01-19 17:53

In my qt application I want to save some application output data to an file in my usb pen drive. I need to put following features in my qt application

  1. Detect t
2条回答
  •  天涯浪人
    2021-01-19 18:08

    Handle WM_DEVICECHANGE - See http://lists.trolltech.com/qt-interest/2001-08/thread00698-0.html for how to handle windows messages in QT.

    If wParam is DBT_DEVICEARRIVAL then cast lParam to a DEV_BROADCAST_HDR *

    If the structures dbch_devicetype is DBT_DEVTYP_VOLUME cast lParam again, this time to a DEV_BROADCAST_VOLUME *

    Now check the dbcv_unitmask bit field, iterate over bits 0..31 and check if the corresponding drive match your USB drive.

    if (wParam == DBT_DEVICEARRIVAL) {
      if (((DEV_BROADCAST_HDR *) lParam)->dbch_devicetype == DBT_DEVTYP_VOLUME) {
        DWORD Mask = ((DEV_BROADCAST_VOLUME *) lParam)->dbcv_unitmask;
        for (int i = 0; i < 32; ++i) {
          if (Mask & (1 << i)) {
            char RootPath[4] = "A:\\";
            RootPath[0] += i;
            // Check if the root path in RootPath is your USB drive.
          }
        }
      }
    }
    

提交回复
热议问题