본문 바로가기

[TIP] USB 장치를 열거하는 코드

파이팅건맨 2008.02.03 19:38 조회 수 : 14090

깔끔히 정리된 USB 장치를 열거하는 코드입니다.

출처는 아래와 같습니다.
PDF 자료를 긁어다 붙입니다.
*. 게시와 관련하여 문제가 있을 경우 연락 부탁드립니다.^^

http://www.intel.com/intelpress/usb/examples/DUSBVC.PDF



// DisplayUSB.cpp: Defines the entry point for the console application. // // Copyright (C) 2001, Intel Corporation // All rights reserved. // Permission is hereby granted to merge this program code with other program // material to create a derivative work. This derivative work may be distributed // in compiled object form only. Any other publication of this program, in any form, // without the explicit permission of the copyright holder is prohibited. // // Send questions and comments to [email protected] #include "stdafx.h" #include "objbase.h" #include "winioctl.h" #include "usbioctl.h" #include "stdio.h" #include "stdlib.h" bool DEBUG; // Set to true to enable DEBUG messages SECURITY_ATTRIBUTES SA; // Needed for Win2000 const char ClassName[] [20] = { "Reserved", "Audio", "Communications", "Human Interface", "Monitor", "Physical Interface", "Power", "Printer", "Storage", "Hub", "Vendor Specific", "*ILLEGAL VALUE*" }; const char ConnectionStatus[] [30] = { "No device connected", "Device connected", "Device FAILED enumeration", "Device general FAILURE", "Device caused overcurrent", "Not enough power for device" }; // Define all stuctures using UCHAR or BOOLEAN so that the variables are not 'aligned' by the compiler typedef struct DESCRIPTOR_REQUEST { ULONG ConnectionIndex; struct {UCHAR bmRequest; UCHAR bRequest; UCHAR wValue[2]; UCHAR wIndex[2]; UCHAR wLength[2];} SetupPacket; UCHAR Data[2048]; }; typedef struct DEVICE_DESCRIPTOR { UCHAR bLength; UCHAR bDescriptorType; UCHAR bcdUSB[2]; UCHAR bDeviceClass; UCHAR bDeviceSubClass; UCHAR bDeviceProtocol; UCHAR bMaxPacketSize0; UCHAR idVendor[2]; UCHAR idProduct[2]; UCHAR bcdDevice[2]; UCHAR iManufacturer; UCHAR iProduct; UCHAR iSerialNumber; UCHAR bNumConfigurations; }; typedef struct HUB_DESCRIPTOR { UCHAR bDescriptorLength; UCHAR bDescriptorType; UCHAR bNumberOfPorts; UCHAR wHubCharacteristics[2]; UCHAR bPowerOnToPowerGood; UCHAR bHubControlCurrent; UCHAR bRemoveAndPowerMask[64]; }; typedef struct NODE_INFORMATION { USB_HUB_NODE NodeType; HUB_DESCRIPTOR HubDescriptor; BOOLEAN HubIsBusPowered; }; typedef struct NODE_CONNECTION_INFORMATION { ULONG ConnectionIndex; DEVICE_DESCRIPTOR DeviceDescriptor; UCHAR CurrentConfigurationValue; BOOLEAN LowSpeed; BOOLEAN DeviceIsHub; UCHAR DeviceAddress[2]; UCHAR NumberOfOpenPipes[4]; UCHAR ConnectionStatus[4]; USB_PIPE_INFO PipeList[32]; }; USHORT DisplayStringDescriptor (HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID, UCHAR Index) { if (DEBUG) printf("\nIn DisplayStringDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x, Index = %x\n", HubHandle, PortIndex, LanguageID, Index); DESCRIPTOR_REQUEST Packet; DWORD BytesReturned; bool Success; if (LanguageID == 0) { // Get the language ID memset(&Packet, 0, sizeof(Packet)); Packet.ConnectionIndex = PortIndex; Packet.SetupPacket.bmRequest = 0x80; Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR; Packet.SetupPacket.wValue[1] = USB_STRING_DESCRIPTOR_TYPE; Packet.SetupPacket.wLength[0] = 4; Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet, sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL); if (!Success) printf(" *** ERROR *** String Descriptor 0 not returned, ErrorCode = %d\n", GetLastError()); LanguageID = Packet.Data[2] + (Packet.Data[3] << 8); } memset(&Packet, 0, sizeof(Packet)); Packet.ConnectionIndex = PortIndex; Packet.SetupPacket.bmRequest = 0x80; Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR; Packet.SetupPacket.wValue[1] = USB_STRING_DESCRIPTOR_TYPE; Packet.SetupPacket.wValue[0] = Index; Packet.SetupPacket.wIndex[0] = LanguageID & 0xFF; Packet.SetupPacket.wIndex[1] = (LanguageID >> 8) & 0xFF; Packet.SetupPacket.wLength[0] = 255; Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet, sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL); if (!Success) printf(" *** ERROR *** String Descriptor %d not returned. ErrorCode = %d\n", Index, GetLastError()); printf(" = %ws", &Packet.Data[2]); return LanguageID; } USHORT DisplayDeviceDescriptor (HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID, PUCHAR BufferPtr) { if (DEBUG) printf("In DisplayDeviceDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x\n", HubHandle, PortIndex, LanguageID); UCHAR LowByte; printf("Device Descriptor"); BufferPtr--; // Backup pointer to prepare for pre-increment printf("\n bLength %2.2x", *++BufferPtr); printf("\n bDescriptorType %2.2x", *++BufferPtr); LowByte = *++BufferPtr; printf("\n bcdUSB %4.4x", LowByte + (*++BufferPtr << 8)); printf("\n bDeviceClass %2.2x", *++BufferPtr); printf("\n bDeviceSubClass %2.2x", *++BufferPtr); printf("\n bDeviceProtocol %2.2x", *++BufferPtr); printf("\n bMaxEP0Size %2.2x", *++BufferPtr); LowByte = *++BufferPtr; printf("\n wVendorID %4.4x", LowByte + (*++BufferPtr << 8)); LowByte = *++BufferPtr; printf("\n wProductID %4.4x", LowByte + (*++BufferPtr << 8)); LowByte = *++BufferPtr; printf("\n wDeviceID %4.4x", LowByte + (*++BufferPtr << 8)); printf("\n iManufacturer %2.2x", *++BufferPtr); if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr); printf("\n iProduct %2.2x", *++BufferPtr); if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr); printf("\n iSerialNumber %2.2x", *++BufferPtr); if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr); printf("\n bNumConfigurations %2.2x\n", *++BufferPtr); return LanguageID; } USHORT DisplayConfigurationDescriptor(HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID) { if (DEBUG) printf("In DisplayConfigurationDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x\n", HubHandle, PortIndex, LanguageID); DWORD BytesReturned; bool Success; UCHAR LowByte; DESCRIPTOR_REQUEST Packet; int i; printf("\nConfiguration Descriptor"); // First need to get the configuration descriptor memset(&Packet, 0, sizeof(Packet)); Packet.ConnectionIndex = PortIndex; Packet.SetupPacket.bmRequest = 0x80; Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR; Packet.SetupPacket.wValue[1] = USB_CONFIGURATION_DESCRIPTOR_TYPE; Packet.SetupPacket.wLength[1] = 1; // Using a 2K buffer Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet, sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL); if (!Success) printf(" *** ERROR *** Configuration Descriptor not returned. ErrorCode = %d\n", GetLastError()); PUCHAR BufferPtr = &Packet.Data[0]; UCHAR Length = *BufferPtr; while (Length != 0) { UCHAR Type = *++BufferPtr; switch (Type) { case 2: printf("\n bLength %2.2x", Length); printf("\n bDescriptorType %2.2x = Configuration Header", Type); LowByte = *++BufferPtr; printf("\n wTotalLength %4.4x", LowByte + (*++BufferPtr << 8)); printf("\n bNumInterfaces %2.2x", *++BufferPtr); printf("\n bConfigValue %2.2x", *++BufferPtr); printf("\n iConfiguration %2.2x", *++BufferPtr); if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr); printf("\n bmAttributes %2.2x", *++BufferPtr); LowByte = *++BufferPtr; printf("\n bMaxPower %2.2x = %d mA", LowByte, (LowByte << 1)); break; case 4: printf("\n bLength %2.2x", Length); printf("\n bDescriptorType %2.2x = Interface Descriptor", Type); printf("\n bInterfaceNum %2.2x", *++BufferPtr); printf("\n bAlternateSetting %2.2x", *++BufferPtr); printf("\n bNumEndpoints %2.2x", *++BufferPtr); LowByte = *++BufferPtr; if ((LowByte > 9) & (LowByte < 255)) LowByte = 11; if (LowByte == 255) LowByte = 10; printf("\n bInterfaceClass %2.2x = %s", *BufferPtr, ClassName[LowByte]); printf("\n bSubClass %2.2x", *++BufferPtr); printf("\n bProtocol %2.2x", *++BufferPtr); printf("\n iInterface %2.2x", *++BufferPtr); if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr); break; case 5: printf("\n bLength %2.2x", Length); printf("\n bDescriptorType %2.2x = Endpoint Descriptor", Type); printf("\n bEndpointAddress %2.2x", *++BufferPtr); printf("\n bmAttributes %2.2x", *++BufferPtr); LowByte = *++BufferPtr; printf("\n wMaxPacketSize %4.4x", LowByte + (*++BufferPtr << 8)); printf("\n bInterval %2.2x", *++BufferPtr); break; case 0x21: printf("\n bLength %2.2x", Length); printf("\n bDescriptorType %2.2x = HID Descriptor", Type); LowByte = *++BufferPtr; printf("\n wHIDversion %4.4x", LowByte + (*++BufferPtr << 8)); printf("\n bCountryCode %2.2x", *++BufferPtr); printf("\n bHIDDescriptorCount %2.2x", *++BufferPtr); printf("\n bHIDReportType %2.2x", *++BufferPtr); LowByte = *++BufferPtr; printf("\n wHIDReportLength %4.4x", LowByte + (*++BufferPtr << 8)); break; default: printf("\nUnknown descriptor with Length = %2.2xH and Type = %2.2xH", Length, Type); BufferPtr-=2; // Back up to start of descriptor for (i = 0; i < Length; i++) { if ((i % 16) == 0) printf("\n"); printf("%2.2x ", *++BufferPtr); } break; } Length = *++BufferPtr; printf("\n"); } return LanguageID; } void GetPortData(HANDLE HubHandle, UCHAR PortCount, int HubDepth) { if (DEBUG) printf("In GetPortData with HubHandle = %x, PortCount = %x, HubDepth = %x\n", HubHandle, PortCount, HubDepth); DWORD BytesReturned; bool Success; int i; ULONG PortIndex; USHORT LanguageID; UCHAR ThisDevice, PortStatus; char ConnectedHubName[256] = "\\\\.\\"; HANDLE ConnectedHubHandle; NODE_INFORMATION NodeInformation; NODE_CONNECTION_INFORMATION ConnectionInformation; struct {ULONG ConnectionIndex; ULONG ActualLength; WCHAR Name[256];} ConnectedHub; // Iterate over the ports to discover what is connected to each one for (PortIndex = 1; PortIndex < (ULONG)PortCount + 1; PortIndex++) { LanguageID = 0; // Reset for each port ConnectionInformation.ConnectionIndex = PortIndex; Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION, &ConnectionInformation, sizeof(ConnectionInformation), &ConnectionInformation, sizeof(ConnectionInformation), &BytesReturned, NULL); if (!Success) printf(" *** ERROR *** Node connection information not returned\n"); PortStatus = ConnectionInformation.ConnectionStatus[0]; // Save some typing! ThisDevice = (PortStatus == DeviceConnected) ? ConnectionInformation.DeviceAddress[0] : 0; // Create an indented display so that hubs and their connections are more easily seen // First the common header // printf("%2.2x", ThisDevice); for (i=0; i



