Click here to Skip to main content
11,438,636 members (74,710 online)
Click here to Skip to main content

How To get the usbdisk's drive letter properly

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

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. Wink | ;-) 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 daySmile | :)

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 BSmile | :) 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)

Share

About the Author

dotnfc
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

 
Questionimplement Pin
Member 100390669-May-13 2:32
memberMember 100390669-May-13 2:32 
Generalstruct alignment issue Pin
aandsc23-Nov-09 4:49
memberaandsc23-Nov-09 4:49 
GeneralRe: struct alignment issue Pin
bachtkb2-Dec-09 16:03
memberbachtkb2-Dec-09 16:03 
Generalquerying through IOCTL_STORAGE_QUERY_PROPERTY Pin
usb_uv21-Apr-09 20:53
memberusb_uv21-Apr-09 20:53 
QuestionHow to Prepare a USB Drive for Safe Removal Pin
usb_uv17-Feb-09 22:28
memberusb_uv17-Feb-09 22:28 
QuestionIs there a .NET version? Pin
Tony Reynolds26-Jan-09 4:44
memberTony Reynolds26-Jan-09 4:44 
GeneralVery useful and clear article. Thanks a lot! Pin
Laurent Cozic17-Dec-08 11:28
memberLaurent Cozic17-Dec-08 11:28 
GeneralCreateFile function returns -1 Pin
usb_uv20-Aug-08 10:49
memberusb_uv20-Aug-08 10:49 
GeneralRe: CreateFile function returns -1 Pin
bachtkb2-Dec-09 16:10
memberbachtkb2-Dec-09 16:10 
GeneralAvoid the Windows XP popup on inserting a USB device Pin
AlexEvans14-Jul-08 22:12
memberAlexEvans14-Jul-08 22:12 
GeneralPen Drive letter detection.. [modified] Pin
amshoyeb16-Mar-08 19:38
memberamshoyeb16-Mar-08 19:38 
GeneralSD card CID register Pin
mitya200515-Nov-07 3:27
membermitya200515-Nov-07 3:27 
QuestionHow to get the vendor IDs & product IDs of all USB devices Pin
Member #384610120-Feb-07 2:42
memberMember #384610120-Feb-07 2:42 
GeneralError code 87 Pin
Volperossa20-Oct-06 6:01
memberVolperossa20-Oct-06 6:01 
GeneralRe: Error code 87 Pin
Marcel_NL28-Mar-07 5:50
memberMarcel_NL28-Mar-07 5:50 
GeneralRe: Error code 87 Pin
wardk29-Mar-07 3:26
memberwardk29-Mar-07 3:26 
GeneralRe: Error code 87 Pin
aandsc23-Nov-09 4:40
memberaandsc23-Nov-09 4:40 
GeneralAdministrative privileges required Pin
Francesco Foti20-Jun-06 4:07
memberFrancesco Foti20-Jun-06 4:07 
GeneralNeat! ... BUT Pin
SexyLilDarkPaw28-May-06 3:05
memberSexyLilDarkPaw28-May-06 3:05 
GeneralThe link : Pin
Ryan.Monti22-Mar-06 21:59
memberRyan.Monti22-Mar-06 21:59 
GeneralI think this link can help you Pin
Ryan.Monti22-Mar-06 21:57
memberRyan.Monti22-Mar-06 21:57 
Generalthanks Pin
armen-ia8-Mar-06 9:50
memberarmen-ia8-Mar-06 9:50 
QuestionHow to detect card in USB multi-card reader? Pin
tserg11-Jan-06 13:13
membertserg11-Jan-06 13:13 
AnswerRe: How to detect card in USB multi-card reader? Pin
JVDani18-Nov-07 21:36
memberJVDani18-Nov-07 21:36 
Questionhow to eject a USB-pen Pin
chergui5-Jan-06 2:55
memberchergui5-Jan-06 2:55 
AnswerRe: how to eject a USB-pen Pin
f22_storm5-Jan-06 15:08
memberf22_storm5-Jan-06 15:08 
AnswerRe: how to eject a USB-pen Pin
f22_storm5-Jan-06 15:14
memberf22_storm5-Jan-06 15:14 
AnswerRe: how to eject a USB-pen Pin
chergui6-Jan-06 0:22
memberchergui6-Jan-06 0:22 
GeneralUSB communication PC to Palm to PC Pin
Afer Ventus1-Dec-05 4:21
memberAfer Ventus1-Dec-05 4:21 
QuestionHow to get & send data to USB Pin
vikas amin4-Nov-05 4:24
membervikas amin4-Nov-05 4:24 
GeneralUSB formatting Pin
vranam10-Jun-05 3:52
membervranam10-Jun-05 3:52 
GeneralRe: USB formatting Pin
f22_storm12-Jun-05 15:37
memberf22_storm12-Jun-05 15:37 
Questionthis runs only with DDK installed, doesn't it? Pin
Anonymous5-Apr-05 0:18
sussAnonymous5-Apr-05 0:18 
AnswerRe: this runs only with DDK installed, doesn't it? Pin
f22_storm12-Jun-05 15:40
memberf22_storm12-Jun-05 15:40 
GeneralPlease help me How to get the Drive letter on win 98 and ME Pin
Ved26-Sep-04 16:38
memberVed26-Sep-04 16:38 
GeneralRe: Please help me How to get the Drive letter on win 98 and ME Pin
jiaojilin13-Jun-05 19:50
memberjiaojilin13-Jun-05 19:50 
GeneralPlease Help Pin
Horsy21-Sep-04 14:27
memberHorsy21-Sep-04 14:27 
QuestionHow can I enhence this code read USB device VendorID and ProductID in integer form Pin
subash11076-Jul-04 21:59
membersubash11076-Jul-04 21:59 
GeneralUSBDisk drive letter w/o MFC Pin
vinovarkey11-May-04 6:11
membervinovarkey11-May-04 6:11 
GeneralRe: USBDisk drive letter w/o MFC Pin
f22_storm12-May-04 17:13
memberf22_storm12-May-04 17:13 
GeneralRe: USBDisk drive letter w/o MFC Pin
RaymondLim18-Mar-05 21:12
memberRaymondLim18-Mar-05 21:12 
GeneralIt needs Administrator privileges Pin
whousesmyname19-Apr-04 1:55
memberwhousesmyname19-Apr-04 1:55 
GeneralRe: It needs Administrator privileges Pin
f22_storm28-Apr-04 17:48
memberf22_storm28-Apr-04 17:48 
GeneralRe: It needs Administrator privileges Pin
Alexey aka inko14-Jun-05 21:27
memberAlexey aka inko14-Jun-05 21:27 
Generaldoesn't work correctly Pin
Valerio30-Mar-04 19:15
memberValerio30-Mar-04 19:15 
GeneralRe: doesn't work correctly Pin
f22_storm31-Mar-04 14:50
memberf22_storm31-Mar-04 14:50 
GeneralRe: doesn't work correctly Pin
f22_storm31-Mar-04 16:06
memberf22_storm31-Mar-04 16:06 
Generaldoesn't work correctly 2time Pin
opengates22-Aug-05 7:17
memberopengates22-Aug-05 7:17 
GeneralRe: doesn't work correctly 2time Pin
prkoat17-Nov-05 14:17
memberprkoat17-Nov-05 14:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150506.1 | Last Updated 29 Mar 2004
Article Copyright 2004 by dotnfc
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid