开发者

How to get the Drive letter and Mount Path - MSDN

I get the devices list from the system using SetupDiGetClassDevs Function - MSDN.

Also i can able to get the vendor id and product id from the devices.

But i cant able to get the drive letter and the mount path

For Example if i plug the usb drive means , i have to get the drive letter like "G:/"

Please help me to get the drive letter and mount path for the devices

if (SetupDiEnumDeviceInterfaces(hDevInfo,
    NULL,&GUID_DEVINTERFACE_USB_DEVICE,i,&Interface_Info))
{
    wprintf(L"\tDeviccvcvcveInstanceId : %d\n", i); 

    pspdidd->cbSize = sizeof(*pspdidd); 

    SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;

    DWORD dwDetDataSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA) + 256;

    pDetData = (SP_DEVICE_INTERFACE_DETAIL_DATA*) malloc (dwDetDataSize);

    pDetData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

   开发者_JAVA技巧SetupDiGetDeviceInterfaceDetail(hDevInfo,&Interface_Info,pDetData,dwDetDataSize, 
   NULL,&DeviceInfoData);

   qDebug ()<<QString::fromWCharArray( pDetData->DevicePath );

}


It's not that simple. You cannot call any API that will tell you: "Device X = Drive Y". That's because the relationship is not one-to-one; it's many-to-many.

A drive letter corresponds to a volume. A drive letter is a mount point. You can also mount a volume at a directory reparse point - the same volume can be mounted multiple times.

A single device can hold multiple volumes (disk partitions, for example). This is common.

A single volume may reside on multiple devices (spanned drives, for example). Yes, it's common for a volume to reside on a single partition of a single device, but that's not necessarily the case.

Note also that a volume does not have to be assigned a mount point at all (consider the 100MB EFI partition you often see on a Windows 7 boot drive).

One approach is the enumerate volumes by GUID (FindFirstVolume/FindNextVolume), then obtain their disk extents via IOCTL (IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS). That will tell you the disk number/s which hold parts of the volume. Then use GetVolumePathNamesForVolumeName to get the mount point/s for the volume (there may be at most one drive letter, but it can be mounted under multiple directories).

Yes, it really is that complicated.


You can use QueryDosDevice function (see http://msdn.microsoft.com/en-us/library/aa365461(VS.85).aspx). See http://msdn.microsoft.com/en-us/library/cc542456(VS.85).aspx for an code example.


Unfortunately this is not a very straightforward operation. There is a good walk-through on how to accomplish something similar on CodeProject. Basically every disk is assigned a unique device number and every volume has a device number corresponding to the disk on which it resides. So you have to open all the volumes and query for their device numbers (IOCTL_STORAGE_GET_DEVICE_NUMBER), then match them against the device number of the disk in question. The CodeProject code is going the other way (volume to disk), but you should be able to modify it easily enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