How to determine the drive type
By Kent Reisdorph
It is often necessary to determine the types of the drives on a user’s machine. You may, for example, need to know which drive letter is associated with the CD-ROM drive, or how many CD-ROM drives are installed on a particular machine. Another scenario is that you may need to determine which of the drives on a system are hard drives, and which are drives containing removable media (a floppy drive, for example). Determining the drive type is fairly easy, as this article will show.
The GetDriveType() function
The Windows API includes a function, GetDriveType(), that tells you the type of each drive on a particular machine. The declaration for GetDriveType() is as follows:
UINT GetDriveType(
LPCTSTR lpRootPathName);
This method is fairly straightforward. You pass the drive letter of the drive to check, and GetDriveType() returns the type of the drive. The string passed to GetDriveType() is the drive letter with a trailing colon. You can pass the trailing backslash as well, but it isn’t necessary. The following two lines of code, then, are functionally equivalent:
int type;
type = GetDriveType("A:");
type = GetDriveType("A:\\");
It is important to realize that any combination of characters other than the above will fail. For example, the following two lines of code will each return 1, indicating that the drive does not exist:
int type;
type = GetDriveType("C");
type = GetDriveType("C:\\Windows");
// the value of type will be 1
You can pass NULL to GetDriveType(), in which case the drive type of the current drive will be returned.
Obviously, calling GetDriveType() is trivial. The rest of the work comes in interpreting the return value.
Return values
GetDriveType() returns an integer. This integer is a code that tells you the drive type. Table A shows the possible values.
Table A: Return values from GetDriveType()
| Value | Description |
| 0 | Drive type cannot be determined. |
| 1 | Drive does not exist. |
| DRIVE_REMOVABLE | Drive has removable media. |
| DRIVE_FIXED | Drive is a hard drive. |
| DRIVE_REMOTE | Drive is a network drive. |
| DRIVE_CDROM | Drive is a CD-ROM drive. |
| DRIVE_RAMDISK | Drive is a RAM disk. |
A return value of 0 indicates that the drive type cannot be determined. I have yet to see this value returned, so I can’t say with any certainty what set of circumstances will lead to this return value. If the drive is not mapped (i.e. doesn’t exist), 1 is returned.
A return value of DRIVE_REMOVABLE means that the drive has removable media. This can be a bit misleading. A CD-ROM drive, for example, obviously has removable media. However, a CD-ROM drive will return DRIVE_CDROM and not DRIVE_REMOVABLE. Other removable drive types include some large-capacity removable drives, such as ZIP and Jazz drives. These drive types will generally return DRIVE_REMOVABLE.
If GetDiskType() returns DRIVE_REMOTE, the drive is a mapped network drive and if it returns DRIVE_RAMDISK the drive is a RAM disk.
A simple example
The following example enumerates all drives (from A: to Z:). If the drive exists, the drive letter and type are added to a memo on the form. Here is that code:
void
__fastcall TForm1::Button1Click(
TObject *Sender)
{
for (char c='A';c<='Z';c++) {
String S = String(c) +
":";
int type =
GetDriveType(S.c_str());
if (type > 1) {
String
TypeStr;
switch
(type) {
case
DRIVE_REMOVABLE :
TypeStr = " is removable";
break;
case
DRIVE_FIXED :
TypeStr = " is fixed";
break;
case
DRIVE_REMOTE :
TypeStr = " is network drive";
break;
case
DRIVE_CDROM :
TypeStr = " is CD-ROM";
break;
case
DRIVE_RAMDISK :
TypeStr = " is RAM disk";
break;
}
Memo1->Lines->Add(S
+ TypeStr);
}
}
}
Conclusion
Getting the drive type is simple once you know about the GetDriveType() function. A related question is obviously, “Is there media in the removable media drive.” The answer to that question will have to wait for a later article.