The heart of the following code snippet is DeviceIoControl Win32 function and IOCTL_STORAGE_GET_MEDIA_TYPES_EX control code(used to get the media types).
The documentation provided in MSDN for the usage of IOCTL_STORAGE_GET_MEDIA_TYPES_EX is really hopeless. There isn’t much to explain in the code.
- Enumerate each drive (using bit vector returned by GetLogicalDrives)
- Determine if the drive is a removeable drive(using GetDriveType)
- Open removable device(using CreateFile function) and pass the device handle to DeviceIoControl. ** the buffer passed to DeviceIoControl function must be big enough hold DEVICE_MEDIA_INFO ** (MSDN really sucks)
- Finally examine the mediaTypes->DeviceType value.
Hope it helps you. Comment on it.