Can I distinguish removable drive (eg. USB flash drive) from fixed drive containing removable medium (eg. SD card reader or DVD-ROM)
-
Sunday, May 06, 2012 9:08 AM
Hello,
Context:
- Using Visual C/C++
- Targeting Windows Vista and later (but if a solution is available for WinXP too, it would be welcome)
- What is available to perform an operation: admin rights, regular application (exe), drivers of various types (including volume filter and device filter).
Problem:
Is there a way to distinguish between the following classes of storage devices?
I) A non-removable device that can contain a removable medium (such as DVD-ROM or SD card reader).
II) A removable drive that cannot contain any removable medium (such as USB flash drive).
Thank you very much.
- Edited by webdevix14541 Sunday, May 06, 2012 9:12 AM
All Replies
-
Sunday, May 06, 2012 5:47 PM
What about a USB DVD or card reader?
You can do some test IO and check the read speed if you are only interested in the medium.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Tuesday, May 08, 2012 7:48 AM
You're right about USB DVD drives. Let me rephrase and simplify it:
I was hoping there was a simple call that would answer the following simple Yes/No question:
Can the device contain a removable medium? (For USB flash drives the answer would be No, for SD card readers, Yes).
-
Tuesday, May 08, 2012 10:20 AM
-
Tuesday, May 08, 2012 2:49 PM
Read these.
Device Input and Output Control (IOCTL)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363219(v=vs.85).aspx
DeviceIoControl function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216(v=vs.85).aspx
IOCTL_STORAGE_QUERY_PROPERTY control code
http://msdn.microsoft.com/en-us/library/windows/hardware/ff560590(v=vs.85).aspx
STORAGE_DEVICE_DESCRIPTOR structure (see the RemovableMedia member)
http://msdn.microsoft.com/en-us/library/windows/hardware/ff566971(v=vs.85).aspx
- Edited by David Miller already exists Tuesday, May 08, 2012 2:49 PM
-
Tuesday, May 08, 2012 7:09 PM
Have you tried the GetDriveType API and checked for DRIVE_REMOVABLE?
Yes, sir, I had tried that. GetDriveType returns DRIVE_REMOVABLE for USB flash drives as well as card readers (strangely enough, for CD-ROM drives it doesn't).Dave
- Edited by webdevix14541 Tuesday, May 08, 2012 7:17 PM
-
Tuesday, May 08, 2012 7:15 PM
Read these.
Device Input and Output Control (IOCTL)
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363219(v=vs.85).aspx
DeviceIoControl function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216(v=vs.85).aspx
IOCTL_STORAGE_QUERY_PROPERTY control code
http://msdn.microsoft.com/en-us/library/windows/hardware/ff560590(v=vs.85).aspx
STORAGE_DEVICE_DESCRIPTOR structure (see the RemovableMedia member)
http://msdn.microsoft.com/en-us/library/windows/hardware/ff566971(v=vs.85).aspx
I am well aware of IOCTL and their usage. I'm looking for the specifics.
You mentioned some specifics, so let me address them:
STORAGE_DEVICE_DESCRIPTOR structure (see the RemovableMedia member)
If you read the documentation you link to, you will find out that it does not solve my problem. The doc for RemovableMedia member states: "Indicates when TRUE that the device's media (if any) is removable. If the device has no media, this member should be ignored."
Again, no way to simply decide if the device has any media.
IOCTL_STORAGE_QUERY_PROPERTY and other similar things would require playing a "whack-a-mole" game trying to white/black-list each specific medium. This is not future-proof and there are other problems with it.
I was hoping there was a simple call that would answer the following simple Yes/No question:
Can the device contain a removable medium? (For USB flash drives the answer would be No, for SD card readers, Yes).
- Edited by webdevix14541 Tuesday, May 08, 2012 7:16 PM
-
Wednesday, May 09, 2012 3:19 AM
I was hoping there was a simple call that would answer the following simple Yes/No question:
Can the device contain a removable medium? (For USB flash drives the answer would be No, for SD card readers, Yes).
NtQueryVolumeInformationFile(FileFsDeviceInformation)
http://msdn.microsoft.com/en-us/library/windows/hardware/ff556705(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/hardware/ff567070(v=vs.85).aspx
FILE_FS_DEVICE_INFORMATION structure
http://msdn.microsoft.com/en-us/library/windows/hardware/ff545788(v=vs.85).aspx
DEVICE_OBJECT structure (definitions for ‘Characteristics’)
http://msdn.microsoft.com/en-us/library/windows/hardware/ff543147(v=vs.85).aspx
FILE_REMOVABLE_MEDIA
“Indicates that the storage device supports removable media. Notice that this characteristic indicates removable media, not a removable device. For example, drivers for JAZ drive devices should specify this characteristic, but drivers for PCMCIA flash disks should not.”---------------------------------------------------------------------------
NtQueryVolumeInformationFile(FileFsDeviceInformation);
if (info.Characteristics & FILE_REMOVABLE_MEDIA)
{
// Yep!
}
- Edited by David Miller already exists Wednesday, May 09, 2012 3:25 AM
-
Wednesday, May 09, 2012 4:04 PM
I appreciate that response, but unfortunately that would work only for file systems. We need to be able to work with unformatted media too (which I forgot to mention in my first post). Any API call for that?NtQueryVolumeInformationFile(FileFsDeviceInformation);
if (info.Characteristics & FILE_REMOVABLE_MEDIA)
{
// Yep!
}
-
Wednesday, May 09, 2012 10:54 PM
We need to be able to work with unformatted media too (which I forgot to mention in my first post). Any API call for that?
SetupDiGetDeviceRegistryProperty(SPDRP_CAPABILITIES);
http://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx
or ..
CM_Get_DevNode_Registry_Property(CM_DRP_CAPABILITIES);
http://msdn.microsoft.com/en-us/library/windows/hardware/ff538496(v=vs.85).aspx
if (dwCapabilities & (CM_DEVCAP_EJECTSUPPORTED|CM_DEVCAP_REMOVABLE))
{
// Yeeha!
} -
Wednesday, May 09, 2012 11:13 PM
Any API call for that?
Also consider the IOCTL_DISK_GET_DRIVE_GEOMETRY control code.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365169(v=vs.85).aspx
DISK_GEOMETRY structure.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363972(v=vs.85).aspx
MEDIA_TYPE enumeration.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365231(v=vs.85).aspx
- Edited by David Miller already exists Wednesday, May 09, 2012 11:15 PM
-
Wednesday, May 09, 2012 11:20 PM
... unfortunately that would work only for file systems.NtQueryVolumeInformationFile(FileFsDeviceInformation);
if (info.Characteristics & FILE_REMOVABLE_MEDIA)
{
// Yep!
}
Are you sure?
The link below says NtQueryVolumeInformationFile also works with storage devices:
“Handle to a file object returned by ZwCreateFile or ZwOpenFile for an open file, directory, storage device, or volume for which volume information is being requested.”
http://msdn.microsoft.com/en-us/library/windows/hardware/ff567070(v=vs.85).aspx
-
Wednesday, May 09, 2012 11:40 PM
Another IOCTL to consider …
IOCTL_STORAGE_GET_HOTPLUG_INFO control code
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363408(v=vs.85).aspx
STORAGE_HOTPLUG_INFO structure
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363466(v=vs.85).aspx
MediaRemovable
If this member is set to a nonzero value, the device media is removable. Otherwise, the device media is not removable.
-
Thursday, May 10, 2012 2:26 PM
I think it would also depend on what the hardware reports.
I am in the transcription business and some voice recorders (memory card readers with fancy recording functions) report themselves as fixed hard drives instead of removable media, when connected via USB.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Thursday, May 10, 2012 4:46 PM
We need to be able to work with unformatted media too (which I forgot to mention in my first post). Any API call for that?
SetupDiGetDeviceRegistryProperty(SPDRP_CAPABILITIES);
http://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx
or ..
CM_Get_DevNode_Registry_Property(CM_DRP_CAPABILITIES);
http://msdn.microsoft.com/en-us/library/windows/hardware/ff538496(v=vs.85).aspx
if (dwCapabilities & (CM_DEVCAP_EJECTSUPPORTED|CM_DEVCAP_REMOVABLE))
{
// Yeeha!
}Those do not indicate real physical properties but rather the Windows software removal/eject functionality exposed to users.
-
Thursday, May 10, 2012 4:48 PM
Those do not distinguish between removable media and removable devices.Any API call for that?
Also consider the IOCTL_DISK_GET_DRIVE_GEOMETRY control code.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365169(v=vs.85).aspx
DISK_GEOMETRY structure.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363972(v=vs.85).aspx
MEDIA_TYPE enumeration.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365231(v=vs.85).aspx
-
Thursday, May 10, 2012 4:50 PM
Thank you for those pointers. This actually looked very promising, because the MS web page really states that the flag should be set only for real removable media . Unfortunately, a few real world tests showed that USB flash drives falsely fire as "containing removable media". It didn't surprise me though...
... unfortunately that would work only for file systems.NtQueryVolumeInformationFile(FileFsDeviceInformation);
if (info.Characteristics & FILE_REMOVABLE_MEDIA)
{
// Yep!
}
Are you sure?
The link below says NtQueryVolumeInformationFile also works with storage devices:
“Handle to a file object returned by ZwCreateFile or ZwOpenFile for an open file, directory, storage device, or volume for which volume information is being requested.”
http://msdn.microsoft.com/en-us/library/windows/hardware/ff567070(v=vs.85).aspx
-
Thursday, May 10, 2012 4:52 PM
Those functions apply only to hot-plug devices. We need to cover all bases.Another IOCTL to consider …
IOCTL_STORAGE_GET_HOTPLUG_INFO control code
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363408(v=vs.85).aspx
STORAGE_HOTPLUG_INFO structure
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363466(v=vs.85).aspx
MediaRemovable
If this member is set to a nonzero value, the device media is removable. Otherwise, the device media is not removable.
-
Thursday, May 10, 2012 4:52 PM
It's actually OK if they report themselves as fixed hard drives, as long as they also report removable media inside. ;)I think it would also depend on what the hardware reports.
I am in the transcription business and some voice recorders (memory card readers with fancy recording functions) report themselves as fixed hard drives instead of removable media, when connected via USB.
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Thursday, May 10, 2012 4:53 PMThanks for all the replies so far. Very appreciated.
-
Friday, May 11, 2012 11:34 AMModerator
I will try to involve others senior engineers into this thread to help you.
Best wishes,
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Friday, May 11, 2012 1:59 PM
That's very kind of you. Thank you! :)I will try to involve others senior engineers into this thread to help you.
-
Monday, May 14, 2012 12:07 PMModeratorYou're welcome!
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, May 16, 2012 6:18 PM
Perhaps this could help you start...
How the Group policy tells removable device from removable media?
-- pa
-
Friday, May 18, 2012 5:58 AM
You might want to check this document first:
Removable Storage Devices and Windows Vista Support
http://msdn.microsoft.com/en-US/library/windows/hardware/gg463533
==================
e.g.
ATA HDD is "Fixed device with fixed media"
ATAPI CD Drive is "Fixed device with removable media"
USB Flash Disk(UFD) is "Hot Pluggable with fixed media"
USB Card Reader is "Hot pluggable with removable media"
=================
- Hot Pluggable
Hot plugging is normally per Connector Interface. Devices that can be added to or removed from the machine while the system is running. USB flash disks and IEEE 1394 disks are examples of hot-pluggable storage device.
Call IOCTL_STORAGE_QUERY_PROPERTY with StorageAdapterProperty to get STORAGE_BUS_TYPE, if storage bus is USB/1394, it's hot pluggable device.
- Removable Media
- Use SetupDiGetDeviceRegistryProperty to query FILE_REMOVABLE_MEDIA characteristic
- Send SCSI pass-through SCSI INQUIRY request. Note: Some UFD vendors choose to set the Removable property in SCSI INQUIRY responses even though the storage media cannot be separated from the physical device
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Edited by Fred Zh - MSFT Monday, May 21, 2012 7:18 AM
- Proposed As Answer by Fred Zh - MSFT Wednesday, May 23, 2012 6:44 AM


