메뉴 건너뛰기

프로그래밍


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

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

깔끔히 정리된 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 John.Hyde@intel.com #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] PYTHON 에서 "UnicodeDecodeError: 'cp949' codec can't decode byte 0xe2 in position 6987: illegal multibyte sequence" 오류 날때... [48] 파이팅건맨 2016.02.20 108619
공지 [TIP] R에서 페이스북 페이지 정보 크롤링 하기 [6] 파이팅건맨 2017.02.11 15769
공지 [TIP] TensorFlow를 윈도우에서 사용하기 (A way to use TensorFlow on Windows) [3] 파이팅건맨 2016.04.16 14700
125 [TIP] 자작 커널을 위한 인터럽트 벡터 수정 (EZ-2410보드 대상) 파이팅건맨 2013.05.04 649
124 위치추적 [1] 우람사랑 2013.04.06 1967
123 [TIP] MySQL - NaviCat 사용시 에러 확인방법 파이팅건맨 2010.02.23 1175
122 [TIP] 설치된 코덱 리스트 얻어오기 파이팅건맨 2010.02.18 921
121 [TIP] VFW 에서 영상 압축을 위한 코덱을 코드상에서 선택하여 실시간 저장하는 방법입니다. 파이팅건맨 2010.02.04 1099
120 [TIP] Visual Studio App Wizard 만들기 관련 파이팅건맨 2009.12.17 993
119 [TIP] C# 에서 Win32 DLL 을 사용하기 위한 방법 파이팅건맨 2009.07.16 1149
118 [TIP] WMI 이용, 사용가능한 모뎀 포트 알아내기 파이팅건맨 2009.07.05 911
117 [TIP] Excel Ole 를 사용시 Hidden 열 혹은 행인지 알아내는 방법 파이팅건맨 2009.05.17 705
116 [TIP] Win32 API를 사용하여 응용 프로그램 열거하기 파이팅건맨 2009.01.15 705
115 [TIP] 홈페이지 자동 클릭하기 소스 파이팅건맨 2009.10.25 1853
114 [TIP] (MSDN) VS2005 에서 BHO 만드는 방법 링크 파이팅건맨 2009.01.06 614
113 [TIP] Windows 에서 COM10 이상을 여는 방법 [2] 파이팅건맨 2008.05.26 977
112 [TIP] 인터넷으로 문자메시지 (SMS) 보내기 파이팅건맨 2008.03.23 3042
111 [TIP] 동적 DLL이 가진 Dialog를 Child Window로 띄우기 파이팅건맨 2008.03.16 1011
110 [TIP] 치명적 오류 발생시 오류창 안뜨게 하기? 파이팅건맨 2008.02.25 776
» [TIP] USB 장치를 열거하는 코드 파이팅건맨 2008.02.03 651
108 [TIP] Windows 에서 장치 연결/제거 관리 코드 파이팅건맨 2007.12.21 712
107 [TIP] Source Insight 용 ARM Assembler 파서 파이팅건맨 2007.12.18 794
106 [펌] precompiled header 설정 관련 파이팅건맨 2007.08.07 650




위로