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

📄 search_usb.cpp

📁 开发usb固件时
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Search_USB.cpp: implementation of the Search_USB class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DialogUSB.h"
#include "Search_USB.h"

//#include "setupapi.h"		// Must link in setupapi.lib

/*
//添加:头文件
extern "C" {
//断言使用外部 C 库
// Declare the C libraries used
#include "setupapi.h"		// Must link in setupapi.lib
#include "hidsdi.h"			// Must link in hid.lib//需要 hid.dll
}
*/
//#include "objbase.h"
//#include "winioctl.h"
//#include "usbioctl.h"

//#include <afxcoll.h>
//#include "stdafx.h"
//#include "stdio.h"
//#include "stdlib.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
//静态常量
//////////////////////////////////////////////////////////////////////
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"
	};
//Bit 1..0 :传送类型,00=,01=,10=,11=所有其它的位都保留。
const char ConnectionType[] [30] = {
		"控制传送",
		"同步传送",
		"批量传送",
		"中断传送",
		"未知传送类型",

	};

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Search_USB::Search_USB()
{

}

Search_USB::~Search_USB()
{

}

//////////////////////////////////////////////////////////////////////////////////
//查询 USB 设备
DWORD Search_USB::Search(CStringArray* pArrayUSB)
{
	CString sBuf;
	HANDLE HostControllerHandle;
	char HostControllerName[] = "\\\\.\\HCD0";
	int i;
	DWORD ErrorCode = 0;
// Say Hello to the user and setup the security attributes for Win2000
	sBuf= "当前已连接的 USB 设备:" ;
	pArrayUSB->Add(sBuf);
	DEBUG = false;
	SA.nLength = sizeof(SECURITY_ATTRIBUTES); 
	SA.lpSecurityDescriptor = NULL; 
	SA.bInheritHandle = false; 

	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)
		{
			sBuf.Format("Host controller handle %d created\n", HostControllerHandle);
			pArrayUSB->Add(sBuf);
		}
		if ((HostControllerHandle != INVALID_HANDLE_VALUE) & (ErrorCode == 0))
		{
			sBuf.Format("发现主控制器:%s ", HostControllerName);
			pArrayUSB->Add("");
			pArrayUSB->Add(sBuf);
			//枚举主机控制器
			ErrorCode = Enumerate_Host_Controller(HostControllerHandle, pArrayUSB);
			if (DEBUG)
			{
				sBuf.Format("Closing host controller handle %d\n", HostControllerHandle);
				pArrayUSB->Add(sBuf);
			}
				CloseHandle(HostControllerHandle);
		}
	}

	// 释放资源
	pArrayUSB->FreeExtra();
	return ErrorCode;

}
			
//枚举主机控制器
DWORD Search_USB::Enumerate_Host_Controller(HANDLE hHostController, CStringArray* pArrayUSB)
{
	CString sBuf;
	DWORD BytesReturned;
	BOOL Success;
	int aaa=_MAX_DRIVE;
	struct {ULONG Length; WCHAR Name[_MAX_PATH];} UnicodeName;
	char RootHubName[_MAX_PATH] = "\\\\.\\";
	HANDLE RootHubHandle;
	NODE_INFORMATION NodeInformation;
	
	//首先取得主控器名
	Success = DeviceIoControl(hHostController,
		IOCTL_GET_HCD_DRIVERKEY_NAME,
		&UnicodeName,
		sizeof(UnicodeName),
		&UnicodeName, 
		sizeof(UnicodeName),
		&BytesReturned,
		NULL);
	if (!Success) return GetLastError(); 

	sBuf= _T(UnicodeName.Name);
	sBuf= "主控器名:" + sBuf;
	pArrayUSB->Add(sBuf);

	//获取根 HUB 系统名
	Success = DeviceIoControl(hHostController,
		IOCTL_USB_GET_ROOT_HUB_NAME,
		&UnicodeName,
		sizeof(UnicodeName),
		&UnicodeName,
		sizeof(UnicodeName),
		&BytesReturned,
		NULL);

	sBuf= _T(UnicodeName.Name);
	sBuf= "主HUB 名:" + sBuf;
	pArrayUSB->Add(sBuf);

	// 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);
	//打开根 HUB
	RootHubHandle = CreateFile(RootHubName,
		GENERIC_WRITE,
		FILE_SHARE_WRITE,
		&SA,
		OPEN_EXISTING,
		0,
		NULL);  
	if (RootHubHandle == INVALID_HANDLE_VALUE) return GetLastError();
	if (DEBUG)
	{
		sBuf.Format("Root hub handle %d created\n", RootHubHandle);
		pArrayUSB->Add(sBuf);
	}
	
	//获取根 HUB 的各个节点信息
	Success = DeviceIoControl(RootHubHandle,
		IOCTL_USB_GET_NODE_INFORMATION,
		&NodeInformation,
		sizeof(NodeInformation),
		&NodeInformation,
		sizeof(NodeInformation),
		&BytesReturned,
		NULL);
	if (!Success) return GetLastError();

	//重新获取根 HUB 口数据
	Get_PortData(RootHubHandle, NodeInformation.HubDescriptor.bNumberOfPorts, 0, pArrayUSB);
	if (DEBUG)
	{
		sBuf.Format("Closing root hub handle %d\n", RootHubHandle);
		pArrayUSB->Add(sBuf);
	}
	//关闭根 HUB
	CloseHandle(RootHubHandle);
	return 0;

}

