2
votes

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.

1
I did it! Here we go. Now I'm using VolumeName from DevicePath instead of DiskNumber and PartitionNumber. We get a VolumeName from "\\.\C:" and the DevicePath. Great!!! Have a nive week!!!lcssanches
It's OK to answer your own question. Since this is now working for you, you should consider writing up how you fixed it as an answer and not just a comment.Yuck
Sorry, but I can't answer my own question. Because my reputation is too low. Was the first thing I tried.lcssanches
"Users with less than 100 reputation..." No, it'll be auto-answered in 5 hours(my guess). Or else, I'll try be here.lcssanches
Sorry, thought it was part of Remove new user restrictions.Yuck

1 Answers

1
votes

Now I'm using VolumeName from DevicePath instead of DiskNumber and PartitionNumber. We get a VolumeName from "\.\C:" and the DevicePath.