Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiating between USB flash drive and USB hard drive on Windows

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.

like image 460
user408962 Avatar asked Aug 02 '10 18:08

user408962


People also ask

What is the difference between a USB flash drive and hard drive?

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.

What's the difference between a thumb drive and an external hard drive?

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.

How much does a USB flash drive weigh?

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?

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.


2 Answers

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

like image 109
Andriy Avatar answered Sep 30 '22 15:09

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;
}
like image 22
Marty Avatar answered Sep 30 '22 15:09

Marty