2010-08-06 145 views
4

我從USB設備插入我得到的設備ID的工作...MSDN CM_GET_Parent例不起作用

上述申請工作在雙贏的XP,但不是在Windows 7

當我揣摩,我發現CM_Get_Parent()不工作...

任何人可以請指導我爲什麼它不工作......,是有它可以在Windows 7

源代碼工作的任何兼容功能:

HDEVINFO hDevInfo; 
GUID guid; 
BYTE buffer[ 1024 ]; 
DWORD dwRequiredSize ; 
WCHAR buf[ 1024 ]; 
DEVINST devInstParent; 
WCHAR volume[1024]; 
int aLength, anIndex; 
SP_DEVICE_INTERFACE_DATA devInterfaceData; 
SP_DEVINFO_DATA devInfoData; 
PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail; 

guid = GUID_DEVINTERFACE_VOLUME; 
hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); 

if(hDevInfo == INVALID_HANDLE_VALUE) 
{ 
    qDebug() << "Error - SetupDiGetClassDevs failed\n"; 
} 

for(DWORD dwIndex = 0;; dwIndex ++) 
{ 
    ZeroMemory(&devInterfaceData, sizeof(devInterfaceData)); 
    devInterfaceData.cbSize = sizeof(devInterfaceData); 
    if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid,dwIndex, &devInterfaceData)) 
    { 
     break; 
    } 

    ZeroMemory(&devInfoData, sizeof(devInfoData)); 
    devInfoData.cbSize = sizeof(devInfoData); 

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

    // Get device interface detail data to get Device Instance from 
    // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA 
    SetupDiGetDeviceInterfaceDetail(
     hDevInfo,&devInterfaceData, 
     pDevDetail, // SP_DEVICE_INTERFACE_DETAIL_DATA 
     1024,&dwRequiredSize,&devInfoData); // SP_DEVINFO_DATA 

    CM_Get_Parent(&devInstParent,devInfoData.DevInst, 0); 
    CM_Get_Parent(&devInstParent,devInstParent, 0); 
    CM_Get_Device_ID( devInstParent, buf, 1024,0); 
} 
+1

前段時間我發佈了一個代碼示例在http://stackoverflow.com/questions/3098696/how-to-get-the-vid-pid-and-drive-letter-for所有使用'CM_Get_Parent'和我在Windows 7下測試過的usb-mass-storage-devices-plug。所以如果你有一個'CM_Get_Parent()'函數不工作的例子,你應該發佈相應的代碼示例可以用來重現問題。 – Oleg 2010-08-06 10:44:59

回答

2

沒有我的Windows 7計算機上的任何問題下面的痘痘修改後的代碼

#include <Windows.h> 
#include <setupapi.h> 
#include <cfgmgr32.h> 
#include <tchar.h> 
#include <stdio.h> 
#define ARRAY_SIZE(arr)  (sizeof(arr)/sizeof(arr[0])) 

#pragma comment (lib, "setupapi.lib") 

