驱动器盘符的接口类型

发布于 2024-09-08 00:19:05 字数 80 浏览 3 评论 0原文

关于获取卷的设备接口类型(给定驱动器号(例如 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.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2024-09-15 00:19:05

您可以使用 GetDriveType 获取驱动器盘符的基本接口类型(即:可移动设备、CDROM、RAMDisk),另请参阅该页面底部的最终评论,了解有关可移动设备的更多信息。另请查看 SetupDiGetDeviceRegistryProperty 和 < a href="https://learn.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-deviceiocontrol" rel="nofollow noreferrer">DeviceIoControl

她是我最好的例子可以想出(未经测试,因为我没有 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; 
}

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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文