interface type for a drive letter

心不动则不痛 提交于 2019-12-11 16:41:09

问题


Any suggestions on getting the device interface type for a volume, given its drive letter (e.g. G:)? Specifically, am looking for a solution that doesn't depend on WMI.

Thank you.


回答1:


You can use GetDriveType to get the basic interface type(ie: removable device, CDROM, RAMDisk) for the drive letter, also see the final comment at the bottom of that page for a little more info on removable devices. Also check out SetupDiGetDeviceRegistryProperty and DeviceIoControl

Her is the best example I can come up with(untested as I don't have the WDK/DDK)

bool IsUSBDevice(const char* szDrivePath, bool& bRemovable)
{
    if(GetDriveType(szDrivePath) != DRIVE_REMOVABLE)
        return false;

    HANDLE hDevice = CreateFile(szDrivePath,0,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
    if(hDevice == INVALID_HANDLE_VALUE)
        return false;

    STORAGE_PROPERTY_QUERY pQuery = {0};
    pQuery.PropertyId = StorageDeviceProperty;
    pQuery.QueryType = PropertyStandardQuery;

    STORAGE_DEVICE_DESCRIPTOR pDeviceDesc = {0};
    pDeviceDesc.Size = sizeof(pDeviceDesc);
    DWORD dwWritten = 0;
    if(DeviceIoControl(hDevice,IOCTL_STORAGE_QUERY_PROPERTY,&pQuery,sizeof(STORAGE_PROPERTY_QUERY),pDeviceDesc,sizeof(pDeviceDesc),&dwWritten,NULL))
    {
        CloseHandle(hDevice);
        return ((bRemovable = pDeviceDesc.RemovableMedia) && pDeviceDesc.BusType == BusTypeUsb);
    }
    else
        CloseHandle(hDevice);

    return false; 
}


来源:https://stackoverflow.com/questions/3157438/interface-type-for-a-drive-letter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!