枚举系统中所有USB HUB设备

 

  1. // HUB.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include
    #include
    #include
    #include
  4. #include
    #include
    #include
    #include
  5. extern "C" {
    #include "cfgmgr32.h"
    #include "hidsdi.h"
    }
  6. #pragma   comment(lib,"hid.lib")    
    #pragma   comment(lib,"cfgmgr32.lib")  
  7. #define USB_DEVICE_DESCRIPTOR_TYPE                0x01
  8. #define USB_CONFIGURATION_DESCRIPTOR_TYPE         0x02
  9. #define USB_STRING_DESCRIPTOR_TYPE                0x03
  10. #define USB_INTERFACE_DESCRIPTOR_TYPE             0x04
  11. #define USB_ENDPOINT_DESCRIPTOR_TYPE              0x05
  12. #define USB_GET_HUB_NAME            8
  13. #define USB_GET_NODE_INFORMATION                    258
  14. #define FILE_DEVICE_USB         FILE_DEVICE_UNKNOWN
  15. #define FILE_DEVICE_USB_SCAN    0x8000
  16. #define IOCTL_INDEX             0x0800
  17. #define IOCTL_GET_USB_DESCRIPTOR        CTL_CODE(FILE_DEVICE_USB_SCAN,IOCTL_INDEX+8, METHOD_BUFFERED,FILE_ANY_ACCESS)
  18. DEFINE_GUID(GUID_DEVINTERFACE_USB_HUB,    0xf18a0e88, 0xc30c, 0x11d0, 0x88, 0x15, 0x00, 0xa0, 0xc9, 0x06, 0xbe, 0xd8);
  19. #define IOCTL_INTERNAL_USB_GET_HUB_NAME  CTL_CODE(FILE_DEVICE_USB,  /
  20.                                                 USB_GET_HUB_NAME,  /
  21.                                                 METHOD_BUFFERED,  /
  22.                                                 FILE_ANY_ACCESS)
  23. #define IOCTL_USB_GET_NODE_INFORMATION   CTL_CODE(FILE_DEVICE_USB,  /
  24.                                                USB_GET_NODE_INFORMATION,  /
  25.                                                METHOD_BUFFERED,  /
  26.                                                FILE_ANY_ACCESS)
  27. typedef struct _USBSCAN_GET_DESCRIPTOR {
  28.   IN UCHAR  DescriptorType;
  29.   IN UCHAR  Index;
  30.   IN USHORT  LanguageId;
  31. } USBSCAN_GET_DESCRIPTOR, *PUSBSCAN_GET_DESCRIPTOR;
  32. typedef struct _USB_DEVICE_DESCRIPTOR { 
  33.   UCHAR  bLength ;
  34.   UCHAR  bDescriptorType ;
  35.   USHORT  bcdUSB ;
  36.   UCHAR  bDeviceClass ;
  37.   UCHAR  bDeviceSubClass ;
  38.   UCHAR  bDeviceProtocol ;
  39.   UCHAR  bMaxPacketSize0 ;
  40.   USHORT  idVendor ;
  41.   USHORT  idProduct ;
  42.   USHORT  bcdDevice ;
  43.   UCHAR  iManufacturer ;
  44.   UCHAR  iProduct ;
  45.   UCHAR  iSerialNumber ;
  46.   UCHAR  bNumConfigurations ;
  47. } USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR ;
  48. typedef enum _USB_HUB_NODE {
  49.     UsbHub,
  50.     UsbMIParent
  51. } USB_HUB_NODE;
  52. typedef struct _USB_HUB_DESCRIPTOR {  UCHAR  bDescriptorLength;  UCHAR  bDescriptorType;  UCHAR  bNumberOfPorts;  USHORT  wHubCharacteristics;  UCHAR  bPowerOnToPowerGood;  UCHAR  bHubControlCurrent;  UCHAR  bRemoveAndPowerMask[64];       } USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
  53. typedef struct _USB_HUB_INFORMATION {
  54.     /*
  55.        copy of data from hub descriptor
  56.     */
  57.     USB_HUB_DESCRIPTOR HubDescriptor;
  58.     BOOLEAN HubIsBusPowered;
  59. } USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
  60. typedef struct _USB_MI_PARENT_INFORMATION {
  61.     ULONG NumberOfInterfaces;
  62. } USB_MI_PARENT_INFORMATION, *PUSB_MI_PARENT_INFORMATION;
  63. typedef struct _USB_NODE_INFORMATION {
  64.     USB_HUB_NODE NodeType;        /* hub, mi parent */
  65.     union {
  66.         USB_HUB_INFORMATION HubInformation;
  67.         USB_MI_PARENT_INFORMATION MiParentInformation;
  68.     } u;
  69. } USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
  70. int _tmain(int argc, _TCHAR* argv[])
  71. {
  72.     HANDLE                              hHub;
  73.     HDEVINFO                            hDeviceInfo;
  74.     SP_DEVICE_INTERFACE_DATA            _sdid;
  75.     PSP_DEVICE_INTERFACE_DETAIL_DATA    _psdidd;
  76.     DWORD dwLength;
  77.     DWORD dwBytesX;
  78.     __try{
  79.         hDeviceInfo = SetupDiGetClassDevs(
  80.             &GUID_DEVINTERFACE_USB_HUB,
  81.             NULL,
  82.             NULL,
  83.             DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
  84.         );
  85.         if( hDeviceInfo == INVALID_HANDLE_VALUE ){
  86.             printf("error:hDevInfo == INVALID_HANDLE_VALUE"); 
  87.             return -1;
  88.         }
  89.         _sdid.cbSize    = sizeof(SP_DEVICE_INTERFACE_DATA);
  90.         forint i = 0; SetupDiEnumDeviceInterfaces( hDeviceInfo, NULL, &GUID_DEVINTERFACE_USB_HUB, i, &_sdid ); i++ ){
  91.             SetupDiGetDeviceInterfaceDetail(
  92.                 hDeviceInfo,
  93.                 &_sdid,
  94.                 NULL,
  95.                 0,
  96.                 &dwBytesX,
  97.                 NULL
  98.             );
  99.             dwLength        = dwBytesX;
  100.             _psdidd         = (PSP_DEVICE_INTERFACE_DETAIL_DATA)GlobalAlloc( GPTR, dwLength );
  101.             if( _psdidd == NULL ){
  102.                 continue;
  103.             }
  104.             
  105.             _psdidd->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  106.             if( !SetupDiGetDeviceInterfaceDetail(
  107.                 hDeviceInfo,
  108.                 &_sdid,
  109.                 _psdidd,
  110.                 dwLength,
  111.                 &dwBytesX,
  112.                 NULL ) )
  113.             {
  114.                 GlobalFree( _psdidd );
  115.                 continue;
  116.             }
  117.             hHub    = CreateFile(
  118.                 _psdidd->DevicePath,
  119.                 GENERIC_READ,
  120.                 FILE_SHARE_READ | FILE_SHARE_WRITE,
  121.                 NULL,
  122.                 OPEN_EXISTING,
  123.                 0,
  124.                 NULL
  125.             );
  126.             GlobalFree( _psdidd );
  127.             if( hDeviceInfo == INVALID_HANDLE_VALUE ){
  128.                 continue;
  129.             }
  130.             USBSCAN_GET_DESCRIPTOR  _usb_nif;
  131.             memset( &_usb_nif, 0, sizeof(USBSCAN_GET_DESCRIPTOR) );
  132.             _usb_nif.DescriptorType = USB_DEVICE_DESCRIPTOR_TYPE;
  133.             _usb_nif.Index          = 1;
  134.             _usb_nif.LanguageId     = 0;
  135.             USB_DEVICE_DESCRIPTOR   _usbdd;
  136.             memset( &_usbdd, 0, sizeof(USB_DEVICE_DESCRIPTOR) );
  137.             dwLength    = sizeof(USB_DEVICE_DESCRIPTOR);
  138.             if( !DeviceIoControl(
  139.                 hHub,
  140.                 IOCTL_GET_USB_DESCRIPTOR,
  141.                 &_usb_nif,
  142.                 sizeof(USBSCAN_GET_DESCRIPTOR),
  143.                 &_usbdd,
  144.                 dwLength,
  145.                 &dwBytesX,
  146.                 NULL ) )
  147.             {   
  148.                 CloseHandle( hHub );
  149.                 printf( "%d/n", GetLastError() );
  150.                 continue;
  151.             }
  152.             
  153.             wprintf( _T("%d,%d/n"), _usbdd.idVendor, _usbdd.iProduct );
  154.             CloseHandle( hHub );
  155.         }
  156.     }
  157.     __finally{
  158.         if( hDeviceInfo != INVALID_HANDLE_VALUE ){
  159.             SetupDiDestroyDeviceInfoList( hDeviceInfo );
  160.         }
  161.     }
  162.     system( "pause" );
  163.     return 0;
  164. }

你可能感兴趣的:(C,C++)