驱动器号的接口类型

驱动器号的接口类型

本文介绍了驱动器号的接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于获得卷的驱动器号(例如G :)的设备接口类型的任何建议?具体来说,我正在寻找不依赖WMI的解决方案.

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.

谢谢.

推荐答案

您可以使用 GetDriveType 来获取驱动器号的基本接口类型(即:可移动设备,CDROM,RAMDisk),另请参阅该页面底部的最终注释.有关可移动设备的更多信息.还要查看 SetupDiGetDeviceRegistryProperty DeviceIoControl

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

她是我能想到的最好的例子(因为我没有WDK/DDK,所以请耐心等待)

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;
}

这篇关于驱动器号的接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:55