번호 제목 날짜 조회 수
공지 [TIP] 자작 운영체제 AiRPOS 소스를 공개합니다. 2026.07.04 10203
공지 [TIP] PYTHON 에서 "UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence" 오류 날때... [52] 2016.02.20 263992
공지 [펌] ARM GCC Inline Assembler Cookbook 2006.08.18 56069
공지 [TIP] R에서 페이스북 페이지 정보 크롤링 하기 [6] 2017.02.11 38197
110 [TIP] 치명적 오류 발생시 오류창 안뜨게 하기? 2008.02.25 1161
» [TIP] USB 장치를 열거하는 코드 2008.02.03 14090
108 [TIP] Windows 에서 장치 연결/제거 관리 코드 2007.12.21 1094
107 [TIP] Source Insight 용 ARM Assembler 파서 2007.12.18 1130
106 [펌] precompiled header 설정 관련 2007.08.07 983
105 [LINK]Linux등의 환경에서 동적,정적 라이브러리 사용관련 2006.09.18 1103
104 [펌] 클래스 맴버함수를 콜백함수로 사용하기 2006.08.25 2924
103 [펌]키입력을 바로 처리하는 linux용 코드 2006.08.23 1062
102 [펌] ARM GCC Inline Assembler Cookbook 2006.08.18 56069
101 [펌]혼합 언어 프로그래밍과 외부 연결(linkage) 2006.08.14 1518
100 [TIP] 소켓을 이용하여 HTTP프로토콜 사용하는 C 코드 예제 2006.05.25 2651
99 [TIP] 작업관리자의 응용프로그램탭에 나타나는 윈도우 이름 감추기 2005.12.18 1510
98 [TIP] 각종 API 후킹 혹은 어플리케이션 후킹 방법 모음 2005.07.30 932
97 [참고] PE operations on non PE file. error- 2005.07.13 551
96 [TIP] CGI 파싱 C소스(CGI POST Method Example: C Code ) 2005.06.09 1646
95 [TIP] WMI이용 무선랜카드 신호강도 얻기 2005.05.23 675
94 [TIP] WMI를 이용한 하드웨어 정보 얻기 2005.05.23 653
93 [TIP] WEB 페이지 스타일의 어플리케이션 만들기 2005.05.02 1232
92 [TIP] CMOS 패스워드 초기화 코드 2005.05.02 1083
91 [TIP] Mail Relay Check 펄 스크립트 2005.05.01 654