I'm searching about this since last week, almost all links about DriveLetter x DevicePath/Volume/Device ID/whatever are purple for me.
I'm developing an application that List some sotorage devices and also HDI/WPD devices using SetupApi functions with these guids GUID_DEVINTERFACE_USB_DEVICE "A5DCBF10-6530-11D2-901F-00C04FB951ED", GUID_DEVINTERFACE_VOLUME "53F5630D-B6BF-11D0-94F2-00A0C91EFB8B".
So... I already able to map devices with drive letter. How? With SetupApi result, using DevicePath to get a DiskNumber and PartitionNumber with DeviceIOControl. And it's ok!!! Actually not all. Actually to CDROM drives I can't get it.
Here's my code.
int bytesReturned = 0;
IOCTL.DiskExtents de1 = new IOCTL.DiskExtents();
int numDiskExtents = 0;
IntPtr ptrDe1 = Marshal.AllocHGlobal(Marshal.SizeOf(de1));
bool result = IOCTL.DeviceIoControl(FileHandle, IOCTL.IOControlCodes.IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, ptrDe1, Marshal.SizeOf(de1), bytesReturned, null);
if ((!result)) {
Marshal.FreeHGlobal(ptrDe1);
//libera a memória alocada.
throw new System.ComponentModel.Win32Exception(); //is thrown here
} else {
de1 = Marshal.PtrToStructure(ptrDe1, typeof(IOCTL.DiskExtents));
Marshal.FreeHGlobal(ptrDe1);
return de1.first.DiskNumber;
}
For all CDROM drive I have, an IncorrectFunction is thrown.
For get the FileHandle I use both, Drive Letter (like "\.\D:") and Device Path. And both fail.
I don't know if I am doing something wrong, 'cause this work for USB Mass Storage and hard disks.
Thanks.
Now I'm using VolumeName from DevicePath instead of DiskNumber and PartitionNumber. We get a VolumeName from "\.\C:" and the DevicePath.