Click here to Skip to main content
14,330,208 members

How To get the usbdisk's drive letter properly

Rate this:
4.45 (40 votes)
Please Sign up or sign in to vote.
4.45 (40 votes)
28 Mar 2004CPOL
Using DeviceIoControl to get UsbDisk Drive letter(s)

Sample image


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, <st1:place w:st="on"><st1:city w:st="on">32<st1:state w:st="on">MB, <st1:postalcode w:st="on">64<st1:state w:st="on">MB, 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.


(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;
 case DRIVE_REMOVABLE: // The drive can be removed from the drive.
 drivetype = DRVREMOVE;
 case DRIVE_CDROM: // The drive is a CD-ROM drive.
 case DRIVE_FIXED: // The disk cannot be removed from the drive.
 drivetype = DRVFIXED;
 case DRIVE_REMOTE: // The drive is a remote (network) drive.
 drivetype = DRVREMOTE;
 case DRIVE_RAMDISK: // The drive is a RAM disk.
 drivetype = DRVRAM;

These codes above are based on ‘Article ID: Q161300 HOWTO: Determine the Type of Drive Using Win<st1:chmetcnv w:st="on" unitname="’" sourcevalue="32" hasspace="False" negative="False" numbertype="1" tcsc="0">32’ 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, 

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); 

Close this drive when we finished our work on it:


3. How does the GetDisksProperty() work?

* FUNCTION: GetDisksProperty(HANDLE hDevice, 
* PURPOSE: get the info of specified device
BOOL GetDisksProperty(HANDLE hDevice, 
 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;

 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

 return bResult;


  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:‘;


  • 2004-03-29 - 1st GO


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer emware
China China
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. ^_^

Comments and Discussions

AnswerRe: How to detect card in USB multi-card reader? Pin
JVDani18-Nov-07 20:36
memberJVDani18-Nov-07 20:36 
Questionhow to eject a USB-pen Pin
chergui5-Jan-06 1:55
memberchergui5-Jan-06 1:55 
AnswerRe: how to eject a USB-pen Pin
dotnfc5-Jan-06 14:08
memberdotnfc5-Jan-06 14:08 
AnswerRe: how to eject a USB-pen Pin
dotnfc5-Jan-06 14:14
memberdotnfc5-Jan-06 14:14 
AnswerRe: how to eject a USB-pen Pin
chergui5-Jan-06 23:22
memberchergui5-Jan-06 23:22 
GeneralUSB communication PC to Palm to PC Pin
Afer Ventus1-Dec-05 3:21
memberAfer Ventus1-Dec-05 3:21 
QuestionHow to get &amp; send data to USB Pin
vikas amin4-Nov-05 3:24
membervikas amin4-Nov-05 3:24 
GeneralUSB formatting Pin
vranam10-Jun-05 2:52
membervranam10-Jun-05 2:52 
GeneralRe: USB formatting Pin
dotnfc12-Jun-05 14:37
memberdotnfc12-Jun-05 14:37 
Questionthis runs only with DDK installed, doesn't it? Pin
Anonymous4-Apr-05 23:18
memberAnonymous4-Apr-05 23:18 
AnswerRe: this runs only with DDK installed, doesn't it? Pin
dotnfc12-Jun-05 14:40
memberdotnfc12-Jun-05 14:40 
GeneralPlease help me How to get the Drive letter on win 98 and ME Pin
Ved26-Sep-04 15:38
memberVed26-Sep-04 15:38 
GeneralRe: Please help me How to get the Drive letter on win 98 and ME Pin
jiaojilin13-Jun-05 18:50
memberjiaojilin13-Jun-05 18:50 
GeneralPlease Help Pin
Horsy21-Sep-04 13:27
memberHorsy21-Sep-04 13:27 
QuestionHow can I enhence this code read USB device VendorID and ProductID in integer form Pin
subash11076-Jul-04 20:59
membersubash11076-Jul-04 20:59 
GeneralUSBDisk drive letter w/o MFC Pin
vinovarkey11-May-04 5:11
membervinovarkey11-May-04 5:11 
GeneralRe: USBDisk drive letter w/o MFC Pin
dotnfc12-May-04 16:13
memberdotnfc12-May-04 16:13 
GeneralRe: USBDisk drive letter w/o MFC Pin
raymondlim18-Mar-05 20:12
memberraymondlim18-Mar-05 20:12 
GeneralIt needs Administrator privileges Pin
whousesmyname19-Apr-04 0:55
memberwhousesmyname19-Apr-04 0:55 
GeneralRe: It needs Administrator privileges Pin
dotnfc28-Apr-04 16:48
memberdotnfc28-Apr-04 16:48 
GeneralRe: It needs Administrator privileges Pin
Alexey aka inko14-Jun-05 20:27
memberAlexey aka inko14-Jun-05 20:27 
Generaldoesn't work correctly Pin
Valery A. Boronin30-Mar-04 18:15
memberValery A. Boronin30-Mar-04 18:15 
GeneralRe: doesn't work correctly Pin
dotnfc31-Mar-04 13:50
memberdotnfc31-Mar-04 13:50 
GeneralRe: doesn't work correctly Pin
dotnfc31-Mar-04 15:06
memberdotnfc31-Mar-04 15:06 
Generaldoesn't work correctly 2time Pin
opengates22-Aug-05 6:17
memberopengates22-Aug-05 6:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 28 Mar 2004


107 bookmarked