2009-10-12 7 views
4

Comment obtenir l'identifiant matériel du périphérique USB en utilisant l'identifiant de l'appareil ... J'utilise vC++ 6.0 et OS est xp. Est-ce possible en utilisant wmi?comment obtenir l'ID de matériel USB en utilisant l'identifiant de l'appareil?

+0

Pouvez-vous poster quelques détails de ce que vous avez déjà? –

+1

Notez qu'un ID de périphérique lui-même déjà _IS_ un ID de matériel. Voir http://msdn.microsoft.com/en-us/library/dd567931.aspx – MSalters

Répondre

0

Vous pourriez être un peu confus car vous supposez l'ID du matériel. IoGetDeviceProperty(yourDevice, DevicePropertyHardwareID, ...) renvoie une liste.

+1

N'est-ce pas une fonction au niveau du noyau? Peut-on appeler cette fonction depuis le mode utilisateur? Je pense que les fonctions commençant par Io et renvoyant NTSTATUS ne sont destinées qu'aux pilotes ... – armanali

6

Enfin j'ai résolu mon problème ... merci pour vos réponses ... Je poste le code ici, il peut être utile pour quelqu'un ... par ce code, nous pouvons obtenir tous les hardwareids des appareils qui sont connceted à notre système ..

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

Fonctionne pour moi. Veuillez le marquer comme solution. – armanali