⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displayusb.cpp

📁 intel关于读写usb设备的相关设备
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			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<HubDepth; i++) printf("+1");
		printf("        Port[%d] = ", PortIndex);

//   Now the connection specific information
		if (PortStatus != DeviceConnected) printf("%s\n", ConnectionStatus[PortStatus]);
		else {	// have a device or a hub connected to this port
			if (!ConnectionInformation.DeviceIsHub) {

//   There is an I/O device connected.  Print out it's descriptors
//	 Note that many current devices do not respond correctly if ConfigID != 0.  So only request the first configuration
				printf("I/O device connected\n");
				LanguageID = DisplayDeviceDescriptor(HubHandle, PortIndex, LanguageID, &ConnectionInformation.DeviceDescriptor.bLength);
				LanguageID = DisplayConfigurationDescriptor(HubHandle, PortIndex, LanguageID);
				}
			else {

//   There is a hub connected and we need to iterate over it's ports
				printf("Hub connected\n");

//   Get the system name of the connected hub so that we can make a connection to it
				ConnectedHub.ConnectionIndex = PortIndex;
				Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_NODE_CONNECTION_NAME, &ConnectedHub,
					sizeof(ConnectedHub), &ConnectedHub, sizeof(ConnectedHub), &BytesReturned, NULL);
				if (!Success) printf(" *** ERROR *** Node connection name not returned\n");
				WideCharToMultiByte(CP_ACP, 0, &ConnectedHub.Name[0], (ConnectedHub.ActualLength)/2, &ConnectedHubName[4], 252, NULL, NULL);
				ConnectedHubHandle = CreateFile(ConnectedHubName, GENERIC_WRITE, FILE_SHARE_WRITE, &SA, OPEN_EXISTING, 0, NULL); 
				if (DEBUG) printf("Connected hub handle %d created\n", ConnectedHubHandle);

//   Connected hub is open.  Collect the node information
				Success = DeviceIoControl(ConnectedHubHandle, IOCTL_USB_GET_NODE_INFORMATION, &NodeInformation,
					sizeof(NodeInformation), &NodeInformation, sizeof(NodeInformation), &BytesReturned, NULL);
				if (!Success) printf(" *** ERROR *** Node information not returned\n");
				GetPortData(ConnectedHubHandle, NodeInformation.HubDescriptor.bNumberOfPorts, HubDepth+1);
				if (DEBUG) printf("Closing connected hub handle %d\n", ConnectedHubHandle);
				CloseHandle(ConnectedHubHandle);
				};
			};
		};
	}

DWORD EnumerateHostController(HANDLE HostControllerHandle) {
	DWORD BytesReturned;
	bool Success;
	struct {ULONG Length; WCHAR Name[256];} UnicodeName;
	char RootHubName[256] = "\\\\.\\";
	HANDLE RootHubHandle;
	NODE_INFORMATION NodeInformation;
	
// First get the system name of the host controller for display
	Success = DeviceIoControl(HostControllerHandle, IOCTL_GET_HCD_DRIVERKEY_NAME, &UnicodeName, sizeof(UnicodeName),
		&UnicodeName, sizeof(UnicodeName), &BytesReturned, NULL);
	if (!Success) return GetLastError(); 
	printf("System name is %ws\n", &UnicodeName.Name[0]);

// Now get the system name of it's root hub for interrogation
	Success = DeviceIoControl(HostControllerHandle, IOCTL_USB_GET_ROOT_HUB_NAME, &UnicodeName, sizeof(UnicodeName),
		&UnicodeName, sizeof(UnicodeName), &BytesReturned, NULL);
	if (DEBUG) printf(" and root hub name is %ws\n", &UnicodeName.Name[0]);

// Now open the root hub.  Need to construct a char name from "\\.\" + UnicodeName
	WideCharToMultiByte(CP_ACP, 0, &UnicodeName.Name[0], (UnicodeName.Length)/2, &RootHubName[4], 252, NULL, NULL);
	RootHubHandle = CreateFile(RootHubName, GENERIC_WRITE, FILE_SHARE_WRITE, &SA, OPEN_EXISTING, 0, NULL);  
	if (RootHubHandle == INVALID_HANDLE_VALUE) return GetLastError();
	if (DEBUG) printf("Root hub handle %d created\n", RootHubHandle);
	
// Root hub is open. Collect the node information
	Success = DeviceIoControl(RootHubHandle, IOCTL_USB_GET_NODE_INFORMATION, &NodeInformation,
		sizeof(NodeInformation), &NodeInformation, sizeof(NodeInformation), &BytesReturned, NULL);
	if (!Success) return GetLastError();

// Can now iterate over the ports
	GetPortData(RootHubHandle, NodeInformation.HubDescriptor.bNumberOfPorts, 0);
	if (DEBUG) printf("Closing root hub handle %d\n", RootHubHandle);
	CloseHandle(RootHubHandle);
	return 0;
	}

int main(int argc, char* argv[]) {
	HANDLE HostControllerHandle;
	char HostControllerName[] = "\\\\.\\HCD0";
	int i;
	DWORD ErrorCode = 0;
	FILE *stream;
// Say Hello to the user and setup the security attributes for Win2000
	printf("USB Design By Example: Display currently attached USB Devices\n\n");
	DEBUG = !true;
	SA.nLength = sizeof(SECURITY_ATTRIBUTES); 
	SA.lpSecurityDescriptor = NULL; 
	SA.bInheritHandle = false; 

// Search for USB host controllers.  Product such as Lucents QuadraBus can make this number large
// Redirect the output text to a file for later review
 	printf("Re-directing console output to 'DisplayUSB.txt'.  Please wait\n\n");
	stream = freopen("C:\\My Documents\\DisplayUSB.txt", "w", stdout);
	if (stream == NULL) {
		printf("Cannot redirect output\n");
		return -1;
	}
	printf("USB Design By Example: Display currently attached USB Devices\n");
 
	for (i=0; i<10; i++) {
		HostControllerName[7] = i + '0';
		HostControllerHandle = CreateFile(HostControllerName, GENERIC_WRITE, FILE_SHARE_WRITE, &SA, OPEN_EXISTING, 0, NULL);
		if (DEBUG) printf("Host controller handle %d created\n", HostControllerHandle);
		if ((HostControllerHandle != INVALID_HANDLE_VALUE) & (ErrorCode == 0)) {
			printf("\nHost Controller %s found. ", HostControllerName);
			ErrorCode = EnumerateHostController(HostControllerHandle);
			if (DEBUG) printf("Closing host controller handle %d\n", HostControllerHandle);
			CloseHandle(HostControllerHandle);
			}
		}
 	fclose(stream);
 	system("notepad C:\\My Documents\\DisplayUSB.txt");
	return ErrorCode;
	}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -