从 USB 设备中的设备路径获取卷名 [英] Getting Volume Name from Device Path in USB devices

查看:66
本文介绍了从 USB 设备中的设备路径获取卷名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Windows API 构建了一个程序,用于检测某些媒体(cd、usb...)的插入.程序返回设备路径:

I've built a program using the Windows API that detects the insertion of some media (cd, usb...). The program returns the device path:

\\\\?\\usb#vid_vvvv&pid_pppp#aaaaaaaaaaaaaaa#{gggggggg-gggg-gggg-gggg-gggggggggggg}

我正在使用函数 GetVolumeNameForVolumeMountPoint 通过解析报告的设备接口路径来获取卷名称 此处,但此功能似乎不适用于 USB 设备.

I am using the function GetVolumeNameForVolumeMountPoint to obtain the volume name by parsing the device interface path as reported here, but it seems that this feature is not working for USB devices.

知道如何在使用 USB 设备时从设备路径中获取卷名吗?

Any idea of how to get the volume name from the device path in case of working with usb devices?

推荐答案

//First get GUID
guid = GUID_DEVINTERFACE_VOLUME
//and get handle for Device information.

hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL,DIGCF_DEVICEINTERFACE|DIGCF_PRESENT); // Get device Information handle for Volume interface

//After that loop through SetupDiEnumDeviceInterfaces() and you will get the usb drive storage volume path

    for(dwIndex = 0; ;dwIndex ++) // Loop until device interfaces are found.
    { 
        ZeroMemory(&devInterfaceData, sizeof(devInterfaceData));
        devInterfaceData.cbSize = sizeof(devInterfaceData);

        if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid,dwIndex,&devInterfaceData))// Get device Interface data.
        {
            break;
        }
        ZeroMemory(&devInfoData, sizeof(devInfoData));
        devInfoData.cbSize = sizeof(devInfoData);

        pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
        pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);



        SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInterfaceData,pDevDetail,BUFFER_SIZE,&dwRequiredSize,&devInfoData); // SP_DEVINFO_DATA

        CM_Get_Parent(&devInstParent,devInfoData.DevInst, 0); // Get the device instance of parent. This points to USBSTOR.
        CM_Get_Device_ID(devInstParent, buf, BUFFER_SIZE,0);

nLength = strlen(pDevDetail->DevicePath);
            pDevDetail->DevicePath[nLength] = '\\';
            pDevDetail->DevicePath[nLength+1] = 0;  

if(GetVolumeNameForVolumeMountPoint(pDevDetail->DevicePath, volume,BUFFER_SIZE))
            {   
//Here you will get the volume corresponding to the usb
}

这篇关于从 USB 设备中的设备路径获取卷名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