//获取根 HUB 各个节点的连接状况
void Search_USB::Get_PortData(HANDLE HubHandle, UCHAR PortCount, int HubDepth, CStringArray* pArrayUSB)
{
	CString sBuf, sTemp;
	if (DEBUG)
	{
		sBuf.Format("In GetPortData with HubHandle = %x, PortCount = %x, HubDepth = %x\n", HubHandle, PortCount, HubDepth);
		pArrayUSB->Add(sBuf);
	}
	DWORD BytesReturned;
	BOOL Success;
	int i;
	ULONG PortIndex;
	USHORT LanguageID;
	UCHAR ThisDevice, PortStatus;
	char ConnectedHubName[_MAX_PATH] = "\\\\.\\";
	HANDLE ConnectedHubHandle;
	NODE_INFORMATION NodeInformation;
	NODE_CONNECTION_INFORMATION ConnectionInformation;
	struct {ULONG ConnectionIndex; ULONG ActualLength; WCHAR Name[_MAX_PATH];} ConnectedHub;

	//重新循环查找 HUB 节点的连接
	for (PortIndex = 1; PortIndex < (ULONG)PortCount + 1; PortIndex++)
	{
		LanguageID = 0;  // Reset for each port
		ConnectionInformation.ConnectionIndex = PortIndex;

		// 查询该 HUB 连接状况
		Success = DeviceIoControl(HubHandle,
			IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
			&ConnectionInformation,
			sizeof(ConnectionInformation),
			&ConnectionInformation,
			sizeof(ConnectionInformation),
			&BytesReturned,
			NULL);
		if (!Success)
		{
			sBuf.Format(" *** ERROR *** Node connection information not returned\n");
			pArrayUSB->Add(sBuf);
		}
	
		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
		sBuf.Format("    %2.2x", ThisDevice);
		for (i=0; i< HubDepth; i++)
		{
			sBuf += "++1";
			pArrayUSB->Add(sBuf);
		}
		// 该 HUB 的节点连接状况
		sBuf.Format("    Port[%d] = ", PortIndex);

		//该 HUB 节点的连接特性
		if (PortStatus != DeviceConnected)
		{
			// 该节点没有连接
			sTemp.Format("%s", ConnectionStatus[PortStatus]);
			sBuf += sTemp;
			pArrayUSB->Add(sBuf);
		}
		else
		{	// 该节点连接了一个设备或 HUB
			if (!ConnectionInformation.DeviceIsHub)
			{
				// 该节点连接了一个 USB 设备

//   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
				sBuf += "I/O device connected\n";
				pArrayUSB->Add(sBuf);
				pArrayUSB->Add("");

				// 将有关该设备的标识显示出来
				// 提取设备描述,并保存结果
				LanguageID = Get_DeviceDescriptor(HubHandle, PortIndex, LanguageID, &ConnectionInformation.DeviceDescriptor.bLength, pArrayUSB);
				// 提取配置描述,并保存结果
				LanguageID = Get_ConfigurationDescriptor(HubHandle, PortIndex, LanguageID, pArrayUSB);
			}else
			{
				// 该节点连接了一个 HUB ,需要查找下一级节点
				sBuf += "Hub connected\n";
				pArrayUSB->Add(sBuf);

//   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){
					sBuf.Format(" *** ERROR *** Node connection name not returned\n");
					pArrayUSB->Add(sBuf);
				}
				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)
				{
					sBuf.Format("Connected hub handle %d created", ConnectedHubHandle);
					pArrayUSB->Add(sBuf);
				}

				//是打开的,收集节点信息
				Success = DeviceIoControl(ConnectedHubHandle,
					IOCTL_USB_GET_NODE_INFORMATION,
					&NodeInformation,
					sizeof(NodeInformation),
					&NodeInformation,
					sizeof(NodeInformation),
					&BytesReturned,
					NULL);
				if (!Success)
				{
					sBuf.Format(" *** ERROR *** Node information not returned");
					pArrayUSB->Add(sBuf);
				}
				Get_PortData(ConnectedHubHandle, NodeInformation.HubDescriptor.bNumberOfPorts, HubDepth+1, pArrayUSB);
				if (DEBUG)
				{
					sBuf.Format("Closing connected hub handle %d", ConnectedHubHandle);
					pArrayUSB->Add(sBuf);
				}
				CloseHandle(ConnectedHubHandle);
				};
			};
		};

}

//获取配置描述
USHORT Search_USB::Get_ConfigurationDescriptor(HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID, CStringArray* pArrayUSB)
{
	CString sBuf;
	CString sBuf1;
	USHORT uData;
	BYTE b;
	DWORD BytesReturned;
	BOOL Success;
	UCHAR LowByte;
	DESCRIPTOR_REQUEST Packet;
	int i;
	if (DEBUG)
	{
		sBuf.Format("In DisplayConfigurationDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x\n", HubHandle, PortIndex, LanguageID);
		pArrayUSB->Add(sBuf);
	}

⌨️ 快捷键说明

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