Alaska Software Inc. - Re: get usb / removable drive letters
Username: Password:
AuthorTopic: Re: get usb / removable drive letters
Andreas Gehrs-Pahl

View the complete thread for this message in:

Re: get usb / removable drive letters
on Tue, 21 Apr 2015 05:47:30 -0400
Shaun,

>Has anyone found a good way to get the drive letter(s) of a 
>removable/usb drive?

The Windows API Function that you probably want is named GetDriveType().

DllCall("Kernel32.dll", DLL_STDCALL, "GetDriveTypeA", cDriveName)

It will return one of the following seven Drive Type values:

#define DRIVE_UNKNOWN      0   The drive type cannot be determined.
#define DRIVE_NO_ROOT_DIR  1   The root path is invalid; for example, 
                               there is no volume mounted at the specified 
                               path.
#define DRIVE_REMOVABLE    2   The drive has removable media; for example, 
                               a floppy drive, thumb drive, or flash card 
                               reader.
#define DRIVE_FIXED        3   The drive has fixed media; for example, a 
                               hard disk drive or flash drive.
#define DRIVE_REMOTE       4   The drive is a remote (network) drive.
#define DRIVE_CDROM        5   The drive is a CD-ROM drive.
#define DRIVE_RAMDISK      6   The drive is a RAM disk.

Keep in mind that not all external (USB) drives are "Removable" in the sense 
of Floppy Disks or Flash Cards, so many USB-connected drives will show up as 
"Fixed Drives", instead. That basically means that even though "USB Drives" 
are usually "Removable", they often won't show up as "Removable Drives" with 
this function. 

The result of this API function should always match the result of the shell 
command: "fsutil fsinfo drivetype", though.

For more details on this function, see also the following link: 
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364939%28v=vs.85%29.aspx

I use the following routines to determine the available Drives and their 
types, and create an array that contains corresponding drive icons:

STATIC aDriveIcons := {NIL, NIL, ICON_DRIVE_REMOVABLE, ICON_DRIVE_FIXED, ICON_DRIVE_REMOTE, ICON_DRIVE_CDROM, ICON_DRIVE_RAMDISK}

Function ListAllDrives(cStartDrive, cEndDrive)
LOCAL cFirst  := iif(cStartDrive == NIL, 'C', cStartDrive)
LOCAL cLast   := iif(cEndDrive   == NIL, 'Z', cEndDrive)
LOCAL aDrives := {}
LOCAL nDrive  := 0
   for nDrive := asc(upper(cFirst)) to asc(upper(cLast))
      if DiskReady(chr(nDrive))
         AAdd(aDrives, {chr(nDrive), GetDriveIcon(chr(nDrive))})
      endif
   next nDrive
return (aDrives)

Function GetDriveIcon(cDrive)
LOCAL cDriveName := upper(left(cDrive, 1)) + ':\'
LOCAL nDriveType := DllCall("Kernel32.dll", DLL_STDCALL, "GetDriveTypeA", cDriveName)
return (aDriveIcons[nDriveType + 1])

DiskReady() is an XbTools III function, but can easily be replaced with an 
API or Xbase++ function of some type, if necessary.

Hope that helps,

Andreas

Andreas Gehrs-Pahl
Absolute Software, LLC

phone: (989) 723-9927
email: Andreas.GP@Charter.net
web:   http://www.Aerospace-History.net