[英]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: 因此,示意地,我的应用程序执行以下操作:
SetupDiGetClassDevs()
to get a handle of device info set HDEVINFO
. SetupDiGetClassDevs()
以获取设备信息集HDEVINFO
。 SetupDiEnumDeviceInfo()
to enumerate all the device in the info set. SetupDiEnumDeviceInfo()
枚举信息集中的所有设备。 Upon each iteration, we will get a SP_DEVINFO_DATA
. SP_DEVINFO_DATA
。 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". DEV_BROADCAST_DEVICEINTERFACE.dbcc_name
, then SetupDiGetDeviceRegistryProperty()
is called to retrieve the description or friendly name. 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.