2

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
lcssanches
  • 995
  • 12
  • 33
  • 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 Jan 24 '12 at 12:12
  • 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 Jan 24 '12 at 13:28
  • Sorry, but I can't answer my own question. Because my reputation is too low. Was the first thing I tried. – lcssanches Jan 24 '12 at 14:22
  • "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 Jan 24 '12 at 14:28
  • Sorry, thought it was part of *Remove new user restrictions*. – Yuck Jan 24 '12 at 14:31

1 Answers1

1

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

animuson
  • 53,861
  • 28
  • 137
  • 147
lcssanches
  • 995
  • 12
  • 33