【问题标题】:how to get USB hardware id using device id?如何使用设备 ID 获取 USB 硬件 ID?
【发布时间】:2009-10-12 12:55:02
【问题描述】:

如何使用设备 ID 获取 USB 设备的硬件 ID...我使用的是 vc++6.0,操作系统是 xp。 是否可以使用 wmi。

【问题讨论】:

标签: c++ winapi visual-c++ usb


【解决方案1】:

终于解决了我的问题...感谢您的回复... 我在这里发布代码,它可能对某人有用...通过此代码,我们可以获得连接到我们系统的设备的所有硬件ID..

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;

【讨论】:

    【解决方案2】:

    您可能会有些困惑,因为您假设 硬件 ID。 IoGetDeviceProperty(yourDevice, DevicePropertyHardwareID, ...) 返回一个列表。

    【讨论】:

    • 这不是内核级函数吗?我们可以从用户模式调用这个函数吗?我认为以 Io 开头并返回 NTSTATUS 的函数仅适用于驱动程序......
    猜你喜欢
    • 2012-05-19
    • 2011-01-02
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多