简体   繁体   English

如何使用Visual C ++从USB大容量存储设备读取文件?

[英]How to read file from USB mass storage Device using Visual C++?

I developed an MFC Dialog application in C++ in MS VS 2013 Ultimate under Windows 7 Maximal. 我在Windows 7 Maximal下的MS VS 2013 Ultimate中用C ++开发了MFC Dialog应用程序。 As a progenitor of source code I use CodeProject paper in Detecting Hardware Insertion or Removal . 作为源代码的原始人,我在“ 检测硬件插入或移除”中使用了CodeProject论文。

My application is a user-mode application. 我的应用程序是一个用户模式应用程序。 It is intended for detection of adding or removing hardware from/to computer. 它旨在检测在计算机中添加硬件或从中删除硬件。 For this purpose I handle WM_DEVICECHANGE message and call RegisterDeviceNotification() function in my application. 为此,我处理WM_DEVICECHANGE消息并在我的应用程序中调用RegisterDeviceNotification()函数。 So, schematically, my aplication does the following: 因此,示意地,我的应用程序执行以下操作:

  1. Calls SetupDiGetClassDevs() to get a handle of device info set HDEVINFO . 调用SetupDiGetClassDevs()以获取设备信息集HDEVINFO
  2. Calls SetupDiEnumDeviceInfo() to enumerate all the device in the info set. 调用SetupDiEnumDeviceInfo()枚举信息集中的所有设备。 Upon each iteration, we will get a SP_DEVINFO_DATA . 每次迭代,我们将获得一个SP_DEVINFO_DATA
  3. During the enumeration, SetupDiGetDeviceInstanceId() is called to read the instance ID for each device. 在枚举期间,将调用SetupDiGetDeviceInstanceId()来读取每个设备的实例ID。 The instance ID is in the form of "USB\\Vid_04e8&Pid_503b\\0002F9A9828E0F06". 实例ID的格式为“ USB \\ Vid_04e8&Pid_503b \\ 0002F9A9828E0F06”。
  4. If the instance ID match the DEV_BROADCAST_DEVICEINTERFACE.dbcc_name , then SetupDiGetDeviceRegistryProperty() is called to retrieve the description or friendly name. 如果实例ID与DEV_BROADCAST_DEVICEINTERFACE.dbcc_name匹配,则SetupDiGetDeviceRegistryProperty()以检索描述或友好名称。

But now I'm in need of reading of file from USB mass storage device when this device is pluged in (when the DBT_DEVICEARRIVAL device event has place). 但是现在,当插入USB大容量存储设备时(当发生DBT_DEVICEARRIVAL设备事件时),我需要从USB大容量存储设备中读取文件。 How can I do it programmatically in Visual C++? 如何在Visual C ++中以编程方式进行操作?

The parameter of DBT_DEVICEARRIVAL is a DEV_BROADCAST_VOLUME which includes a mask of the drive letter assinged. DBT_DEVICEARRIVAL的参数是DEV_BROADCAST_VOLUME,其中包含所分配的驱动器号的掩码。 So since you are only after mass storage devices the following works. 因此,由于您只需要大容量存储设备,因此可以进行以下操作。

DEV_BROADCAST_VOLUME *pj = (DEV_BROADCAST_VOLUME*) lParam;
if (pj->dbcv_devicetype == DBT_DEVTYPE_VOLUME) {
    long um = pj->dbcv_unitmask;
    for ( int i=0; i < 26; i++ ) {
        if (um&1) break;
        um = um >> 1;
    }

    if ( i < 26 ) {
        char Drive  = 'A' + i;
    }
}

In practice, some drives are ready to read instantly, others need few seconds. 实际上,有些驱动器可以立即读取,而另一些则需要几秒钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM