How to match between physical usb device and its drive letter?
struct tagDrives
{
WCHAR letter;
WCHAR volume[ BUFFER_SIZE ];
} g_drives[ ]; //
WCHAR GetUSBDrive( )
{
LPTSTR lpDevID;
WCHAR cDrive;
DWORD dwSize = BUFFER_SIZE; // Get all removable disks on user laptop.
if ( !GetAllRemovableDisks( ) )
{
WRITELOG( "Error - GetAllRemovableDisks failed\n" );
return ;
} // Alocate memory to device ID
lpDevID = (LPTSTR) AllocMem( BUFFER_SIZE ); // Get device ID corresponding to USBFM from registry.
if ( !GetRegValue( lpDevID, DEVICE_ID, dwSize ) )
{
WRITELOG( "Error - Registry - USBFMDevID failed\n" );
FreeMem( lpDevID );
return ;
} // Get drive corresponding to the registry entry.
cDrive = GetSpecificDrive( lpDevID ); FreeMem( lpDevID ); // return the drive letter.
return cDrive;
} /******************************************************************************
* GetAllRemovableDisks - This function retrieves volume information for all
removable disks
*
* In: None
*
* Out: TRUE - Success
* FALSE - Failure
*
*******************************************************************************/ BOOL GetAllRemovableDisks( )
{
WCHAR caDrive[ ];
WCHAR volume[ BUFFER_SIZE ];
int nLoopIndex;
DWORD dwDriveMask; caDrive[ ] = 'A';
caDrive[ ] = ':';
caDrive[ ] = '\\';
caDrive[ ] = ; g_count = ; // Get all drives in the system.
dwDriveMask = GetLogicalDrives( ); if ( dwDriveMask == )
{
WRITELOG( "Error - GetLogicalDrives failed\n" );
return FALSE;
} // Loop for all drives (MAX_DRIVES = 26) for ( nLoopIndex = ; nLoopIndex < MAX_DRIVES; nLoopIndex++ )
{
// if a drive is present,
if ( dwDriveMask & )
{
caDrive[ ] = 'A' + nLoopIndex; // If a drive is removable
if ( GetDriveType( caDrive ) == DRIVE_REMOVABLE )
{
//Get its volume info and store it in the global variable.
if ( GetVolumeNameForVolumeMountPoint( caDrive, volume, BUFFER_SIZE ) )
{
g_drives[ g_count ].letter = caDrive[ ];
wcscpy( g_drives[ g_count ].volume, volume );
g_count++;
} }
}
dwDriveMask >>= ;
} // success if atleast one removable drive is found.
if ( g_count == )
{
return FALSE;
}
else
{
return TRUE;
}
} /******************************************************************************
* GetSpecificDrive - This function returns the drive corresponding to the
given device ID
*
* In : lpDevID - Device ID
*
* Return: Drive letter corresponding to the given device ID.
*
*******************************************************************************/ WCHAR GetSpecificDrive( LPTSTR lpDevID )
{
HDEVINFO hDevInfo;
GUID guid;
BYTE buffer[ BUFFER_SIZE ];
DWORD dwRequiredSize;
WCHAR buf[ BUFFER_SIZE ];
DEVINST devInstParent;
DWORD dwIndex;
WCHAR volume[ BUFFER_SIZE ];
int nLength, nLoopIndex; SP_DEVICE_INTERFACE_DATA devInterfaceData;
SP_DEVINFO_DATA devInfoData;
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail; if ( !lpDevID )
{
return ;
} // GUID_DEVINTERFACE_VOLUME is interface Guid for Volume class devices.
guid = GUID_DEVINTERFACE_VOLUME; // Get device Information handle for Volume interface
hDevInfo = SetupDiGetClassDevs( &guid, NULL, NULL,
DIGCF_DEVICEINTERFACE | DIGCF_PRESENT ); if ( hDevInfo == INVALID_HANDLE_VALUE )
{
WRITELOG( "Error - SetupDiGetClassDevs failed\n" );
return ;
} // Loop until device interfaces are found.
for ( dwIndex = ;; dwIndex++ )
{
ZeroMemory( &devInterfaceData, sizeof( devInterfaceData ) );
devInterfaceData.cbSize = sizeof( devInterfaceData ); // Get device Interface data. if ( !SetupDiEnumDeviceInterfaces( hDevInfo, NULL, &guid, dwIndex,
&devInterfaceData ) )
{
break;
} ZeroMemory( &devInfoData, sizeof( devInfoData ) );
devInfoData.cbSize = sizeof( devInfoData ); pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) buffer;
pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); // Get device interface detail data to get
// Device Instance from SP_DEVINFO_DATA and
// Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA SetupDiGetDeviceInterfaceDetail( hDevInfo, &devInterfaceData, pDevDetail, // SP_DEVICE_INTERFACE_DETAIL_DATA
BUFFER_SIZE, &dwRequiredSize, &devInfoData ); // SP_DEVINFO_DATA // Get the device instance of parent. This points to USBSTOR.
CM_Get_Parent( &devInstParent, devInfoData.DevInst, ); // Get the device instance of grand parent. This points to USB root.
CM_Get_Parent( &devInstParent, devInstParent, ); // Get the device ID of the USB root.
CM_Get_Device_ID( devInstParent, buf, BUFFER_SIZE, ); // If USB root device matches with the input device ID, it is the target device. if ( buf != NULL && wcscmp( lpDevID, buf ) == )
{
// Append \ to the DevicePath of SP_DEVICE_INTERFACE_DETAIL_DATA nLength = wcslen( pDevDetail->DevicePath );
pDevDetail->DevicePath[ nLength ] = '\\';
pDevDetail->DevicePath[ nLength + ] = ; // Get Volume mount point for the device path.
if ( GetVolumeNameForVolumeMountPoint( pDevDetail->DevicePath, volume,
BUFFER_SIZE ) )
{
for ( nLoopIndex = ; nLoopIndex < g_count; nLoopIndex++ )
{
// Compare volume mount point with the one stored earlier.
// If both match, return the corresponding drive letter. if ( wcscmp( g_drives[ nLoopIndex ].volume, volume ) == )
{
SetupDiDestroyDeviceInfoList( hDevInfo );
return g_drives[ nLoopIndex ].letter;
}
}
}
}
} SetupDiDestroyDeviceInfoList( hDevInfo );
WRITELOG( "Error - No drives found in GetSpecificDrives\n" );
return ;
}
http://www.lvr.com/files/find_drive_letter.txt
(From Marc Reinig) Look at the code that Devcon.exe (from the DDK) uses in
Devcon.exe find =diskdrive
This will give you the instance ID of the disks present on the system, in
order of their disk number. 0, 1, 2, 3, … . So now you know what the disk
number is for your device.
Then, using the code below, loop through all possible 26 logical drives,
using strings that look like “C:” for szDriveLetter. When you get an
sdnDevNumber.DeviceNumber that matches the disk number you are looking
for, you have found your drive letter, szDriveLetter:
char szDriveLetter[ ];
char * pszDrive;
STORAGE_DEVICE_NUMBER sdnDevNumber;
BOOL fResult; fResult = GetBasicDiskDriveMapping( szDriveLetter, &sdnDevNumber ); // sdnDevNumber.DeviceNumber will contain the drive number you want
// sdnDevNumber.PartitionNumber will contain the partition for your reference BOOL GetBasicDiskDriveMapping( LPCSTR pszDrive, STORAGE_DEVICE_NUMBER *psdn ) { BOOL fResult;
char szDriveName[ ];
HANDLE hDrive;
DWORD dwBytesReturned; __try
{
lstrcpy( szDriveName, "\\\\.\\" ); lstrcat( szDriveName, pszDrive ); // Open the volume to which the drive letter refers and get
// the physical drive on which the volume resides. hDrive = CreateFile( szDriveName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, , OPEN_EXISTING, , ); if ( INVALID_HANDLE_VALUE != hDrive ) fResult = DeviceIoControl( hDrive, IOCTL_STORAGE_GET_DEVICE_NUMBER, , , psdn, sizeof(STORAGE_DEVICE_NUMBER), &dwBytesReturned, ); else fResult = false;
} __except( EXCEPTION_EXECUTE_HANDLER ) {
// if we get here, one of the arguments probably
// points to not enough memory. SetLastError( ERROR_INVALID_PARAMETER ); fResult = false;
} CloseHandle( hDrive ); return ( fResult );
}
How To get the usbdisk's drive letter properly
http://www.codeproject.com/Articles/6559/How-To-get-the-usbdisk-s-drive-letter-properly
Introduction
We know USB disk should be a removable disk just like floppy disk, and be used more and more widely now.
Because, the USB disk is more faster, reliable, and affordable than old floppy disk.
So, when we want to check one disk or drive of target system is removable or not,
we may be thinking of using API function "GetDriveType()
".
Yes, it really works on some USB device, such as 16MB, 32MB, 64MB, and 128MB. ;-)
Here, how aboutremovable hard disk which is connected to system by USB channel? -
Windows will report them as 'Fix Disk', and we would get the same result using 'GetDriveType()
' function.
How can we differentiate between these USB ‘Fix Disk’ and Those IDE ‘Fix Disk’?
Here is the solution for this event.
Background
(Why do I want get the USB disks' drive letter properly?
Because I want to check the virus while one new USB drive is inserted.
We should not be remiss of the virus which is more and more technical day by day:)
Since we can get the base information about the disk type
(using API Function ‘GetDriveType()
’),
we may only want to check the ‘Removable Hard Disk’ to verify its bus-type.
Well, we’ll have two steps to get the USB disk’s drive letters:
Code Thoughts
switch ( GetDriveType( szDrvName ) )
{
case : // The drive type cannot be determined.
case :// The root directory does not exist.
drivetype = DRVUNKNOWN;
break;
case DRIVE_REMOVABLE:// The drive can be removed from the drive.
drivetype = DRVREMOVE;
break;
case DRIVE_CDROM:// The drive is a CD-ROM drive.
break;
case DRIVE_FIXED:// The disk cannot be removed from the drive.
drivetype = DRVFIXED;
break;
case DRIVE_REMOTE:// The drive is a remote (network) drive.
drivetype = DRVREMOTE;
break;
case DRIVE_RAMDISK:// The drive is a RAM disk.
drivetype = DRVRAM;
break;
}
These codes above are based on ‘Article ID: Q161300 HOWTO:
Determine the Type of Drive Using Win32’ from MSDN.
2. Determinate the bus type of the ‘Fix Disk’:
Now, we may embed our codes at the ‘case = DRIVE_FIXED
’:
Open the drive which we get now:
hDevice = CreateFile(szBuf,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, NULL, NULL);
If we opened this drive, check its BUSTYPE
, using API GetDisksProperty()
:
if(GetDisksProperty(hDevice, pDevDesc))
{
if(pDevDesc->BusType == BusTypeUsb) // This is the ‘Check Point’!!! ;-)
{
// We store the drive letter here
szMoveDiskName[k] = chFirstDriveFromMask(temp);
szMoveDiskName[]=k;
k++;
}
}
Close this drive when we finished our work on it:
CloseHandle(hDevice);
3. How does the GetDisksProperty() work?
/********************************************************
*
* FUNCTION: GetDisksProperty(HANDLE hDevice,
* PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
*
* PURPOSE: get the info of specified device
*
******************************************************/
BOOL GetDisksProperty(HANDLE hDevice,
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
STORAGE_PROPERTY_QUERY Query; // input param for query
DWORD dwOutBytes; // IOCTL output length
BOOL bResult; // IOCTL return val // specify the query type
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery; // Query using IOCTL_STORAGE_QUERY_PROPERTY
bResult = ::DeviceIoControl(hDevice, // device handle
IOCTL_STORAGE_QUERY_PROPERTY, // info of device property
&Query, sizeof(STORAGE_PROPERTY_QUERY), // input data buffer
pDevDesc, pDevDesc->Size, // output data buffer
&dwOutBytes, // out's length
(LPOVERLAPPED)NULL); return bResult;
}
Comments
- There are some structures not commented, see usbdisks_src for them.;
- Floppy drive (A: or B:) is reported as USB Disks by this demo, -And-
it is easy to correct this, just putting some codes to the ‘case = DRIVE_REMOVABLE:
‘;
History
- 2004-03-29 - 1st GO
License
This article, along with any associated source code and files,
is licensed underThe Code Project Open License (CPOL)
How to match between physical usb device and its drive letter?的更多相关文章
- What is a Windows USB device path and how is it formatted?
http://community.silabs.com/t5/Interface-Knowledge-Base/Windows-USB-Device-Path/ta-p/114059 Windows ...
- Install Slax on USB device (Slax U 盘安装)
Slax is a modern, portable, small and fast Linux operating system with a modular approach and outsta ...
- USB Device Finder
http://www.velleman.eu/images/tmp/usbfind.c #ifdef __cplusplus extern "C" { #endif #includ ...
- Assigning Host USB device to a Guest VM
Example Assigning Host USB device to a Guest VM This example is based on qemu-kvm (0.15.0) as instal ...
- usb host和usb device
S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb devic ...
- "The connection for the USB device '###' was unsuccessful. The device is currently in use"
一.问题描述 1.情景描述 笔者的物理主机系统是“windows7 64位”,想使用“摄像头录像大师”.这个软件在录制视频的过程中,需要调用windows自带的"windows media ...
- USB device & USB controller & USB passthrough
目录 USB device USB controller Linux 相关命令 Python 相关库 Libvirt USB passthrough 参考资料 近期往 openstack 里倒腾 US ...
- usb device address error 110
ubuntu失灵了,怎么都起不来,报一堆错误usb device descriptor read/64, error 110......重启,换kvm的接口,usb键盘鼠标...终于在试了下面这个方法 ...
- STM32F4 HAL Composite USB Device Example : CDC + MSC
STM32F4 USB Composite CDC + MSC I'm in the process of building a USB composite CDC + MSC device on t ...
随机推荐
- mac下---charles抓包https
网上找的很多安装包都有问题,终于找到个可用的! 下载地址: http://pan.baidu.com/s/1pLAONbX ———————————————————————————— 教程转载:htt ...
- Oracle 函数 “申请通过后,将该表中循环遍历到的所有内容插到另一个表中”
create or replace function mcode_apply_insert_material(p_mca_no VARCHAR2, p_action VARCHAR2, p_wf_no ...
- 应用服务器中对JDK的epoll空转bug的处理
原文链接:应用服务器中对JDK的epoll空转bug的处理 前面讲到了epoll的一些机制,与select和poll等传统古老的IO多路复用机制的一些区别,这些区别实质可以总结为一句话, 就是epol ...
- CentOS7的firewall和安装iptables
前言:CentOS7 的防火墙默认使用是firewall,而我们通常使用iptables: 本文记录了firewall基础的命令和iptables的安装和使用. firewall部分: part1 : ...
- python使用cookie登陆网页
python2: Python 爬虫入门六之 Cookie 的使用 python3: Python3 网络爬虫 (六):Python3 使用 Cookie - 模拟登陆获取妹子联系方式 python ...
- 程序设计实习MOOC / 程序设计与算法(三)第一周测验
作业题: 7. 填空(2分)简单的swap 通过码是 ( 请参考公告中的“关于编程作业的说明”完成编程作业(请注意,编程题都要求提交通过码,在openjudge上提交了程序并且通过以后,就可以下载到通 ...
- 在ASP.NET Web API和ASP.NET Web MVC中使用Ninject
先附上源码下载地址 一.准备工作 1.新建一个名为MvcDemo的空解决方案 2.新建一个名为MvcDemo.WebUI的空MVC应用程序 3.使用NuGet安装Ninject库 二.在ASP.N ...
- 41-2:和为S的连续正数序列
import java.util.ArrayList; /** * 面试题41-题目2:和为S的连续正数序列 * 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案 ...
- Linux 服务器上Redis安装和配置
1.下载安装redis 在Linux服务器上,命令行执行以下命令(cd ./usr local/src 一般源码放在这里(推荐源码安装)) wget http://download.redis.io/ ...
- iuap
2017.12 用友今年着力点往云平台发展,是时候整理一下思路 第一:iuap 第二:Linux 第三:财务会计业务入门 第四:NC节点视频教程--财务模块 2019年3月4日 all in iuap ...