I'm trying to differentiate between a USB flash drive and a USB hard drive on Windows using the Win32 API.
The GetDriveType() function will return DRIVE_REMOVABLE if the drive is removable, and USB flash drives are of course removable. But I'm thinking that Windows probably considers USB hard drives removable as well (unfortunately I don't have access to a USB hard drive to test it out).
Thanks in advance.
A USB flash drive is a data storage device that includes flash memory with an integrated USB interface, and it's also called a pen drive and thumb drive. An external hard drive is a hard disk storage product that emphasizes portability and realizes large-capacity data exchange between computers.
A “thumb” drive is an small flash drive - solid state memory. An external hard drive is a mechanical drive, both usually interfaced by USB. I find flash drives to be much less reliable than external hard drives. I know of a number of flash drives that have failed.
Generally, a USB flash drive only weighs 20 to 50g. While external hard drives are shaped like hard drives because they're meant to replace internal hard drives. Usually, there are three types of external hard drives: 3.5-inch desktop hard drives, 2.5-inch notebook hard drives, and 1.8-inch mini hard drives.
What is a USB drive? USB (universal serial bus) drive is a type of flash drive. It has all the features of the flash drive, for example, it's plug and play, easy to carry, and does not require a physical drive. However, only flash drives with an USB interface can be called USB drives.
If you want to determine that a device is USB device, you can open its handle and send IOCTL queries using DeviceIoControl() to get bus type a device is connected to.
EnumUsbDrivesLetters - the post is in Russian but it contains C++ source code, so the matter could be understood easily.
Cheers, Andriy
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Method OpenVolume
// Purpose: Open volume for removal. Change to ::CreateFile(volumeName, 0, 0, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, 0);
// if you just want to inquire if it's removable.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
HANDLE OpenVolume(const char& driveLetter)
{
char volumeName[8] = "";
char* volumeFormat = "\\\\.\\%c:";
sprintf(volumeName, volumeFormat, driveLetter);
HANDLE volume = ::CreateFile(volumeName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (volume == INVALID_HANDLE_VALUE) return INVALID_HANDLE_VALUE;
DWORD bytesReturned = 0;
STORAGE_HOTPLUG_INFO Info = {0};
if (::DeviceIoControl(volume, IOCTL_STORAGE_GET_HOTPLUG_INFO, 0, 0, &Info, sizeof(Info), &bytesReturned, NULL))
{
if (!(Info.MediaRemovable || Info.DeviceHotplug))
{
::CloseHandle(volume);
::SetLastError(ERROR_INVALID_PARAMETER);
return INVALID_HANDLE_VALUE;
}
}
return volume;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With