在Windows上的Qt中检测USB通知

在Windows上的Qt中检测USB通知

本文介绍了在Windows上的Qt中检测USB通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的qt应用程序中,我想将一些应用程序输出数据保存到USB笔式驱动器中的文件中.我需要在我的qt应用程序中添加以下功能

  1. 检测到USB驱动器插入
  2. 我只有一个USB插槽.
  3. 插入后,我想知道其驱动器号和字母,然后将PC中特定位置的文件传输到该USB驱动器.

有人可以告诉我我用来获取上述所有功能的winapi .lib,.h和.dll文件吗?

如果有人可以提供一些代码片段,对我很有帮助.

解决方案

处理 WM_DEVICECHANGE -请参见 http://lists.trolltech.com/qt-interest/2001-08/thread00698-0.html 了解如何在QT中处理Windows消息./p>

如果wParam是 DBT_DEVICEARRIVAL ,则将lParam转换为 DEV_BROADCAST_HDR *

如果结构 dbch_devicetype DBT_DEVTYP_VOLUME 再次强制转换为lParam,则这次将其转换为 DEV_BROADCAST_VOLUME *

现在检查 dbcv_unitmask 位字段,遍历位0..31并检查相应的驱动器是否与您的USB驱动器匹配.

  if(wParam == DBT_DEVICEARRIVAL){如果((((DEV_BROADCAST_HDR *)lParam)-> dbch_devicetype == DBT_DEVTYP_VOLUME){DWORD掩码=(((DEV_BROADCAST_VOLUME *)lParam)-> dbcv_unitmask;对于(int i = 0; i< 32; ++ i){if(Mask&(1<< i)){char RootPath [4] ="A:\\";RootPath [0] + = i;//检查RootPath中的根路径是否是您的USB驱动器.}}}} 

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 the usb drive insertion
  2. I have only one usb slot.
  3. After i insert it I want to know its drive number and letter and transfer a file at specific location in my PC to that usb drive.

Can anybody tell me which winapi .lib , .h and .dll file i hav to use to get all the above functionalities ?

If someone can provide some code snippets, it will very much helpful for me.

解决方案

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.
      }
    }
  }
}

这篇关于在Windows上的Qt中检测USB通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:22