Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if empty flash card reader is present in usb slot?

Tags:

c++

winapi

I use GetLogicalDrives() to get all drives on my computer but that function shows not only present ready to use volumes but also empty flash-readers with no card in them. Next the GetDriveType() shows code 2 for such volume. And that is no matter if the flash card is present or not in the slot. Some multi-readers produce many such non-existing drives, the question is how can I determine the correct status of such volume.

Trying to call FindFirstFile() on such non-present drive produces visual error even in console app : Exception Processing Message c0000013 Parameters 75b3bf7c 4 75b3bf7c 75b3bf7c After such error the code continues to run, but this annoying error shows up to the user in a window as the app would made a major crash.

So one method of dealing with that would be using FindFirstFile but I do not know any way to get that error out of the way of the user.

like image 593
rsk82 Avatar asked Dec 25 '12 00:12

rsk82


2 Answers

Finally I found it! The key to success here is GetVolumeInformation().

It returns 0 and sets last error to 21 when there is an empty card reader in usb slot. :)

like image 108
rsk82 Avatar answered Nov 17 '22 00:11

rsk82


Here are a few excellent choices:

  • How do I detected whether a hard drive is connected via USB?

In particular:

if( 2 == ::getDriveType( <driveletter> ))

Return values of function:

DRIVE_UNKNOWN 0: The drive type cannot be determined.

DRIVE_NO_ROOT_DIR 1: The root path is invalid; for example, there is no volume mounted at the specified path.

DRIVE_REMOVABLE 2: The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.

DRIVE_FIXED 3: The drive has fixed media; for example, a hard disk drive or flash drive.

DRIVE_REMOTE 4: The drive is a remote (network) drive.

DRIVE_CDROM 5: The drive is a CD-ROM drive.

DRIVE_RAMDISK 6: The drive is a RAM disk.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx

like image 41
paulsm4 Avatar answered Nov 16 '22 23:11

paulsm4