int main() 
{ 
    HDEVINFO hDevInfo; 
    GUID guid; 
    BYTE buffer[ 1024 ]; 
    DWORD dwRequiredSize ; 
    DEVINST devInstParent; 
    SP_DEVICE_INTERFACE_DATA devInterfaceData; 
    SP_DEVINFO_DATA devInfoData; 
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDevDetail; 
    CONFIGRET status; 
    TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN]; 
    BOOL bSuccess; 

    guid = GUID_DEVINTERFACE_VOLUME; 
    hDevInfo = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); 
    if(hDevInfo == INVALID_HANDLE_VALUE) { 
     return 1; 
    } 

    for(DWORD dwIndex = 0;; dwIndex ++) { 
     ZeroMemory(&devInterfaceData, sizeof(devInterfaceData)); 
     devInterfaceData.cbSize = sizeof(devInterfaceData); 

     if(!SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &guid, dwIndex, &devInterfaceData)) { 
      DWORD dwErrorCode = GetLastError(); 
      if (dwErrorCode == ERROR_NO_MORE_ITEMS) 
       break; 
      else 
       return 1; // ERROR!!! 
     } 

     ZeroMemory(&devInfoData, sizeof(devInfoData)); 
     devInfoData.cbSize = sizeof(devInfoData); 
     pDevDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer; 
     pDevDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); 

     // Get device interface detail data to get Device Instance from 
     // SP_DEVINFO_DATA and Device Path from SP_DEVICE_INTERFACE_DETAIL_DATA 
     bSuccess = SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInterfaceData, 
       pDevDetail,// SP_DEVICE_INTERFACE_DETAIL_DATA 
       sizeof(buffer),&dwRequiredSize,&devInfoData); // SP_DEVINFO_DATA 
     status = CM_Get_Device_ID (devInfoData.DevInst, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0); 
     if (status == CR_SUCCESS) { 
      _tprintf (TEXT("%s\n"), szDeviceInstanceID); 
     } 

     status = CM_Get_Parent(&devInstParent,devInfoData.DevInst, 0); 
     if (status == CR_SUCCESS) { 
      status = CM_Get_Device_ID (devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0); 
      if (status == CR_SUCCESS) 
       _tprintf (TEXT(" %s\n"), szDeviceInstanceID); 

      status = CM_Get_Parent(&devInstParent,devInstParent, 0); 
      if (status == CR_SUCCESS) { 
       status = CM_Get_Device_ID( devInstParent, szDeviceInstanceID, ARRAY_SIZE(szDeviceInstanceID), 0); 
       if (status == CR_SUCCESS) { 
        _tprintf (TEXT("  %s\n"), szDeviceInstanceID); 
       } 
      } 
      else { 
       continue; 
      } 
     } 
     else { 
      continue; 
     } 
    } 
} 

工作併產生輸出類似如下:

SCSI\CDROM&VEN_ELBY&PROD_CLONEDRIVE&REV_1.4\1&2AFD7D61&0&000000 
    ROOT\SCSIADAPTER\0000 
     HTREE\ROOT\0 
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#0000000000100000 
    ROOT\VOLMGR\0000 
     HTREE\ROOT\0 
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#000000004B100000 
    ROOT\VOLMGR\0000 
     HTREE\ROOT\0 
STORAGE\VOLUME\{21C004DA-0EB2-11DF-8C02-806E6F6E6963}#00000037C7A00000 
    ROOT\VOLMGR\0000 
     HTREE\ROOT\0 

修訂基於註釋:在我在Windows XP中的VMware相同的實用程序產生以下輸出

FDC\GENERIC_FLOPPY_DRIVE\6&1435B2E2&0&0 
    ACPI\PNP0700\5&324D5432&0 
     ACPI\PNP0A05\4&5289E18&0 
IDE\CDROMNECVMWAR_VMWARE_IDE_CDR10_______________1.00____\3031303030303030303030303030303030303130 
    PCIIDE\IDECHANNEL\4&23686003&0&1 
     PCI\VEN_8086&DEV_7111&SUBSYS_197615AD&REV_01\3&61AAA01&0&39 
STORAGE\VOLUME\1&30A96598&0&SIGNATURE91929192OFFSET7E00LENGTH1FFD5AA00 
    ROOT\FTDISK\0000 
     HTREE\ROOT\0 

因爲它是另一臺計算機(虛擬計算機)在另一個操作系統上,我們收到另一個設備列表。您當前的問題是使功能CM_Get_Parent()在Windows XP和Windows 7上都能正常工作。發佈的代碼就像我們所看到的那樣。或者它現在可以在Windows XP或Windows 7計算機上正常工作?

如何獲得PID和VID(產品ID和ventor ID)我Get information about disk drives result on windows7 - 32 bit system描述,但對於任何驅動器號而不是爲卷設備。可以很容易地更改代碼以顯示更多信息,如卷名,總線名稱和其他信息。但這一切已經是另一個問題。

更新:我放置程序的擴展版本從Get information about disk drives result on windows7 - 32 bit systemhttp://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.c下,因爲與在stackoverflow.com消息大小限制的。測試輸出看起來像http://www.ok-soft-gmbh.com/ForStackOverflow/EnumMassStorage.txt

+0

@barbgal:代碼是否在Windows 7和Windows XP計算機上運行? 'CM_Get_Parent()'還有問題嗎? – Oleg 2010-08-18 12:42:22