2009-10-12 142 views
4

如何使用設備ID獲得USB設備的硬件ID ...我使用的是vC++ 6.0,操作系統是xp。 是否有可能通過使用wmi。如何使用設備ID獲取USB硬件ID?

+0

你能發表一些你已經有的細節嗎? – 2009-10-12 20:44:00

+1

請注意,設備ID本身已經是硬件ID。請參閱http://msdn.microsoft.com/en-us/library/dd567931.aspx – MSalters 2009-10-13 14:41:31

回答

0

,因爲你承擔硬件ID你可能會有些困惑。 IoGetDeviceProperty(yourDevice, DevicePropertyHardwareID, ...)返回一個列表。

+1

這不是內核級功能嗎?我們可以從用戶模式調用這個函數嗎?我認爲以Io開頭並返回NTSTATUS的函數僅適用於司機... – armanali 2014-12-04 18:46:51

6

最後,我解決了我的問題...感謝您的回覆... 我在這裏發佈代碼,它可能對某人有用......通過此代碼,我們可以獲得所有設備的hardwareids到我們的系統..

HDEVINFO hDevInfo; 
    SP_DEVINFO_DATA DeviceInfoData; 
    DWORD i; 

    // Create a HDEVINFO with all present devices. 
    hDevInfo = SetupDiGetClassDevs(NULL, 
     0, // Enumerator 
     0, 
     DIGCF_PRESENT | DIGCF_ALLCLASSES); 

    if (hDevInfo == INVALID_HANDLE_VALUE) 
    { 
     //Error handling here. 
     printf("Error Details:[%s]\n","INVALID_HANDLE_VALUE"); 
     return 1; 
    } 

    // Enumerate through all devices in Set. 

    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); 
    for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i, 
     &DeviceInfoData);i++) 
    { 
     DWORD DataT; 
     LPTSTR buffer = NULL; 
     LPTSTR hwbuffer=NULL; 
     DWORD buffersize = 0; 

     // 
     // Call function with null to begin with, 
     // then use the returned buffer size (doubled) 
     // to Alloc the buffer. Keep calling until 
     // success or an unknown failure. 
     // 
     // Double the returned buffersize to correct 
     // for underlying legacy CM functions that 
     // return an incorrect buffersize value on 
     // DBCS/MBCS systems. 
     // 
     while (!SetupDiGetDeviceRegistryProperty(
      hDevInfo, 
      &DeviceInfoData, 
      SPDRP_HARDWAREID, 
      &DataT, 
      (PBYTE)buffer, 
      buffersize, 
      &buffersize)) 
     { 

      if (GetLastError() == 
       ERROR_INSUFFICIENT_BUFFER) 
      { 
       // Change the buffer size. 
       if (buffer) LocalFree(buffer); 
       // Double the size to avoid problems on 
       // W2k MBCS systems per KB 888609. 
       buffer = (char*)LocalAlloc(LPTR,buffersize * 2); 
      } 
      else 
      { 
       //Error handling here. 
       //printf("Error Details:[%s]\n",GetLastError()); 
       break; 
      } 
     } 
     printf("Test Result:[%s]\n",buffer); 



     if (buffer) LocalFree(buffer); 
    } 


    if (GetLastError()!=NO_ERROR && 
     GetLastError()!=ERROR_NO_MORE_ITEMS) 
    { 
     // Error handling here. 
     printf("Error Details:[%s]\n",GetLastError()); 
     return 1; 
    } 

    // Cleanup 
    SetupDiDestroyDeviceInfoList(hDevInfo); 


    return 0; 
+0

適用於我。請標記爲解決方案。 – armanali 2014-12-04 18:46:02