2015-09-22 70 views
0

我已經構建了一個使用檢測某些媒體(cd,usb ...)的插入的Windows API的程序。該程序返回的設備路徑:從USB設備的設備路徑獲取卷名稱

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

我使用的功能GetVolumeNameForVolumeMountPoint通過解析設備接口路徑報道here獲得卷名,但似乎這個功能不能正常使用USB設備。

在使用USB設備的情況下,如何從設備路徑獲取卷名的任何想法?

回答

2
//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 
} 
+0

幾個月前,我設法讓它工作,但該解決方案運行良好,並且比我的優化更好。謝謝。 – Finfa811

+0

歡迎您 –