5,138,005 members and growing! (13,435 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Win32/64 SDK & OS » General     Intermediate License: The Code Project Open License (CPOL)

How To get the usbdisk's drive letter properly

By f22_storm

Using DeviceIoControl to get UsbDisk Drive letter(s)
VC6, VC7, VC7.1, C++Windows, Win2K, WinXP, Win2003, MFC, VS6, VS, Dev

Posted: 28 Mar 2004
Updated: 28 Mar 2004
Views: 108,357
Announcements



Search    
Advanced Search
Sitemap
23 votes for this Article.
Popularity: 5.50 Rating: 4.04 out of 5
1 vote, 4.3%
1
2 votes, 8.7%
2
2 votes, 8.7%
3
6 votes, 26.1%
4
12 votes, 52.2%
5

Sample image

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 about removable 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

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 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[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

  1. There are some structures not commented, see usbdisks_src for them.;
  2. 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 under The Code Project Open License (CPOL)

About the Author

f22_storm


software development is so nice and we can make our world better than ever, even the real world.
vc++6 is enough for me, althought i tried to upgrade to higher version, each time, i down-grade to vc6. ^_^
Occupation: Software Developer
Company: Sysoft Forum
Location: China China

Other popular Win32/64 SDK & OS articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralPen Drive letter detection.. [modified]memberamshoyeb19:38 16 Mar '08  
GeneralSD card CID registermembermitya20053:27 15 Nov '07  
GeneralHow to get the vendor IDs & product IDs of all USB devicesmember2:42 20 Feb '07  
GeneralError code 87memberVolperossa6:01 20 Oct '06  
GeneralRe: Error code 87memberMarcel_NL5:50 28 Mar '07  
GeneralRe: Error code 87memberwardk3:26 29 Mar '07  
GeneralAdministrative privileges requiredsupporterFrancesco Foti4:07 20 Jun '06  
GeneralNeat! ... BUTmemberSexyLilDarkPaw3:05 28 May '06  
GeneralThe link :memberRyan.Monti21:59 22 Mar '06  
GeneralI think this link can help youmemberRyan.Monti21:57 22 Mar '06  
Generalthanksmemberarmen-ia9:50 8 Mar '06  
GeneralHow to detect card in USB multi-card reader?membertserg13:13 11 Jan '06  
GeneralRe: How to detect card in USB multi-card reader?memberJVDani21:36 18 Nov '07  
Questionhow to eject a USB-penmemberchergui2:55 5 Jan '06  
AnswerRe: how to eject a USB-penmemberf22_storm15:08 5 Jan '06  
AnswerRe: how to eject a USB-penmemberf22_storm15:14 5 Jan '06  
AnswerRe: how to eject a USB-penmemberchergui0:22 6 Jan '06  
GeneralUSB communication PC to Palm to PCmemberAfer Ventus4:21 1 Dec '05  
GeneralHow to get & send data to USBmembervikas amin4:24 4 Nov '05  
GeneralUSB formattingmembervranam3:52 10 Jun '05  
GeneralRe: USB formattingmemberf22_storm15:37 12 Jun '05  
Generalthis runs only with DDK installed, doesn't it?sussAnonymous0:18 5 Apr '05  
GeneralRe: this runs only with DDK installed, doesn't it?memberf22_storm15:40 12 Jun '05  
GeneralPlease help me How to get the Drive letter on win 98 and MEmemberVed16:38 26 Sep '04  
GeneralRe: Please help me How to get the Drive letter on win 98 and MEmemberjiaojilin19:50 13 Jun '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Mar 2004
Editor: Nishant Sivakumar
Copyright 2004 by f22_storm
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project