📄 mfc_usbviewdlg.cpp
字号:
{
OOPS();
return NULL;
}
// 现在请求整个的结构描述符使用对支撑整个的描述符是够大的一个动态地分派的被按规定尺寸制作的缓冲
nBytes = sizeof(USB_DESCRIPTOR_REQUEST) + configDesc->wTotalLength;
//nBytes = sizeof(configDescReqBuf);
configDescReq = (PUSB_DESCRIPTOR_REQUEST)ALLOC(nBytes);
if (configDescReq == NULL)
{
OOPS();
return NULL;
}
configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
// 指出端口从哪一描述符将会被请求
configDescReq->ConnectionIndex = ConnectionIndex;
// SBHUB 使用 URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE 处理这 IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 请求.
// USBD 将会自动地设定这些领域初值:
// bmRequest = 0x80
// bRequest = 0x06
// 我们必须初始化这些:
// wValue = 描述符类型 (高) 与描述符索引 (低字节)
// wIndex = 0 (或者语言ID字符串描述符)
// wLength = 描述符缓冲区的长度
configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8) | DescriptorIndex;
configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
// 现在发出获得描述符请求.
success = DeviceIoControl(hHubDevice,IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,configDescReq,
nBytes,configDescReq,nBytes,&nBytesReturned,NULL);
if (!success)
{
OOPS();
FREE(configDescReq);
return NULL;
}
if (nBytes != nBytesReturned)
{
OOPS();
FREE(configDescReq);
return NULL;
}
//if (configDesc->wTotalLength != (nBytes - sizeof(USB_DESCRIPTOR_REQUEST)))
//{
// OOPS();
// FREE(configDescReq);
// return NULL;
//}
return configDescReq;
}
BOOL CMFC_USBViewDlg::AreThereStringDescriptors(PUSB_DEVICE_DESCRIPTOR DeviceDesc, PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc)
{
PUCHAR descEnd;
PUSB_COMMON_DESCRIPTOR commonDesc;
// 检查设备描述符字符串
if (DeviceDesc->iManufacturer || DeviceDesc->iProduct || DeviceDesc->iSerialNumber)
{
return TRUE;
}
// 检查配置和接口描述符字符串
descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;
commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;
while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd && (PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
{
switch (commonDesc->bDescriptorType)
{
case USB_CONFIGURATION_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
{
OOPS();
break;
}
if (((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc)->iConfiguration)
{
return TRUE;
}
commonDesc += commonDesc->bLength;
continue;
case USB_INTERFACE_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR) &&
commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR2))
{
OOPS();
break;
}
if (((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->iInterface)
{
return TRUE;
}
commonDesc += commonDesc->bLength;
continue;
default:
commonDesc += commonDesc->bLength;
continue;
}
break;
}
return FALSE;
}
PSTRING_DESCRIPTOR_NODE CMFC_USBViewDlg::GetAllStringDescriptors(HANDLE hHubDevice, ULONG ConnectionIndex, PUSB_DEVICE_DESCRIPTOR DeviceDesc, PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc)
{
PSTRING_DESCRIPTOR_NODE supportedLanguagesString;
PSTRING_DESCRIPTOR_NODE stringDescNodeTail;
ULONG numLanguageIDs;
USHORT *languageIDs;
PUCHAR descEnd;
PUSB_COMMON_DESCRIPTOR commonDesc;
// 得到支援的语言ID的排列, 在字符串描述符中被退还
supportedLanguagesString = GetStringDescriptor(hHubDevice,ConnectionIndex,0,0);
if (supportedLanguagesString == NULL)
{
return NULL;
}
numLanguageIDs = (supportedLanguagesString->StringDescriptor->bLength - 2) / 2;
languageIDs = &supportedLanguagesString->StringDescriptor->bString[0];
stringDescNodeTail = supportedLanguagesString;
// 获得设备描述符字符串
if (DeviceDesc->iManufacturer)
{
stringDescNodeTail = GetStringDescriptors(hHubDevice,ConnectionIndex,DeviceDesc->iManufacturer,
numLanguageIDs,languageIDs,stringDescNodeTail);
}
if (DeviceDesc->iProduct)
{
stringDescNodeTail = GetStringDescriptors(hHubDevice,ConnectionIndex,DeviceDesc->iProduct,
numLanguageIDs,languageIDs,stringDescNodeTail);
}
if (DeviceDesc->iSerialNumber)
{
stringDescNodeTail = GetStringDescriptors(hHubDevice,ConnectionIndex,DeviceDesc->iSerialNumber,
numLanguageIDs,languageIDs,stringDescNodeTail);
}
// 获得配置和接口描述符字符串
descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;
commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;
while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd && (PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
{
switch (commonDesc->bDescriptorType)
{
case USB_CONFIGURATION_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
{
OOPS();
break;
}
if (((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc)->iConfiguration)
{
stringDescNodeTail = GetStringDescriptors(hHubDevice,ConnectionIndex,
((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc)->iConfiguration,
numLanguageIDs,languageIDs,stringDescNodeTail);
}
commonDesc += commonDesc->bLength;
continue;
case USB_INTERFACE_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR) && commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR2))
{
OOPS();
break;
}
if (((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->iInterface)
{
stringDescNodeTail = GetStringDescriptors(hHubDevice,ConnectionIndex,
((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->iInterface,
numLanguageIDs,languageIDs,stringDescNodeTail);
}
commonDesc += commonDesc->bLength;
continue;
default:
commonDesc += commonDesc->bLength;
continue;
}
break;
}
return supportedLanguagesString;
}
PSTRING_DESCRIPTOR_NODE CMFC_USBViewDlg::GetStringDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex, USHORT LanguageID)
{
BOOL success;
ULONG nBytes;
ULONG nBytesReturned;
UCHAR stringDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST) + MAXIMUM_USB_STRING_LENGTH];
PUSB_DESCRIPTOR_REQUEST stringDescReq;
PUSB_STRING_DESCRIPTOR stringDesc;
PSTRING_DESCRIPTOR_NODE stringDescNode;
nBytes = sizeof(stringDescReqBuf);
stringDescReq = (PUSB_DESCRIPTOR_REQUEST)stringDescReqBuf;
stringDesc = (PUSB_STRING_DESCRIPTOR)(stringDescReq+1);
// 将整个结构初始化为0
memset(stringDescReq, 0, nBytes);
// 指出端口从哪一描述符将会被请求
stringDescReq->ConnectionIndex = ConnectionIndex;
// SBHUB 使用 URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE 处理这 IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 请求.
// USBD 将会自动地设定这些领域初值:
// bmRequest = 0x80
// bRequest = 0x06
// 我们必须初始化这些:
// wValue = 描述符类型 (高) 与描述符索引 (低字节)
// wIndex = 0 (或者语言ID字符串描述符)
// wLength = 描述符缓冲区的长度
stringDescReq->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | DescriptorIndex;
stringDescReq->SetupPacket.wIndex = LanguageID;
stringDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
// 现在发出获得描述符请求.
success = DeviceIoControl(hHubDevice,IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,stringDescReq,
nBytes,stringDescReq,nBytes,&nBytesReturned,NULL);
// 在这返回的获得描述符请求上做一些健全的检查.
if (!success)
{
OOPS();
return NULL;
}
if (nBytesReturned < 2)
{
OOPS();
return NULL;
}
if (stringDesc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE)
{
OOPS();
return NULL;
}
if (stringDesc->bLength != nBytesReturned - sizeof(USB_DESCRIPTOR_REQUEST))
{
OOPS();
return NULL;
}
if (stringDesc->bLength % 2 != 0)
{
OOPS();
return NULL;
}
// 为字符串描述符节点分配一些(零填充)空间并且拷贝字符串描述符给它.
stringDescNode = (PSTRING_DESCRIPTOR_NODE)ALLOC(sizeof(STRING_DESCRIPTOR_NODE) +stringDesc->bLength);
if (stringDescNode == NULL)
{
OOPS();
return NULL;
}
stringDescNode->DescriptorIndex = DescriptorIndex;
stringDescNode->LanguageID = LanguageID;
memcpy(stringDescNode->StringDescriptor,stringDesc,stringDesc->bLength);
return stringDescNode;
}
PSTRING_DESCRIPTOR_NODE CMFC_USBViewDlg::GetStringDescriptors(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex, ULONG NumLanguageIDs, USHORT *LanguageIDs, PSTRING_DESCRIPTOR_NODE StringDescNodeTail)
{
ULONG i;
for (i=0; i<NumLanguageIDs; i++)
{
StringDescNodeTail->Next = GetStringDescriptor(hHubDevice,ConnectionIndex,DescriptorIndex,*LanguageIDs);
if (StringDescNodeTail->Next)
{
StringDescNodeTail = StringDescNodeTail->Next;
}
LanguageIDs++;
}
return StringDescNodeTail;
}
PCHAR CMFC_USBViewDlg::GetExternalHubName(HANDLE Hub, ULONG ConnectionIndex)
{
BOOL success;
ULONG nBytes;
USB_NODE_CONNECTION_NAME extHubName;
PUSB_NODE_CONNECTION_NAME extHubNameW;
PCHAR extHubNameA;
extHubNameW = NULL;
extHubNameA = NULL;
// 获得被附上到被叙述的端口外部HUB的名字长度.
extHubName.ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,IOCTL_USB_GET_NODE_CONNECTION_NAME,&extHubName,
sizeof(extHubName),&extHubName,sizeof(extHubName),&nBytes,NULL);
if (!success)
{
OOPS();
goto GetExternalHubNameError;
}
// 分配空间给外部HUB名
nBytes = extHubName.ActualLength;
if (nBytes <= sizeof(extHubName))
{
OOPS();
goto GetExternalHubNameError;
}
extHubNameW = (_USB_NODE_CONNECTION_NAME *)ALLOC(nBytes);
if (extHubNameW == NULL)
{
OOPS();
goto GetExternalHubNameError;
}
// 让外部HUB的名字被附上到被叙述的端口
extHubNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,IOCTL_USB_GET_NODE_CONNECTION_NAME,extHubNameW,nBytes,
extHubNameW,nBytes,&nBytes,NULL);
if (!success)
{
OOPS();
goto GetExternalHubNameError;
}
// 转换外部HUB名
extHubNameA = WideStrToMultiStr(extHubNameW->NodeName);
// 完成了全部, 释放那不隐蔽的外部HUB命名而且归还那转换外部的HUB名字
FREE(extHubNameW);
return extHubNameA;
GetExternalHubNameError:
// 有一个错误,被分派的自由任何事
if (extHubNameW != NULL)
{
FREE(extHubNameW);
extHubNameW = NULL;
}
return NULL;
}
void CMFC_USBViewDlg::DisplayConnectionInfo(PUSB_NODE_CONNECTION_INFORMATION ConnectInfo, PSTRING_DESCRIPTOR_NODE StringDescs)
{
PCHAR VendorString;
if(ConnectInfo->DeviceDescriptor.bcdUSB == 512)
{
m_strUsbInfo.Format("名称: USB2.0\r\n");
m_MyList.AddString(m_strUsbInfo);
}
VendorString = GetVendorString(ConnectInfo->DeviceDescriptor.idVendor);
if (VendorString != NULL)
{
m_strUsbInfo.Format("VID (idVendor):%04X (%s)\r\n",ConnectInfo->DeviceDescriptor.idVendor,VendorString);
m_MyList.AddString(m_strUsbInfo);
}
else
{
m_strUsbInfo.Format("VID (idVendor):%04X\r\n",ConnectInfo->DeviceDescriptor.idVendor);
m_MyList.AddString(m_strUsbInfo);
}
m_strUsbInfo.Format("PID (idProduct): %04X\r\n",ConnectInfo->DeviceDescriptor.idProduct);
m_MyList.AddString(m_strUsbInfo);
m_strUsbInfo.Format("制造商信息(iManufacturer): 0x%02X\r\n",ConnectInfo->DeviceDescriptor.iManufacturer);
if (ConnectInfo->DeviceDescriptor.iManufacturer)
{
DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iManufacturer,StringDescs);
}
m_strUsbInfo.Format("产品信息(iProduct): 0x%02X\r\n",ConnectInfo->DeviceDescriptor.iProduct);
if (ConnectInfo->DeviceDescriptor.iProduct)
{
DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iProduct,StringDescs);
}
m_strUsbInfo.Format("iSerialNumber: 0x%02X\r\n",ConnectInfo->DeviceDescriptor.iSerialNumber);
if (ConnectInfo->DeviceDescriptor.iSerialNumber)
{
DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iSerialNumber,StringDescs);
}
m_MyList.AddString("\r\n");
}
void CMFC_USBViewDlg::DisplayStringDescriptor(UCHAR Index, PSTRING_DESCRIPTOR_NODE StringDescs)
{
ULONG nBytes;
while (StringDescs)
{
if (StringDescs->DescriptorIndex == Index)
{
TextBufferPos = 0;
if (TextBufferLen - TextBufferPos < BUFFERMINFREESPACE)
{
CHAR *TextBufferTmp;
TextBufferTmp =new CHAR[BUFFERALLOCINCREMENT];
if (TextBufferTmp != NULL)
{
TextBuffer = TextBufferTmp;
TextBufferLen += BUFFERALLOCINCREMENT;
delete []TextBufferTmp;
TextBufferTmp = NULL;
}
else
{
OOPS();
delete []TextBufferTmp;
TextBufferTmp = NULL;
return;
}
}
nBytes = WideCharToMultiByte(CP_ACP,0,StringDescs->StringDescriptor->bString,
StringDescs->StringDescriptor->bLength,
TextBuffer,TextBufferLen,NULL,NULL);
}
StringDescs = StringDescs->Next;
}
switch(Index)
{
case 1:
m_strUsbInfo.Format("制造商信息(iManufacturer):%s",TextBuffer);
break;
case 2:
m_strUsbInfo.Format("产品信息(iProduct):%s",TextBuffer);
break;
case 3:
m_strUsbInfo.Format("序列号(iSerialNumber):%s",TextBuffer);
break;
}
m_MyList.AddString(m_strUsbInfo);
}
PCHAR CMFC_USBViewDlg::GetVendorString(USHORT idVendor)
{
PUSBVENDORID vendorID;
if (idVendor != 0x0000)
{
vendorID = USBVendorIDs;
while (vendorID->usVendorID != 0x0000)
{
if (vendorID->usVendorID == idVendor)
{
return (vendorID->szVendor);
}
vendorID++;
}
}
return NULL;
}
void CMFC_USBViewDlg::DestroyTextBuffer()
{
if (TextBuffer != NULL)
{
//FREE(TextBuffer);
TextBuffer = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -