How To get the usbdisk's drive letter properly
来源:互联网 发布:c语言编译器 编辑:程序博客网 时间:2024/05/18 17:04
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, 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
1. Get the disks base information:
switch ( GetDriveType( szDrvName ) ) { case 0: // The drive type cannot be determined. case 1: // 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 Win
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[0]=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
- How To get the usbdisk's drive letter properly
- C# How to get the system drive of remote server
- [JNI] How to get the java's native method body?
- How to Get the WDK
- How do my .NET-based clients get the app's HINSTANCE to pass to my function?
- How to get to the point quickly?
- How to close resources properly
- How to properly unblock libusb_bulk_transfer
- How to get new record's id
- How to Get Table’s Generated Ids
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- How to get rid of the white margin in MATLAB's saveas or print outputs
- How to get parameters from the URL?
- How to get the current epoch time
- How to get the useful synonyms
- How to get the Information leakage
- [PHP]How to get the system info?
- How to get the digest hash
- 地球上最快的矢量图形软件 Xrar LX
- Craiglist 面临来自微软的挑战
- 视频 - 地下原子弹爆炸
- 比尔-盖茨:未来普通纸笔将消失
- 如何正确选择ARM控制器和操作系统
- How To get the usbdisk's drive letter properly
- 就像爱情是什么一样,关于面向对象的话题永远也谈不完。爱情将来时的我现在对象进行时
- 美国名牌大学计算机网络课程网址
- Eclipse+Mysql+tomcat环境终于配好
- delphi 中设定 合计行信息的一段代码
- GPON应用于3G基站传输的分析报告
- Tips_C++
- [asp.net]在15分钟之内建立数据访问层-DAL
- 信息时代如何成为一名优秀的程序员