📄 scanusbdlg.cpp
字号:
(PUSB_CONFIGURATION_DESCRIPTOR)(configDesc+1));
}
else
{
stringDescs = NULL;
}
// If the device connected to the port is an external hub, get the
// name of the external hub and recursively enumerate it.
//
if (connectionInfo->DeviceIsHub)
{
PCHAR extHubName;
extHubName = GetExternalHubName(hHubDevice,index);
if (extHubName != NULL)
{
str.Format("ExternalHubName: %s\r\n",extHubName);
strShow+=str;
//Gordon@Kinpobj.com.cn
EnumerateHub(strShow,
extHubName,
connectionInfo,
configDesc,
stringDescs,
deviceDesc);
// On to the next port
//
continue;
}
}
// Allocate some space for a USBDEVICEINFO structure to hold the
// hub info, hub name, and connection info pointers. GPTR zero
// initializes the structure for us.
//
info = (PUSBDEVICEINFO) ALLOC(sizeof(USBDEVICEINFO));
if (info == NULL)
{
if (configDesc != NULL)
{
FREE(configDesc);
}
FREE(connectionInfo);
break;
}
info->ConnectionInfo = connectionInfo;
info->ConfigDesc = configDesc;
info->StringDescs = stringDescs;
wsprintf(leafName, "[Port%d] ", index);
strcat(leafName, ConnectionStatuses[connectionInfo->ConnectionStatus]);
if (deviceDesc)
{
strcat(leafName, " : ");
strcat(leafName, deviceDesc);
}
strShow+=leafName;
strShow+="\r\n";
//gordon add to show device configuration
DisplayConnectionInfo(strShow,info->ConnectionInfo,
info->StringDescs);
if (info->ConfigDesc)
{
DisplayConfigDesc(strShow,(PUSB_CONFIGURATION_DESCRIPTOR)(info->ConfigDesc + 1),
info->StringDescs);
}
}
}
void CScanUsbDlg::EnumerateHub(CString& strShow, PCHAR HubName, PUSB_NODE_CONNECTION_INFORMATION ConnectionInfo, PUSB_DESCRIPTOR_REQUEST ConfigDesc, PSTRING_DESCRIPTOR_NODE StringDescs, PCHAR DeviceDesc)
{
HANDLE hHubDevice;
PCHAR deviceName;
BOOL success;
ULONG nBytes;
PUSBDEVICEINFO info;
CHAR leafName[512]; // XXXXX how big does this have to be?
// Initialize locals to not allocated state so the error cleanup routine
// only tries to cleanup things that were successfully allocated.
//
info = NULL;
hHubDevice = INVALID_HANDLE_VALUE;
// Allocate some space for a USBDEVICEINFO structure to hold the
// hub info, hub name, and connection info pointers. GPTR zero
// initializes the structure for us.
//
info = (PUSBDEVICEINFO) ALLOC(sizeof(USBDEVICEINFO));
if (info == NULL) goto EnumerateHubError;
// Keep copies of the Hub Name, Connection Info, and Configuration
// Descriptor pointers
//
info->HubName = HubName;
info->ConnectionInfo = ConnectionInfo;
info->ConfigDesc = ConfigDesc;
info->StringDescs = StringDescs;
// Allocate some space for a USB_NODE_INFORMATION structure for this Hub,
info->HubInfo = (PUSB_NODE_INFORMATION)ALLOC(sizeof(USB_NODE_INFORMATION));
if (info->HubInfo == NULL) goto EnumerateHubError;
// Allocate a temp buffer for the full hub device name.
deviceName = (PCHAR)ALLOC(strlen(HubName) + sizeof("\\\\.\\"));
if (deviceName == NULL) goto EnumerateHubError;
// Create the full hub device name
strcpy(deviceName, "\\\\.\\");
strcpy(deviceName + sizeof("\\\\.\\") - 1, info->HubName);
// Try to hub the open device
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
// Done with temp buffer for full hub device name
FREE(deviceName);
if (hHubDevice == INVALID_HANDLE_VALUE) goto EnumerateHubError;
// Now query USBHUB for the USB_NODE_INFORMATION structure for this hub.
// This will tell us the number of downstream ports to enumerate, among
// other things.
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
info->HubInfo,
sizeof(USB_NODE_INFORMATION),
info->HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL);
if (!success) goto EnumerateHubError;
// Build the leaf name from the port number and the device description
if (ConnectionInfo)
{
wsprintf(leafName, "[Port%d] ", ConnectionInfo->ConnectionIndex);
strcat(leafName, ConnectionStatuses[ConnectionInfo->ConnectionStatus]);
strcat(leafName, " : ");
}
else
{
leafName[0] = 0;
}
if (DeviceDesc)
{
strcat(leafName, DeviceDesc);
}
else
{
strcat(leafName, info->HubName);
}
// Now add an item to the TreeView with the PUSBDEVICEINFO pointer info
// as the LPARAM reference value containing everything we know about the
// hub.
//
// hItem = AddLeaf(hTreeParent,
// (LPARAM)info,
// leafName);
// hItem = m_treeInfo.InsertItem(leafName,hTreeParent);
// m_treeInfo.InsertItem(info->StringDescs->StringDescriptor->,hItem);
// if (hItem == NULL)
// {
// goto EnumerateHubError;
// }
strShow+=leafName;
strShow+="\r\n";
// Now recursively enumrate the ports of this hub.
//
EnumerateHubPorts(
strShow,
hHubDevice,
info->HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts
);
CloseHandle(hHubDevice);
return;
EnumerateHubError:
// Clean up any stuff that got allocated
if (hHubDevice != INVALID_HANDLE_VALUE)
{
CloseHandle(hHubDevice);
hHubDevice = INVALID_HANDLE_VALUE;
}
if (info != NULL)
{
if (info->HubName != NULL)
{
FREE(info->HubName);
info->HubName = NULL;
}
if (info->HubInfo != NULL)
{
FREE(info->HubInfo);
info->HubInfo;
}
FREE(info);
info = NULL;
}
if (ConnectionInfo)
{
FREE(ConnectionInfo);
}
if (ConfigDesc)
{
FREE(ConfigDesc);
}
if (StringDescs != NULL)
{
PSTRING_DESCRIPTOR_NODE Next;
do
{
Next = StringDescs->Next;
FREE(StringDescs);
StringDescs = Next;
}while (StringDescs != NULL);
}
}
void CScanUsbDlg::OnOK()
{
Refresh();
}
VOID CScanUsbDlg::DisplayConfigurationDescriptor (CString& strShow,PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,PSTRING_DESCRIPTOR_NODE StringDescs)
{
CString str;
strShow+="\r\nConfiguration Descriptor:\r\n";
str.Format("wTotalLength: 0x%04X\r\n",ConfigDesc->wTotalLength);
strShow+=str;
str.Format("bNumInterfaces: 0x%02X\r\n",ConfigDesc->bNumInterfaces);
strShow+=str;
str.Format("bConfigurationValue: 0x%02X\r\n",ConfigDesc->bConfigurationValue);
strShow+=str;
str.Format("iConfiguration: 0x%02X\r\n",ConfigDesc->iConfiguration);
strShow+=str;
if (ConfigDesc->iConfiguration)
{
DisplayStringDescriptor(strShow,ConfigDesc->iConfiguration,StringDescs);
}
str.Format("bmAttributes: 0x%02X (",ConfigDesc->bmAttributes);
strShow+=str;
if (ConfigDesc->bmAttributes & 0x80)
{
str.Format("Bus Powered ");
}
if (ConfigDesc->bmAttributes & 0x40)
{
str.Format("Self Powered ");
}
if (ConfigDesc->bmAttributes & 0x20)
{
str.Format("Remote Wakeup");
}
strShow+=str;
str.Format(")\r\n");
strShow+=str;
str.Format("MaxPower: 0x%02X (%d Ma)\r\n",ConfigDesc->MaxPower, ConfigDesc->MaxPower * 2);
strShow+=str;
}
VOID CScanUsbDlg::DisplayInterfaceDescriptor (CString& strShow,PUSB_INTERFACE_DESCRIPTOR InterfaceDesc, PSTRING_DESCRIPTOR_NODE StringDescs)
{
PCHAR pStr;
CString str;
strShow+=("\r\nInterface Descriptor:\r\n");
str.Format("bInterfaceNumber: 0x%02X\r\n",InterfaceDesc->bInterfaceNumber);
strShow+=str;
str.Format("bAlternateSetting: 0x%02X\r\n",InterfaceDesc->bAlternateSetting);
strShow+=str;
str.Format("bNumEndpoints: 0x%02X\r\n",InterfaceDesc->bNumEndpoints);
strShow+=str;
str.Format("bInterfaceClass: 0x%02X",InterfaceDesc->bInterfaceClass);
strShow+=str;
switch (InterfaceDesc->bInterfaceClass)
{
case USB_DEVICE_CLASS_AUDIO:
pStr = " (Audio)\r\n";
break;
case USB_DEVICE_CLASS_HUMAN_INTERFACE:
pStr = " (HID)\r\n";
break;
case USB_DEVICE_CLASS_HUB:
pStr = " (Hub)\r\n";
break;
default:
break;
}
str.Format(pStr);
strShow+=str;
str.Format("bInterfaceSubClass: 0x%02X",InterfaceDesc->bInterfaceSubClass);
strShow+=str;
pStr = "\r\n";
/*
switch (InterfaceDesc->bInterfaceClass)
{
case USB_DEVICE_CLASS_AUDIO:
switch (InterfaceDesc->bInterfaceSubClass)
{
case USB_AUDIO_SUBCLASS_AUDIOCONTROL:
pStr = " (Audio Control)\r\n";
break;
case USB_AUDIO_SUBCLASS_AUDIOSTREAMING:
pStr = " (Audio Streaming)\r\n";
break;
case USB_AUDIO_SUBCLASS_MIDISTREAMING:
pStr = " (MIDI Streaming)\r\n";
break;
default:
break;
}
break;
default:
break;
}
*/
str.Format(pStr);
strShow+=str;
str.Format("bInterfaceProtocol: 0x%02X\r\n",InterfaceDesc->bInterfaceProtocol);
strShow+=str;
str.Format("iInterface: 0x%02X\r\n",InterfaceDesc->iInterface);
strShow+=str;
if (InterfaceDesc->iInterface)
{
DisplayStringDescriptor(strShow,InterfaceDesc->iInterface,StringDescs);
}
if (InterfaceDesc->bLength == sizeof(USB_INTERFACE_DESCRIPTOR2))
{
PUSB_INTERFACE_DESCRIPTOR2 interfaceDesc2;
interfaceDesc2 = (PUSB_INTERFACE_DESCRIPTOR2)InterfaceDesc;
str.Format("wNumClasses: 0x%04X\r\n",interfaceDesc2->wNumClasses);
strShow+=str;
}
}
VOID CScanUsbDlg::DisplayEndpointDescriptor (CString& strShow,PUSB_ENDPOINT_DESCRIPTOR EndpointDesc)
{
CString str;
strShow+=("\r\nEndpoint Descriptor:\r\n");
str.Format("bEndpointAddress: 0x%02X\r\n",EndpointDesc->bEndpointAddress);
strShow+=str;
switch (EndpointDesc->bmAttributes & 0x03)
{
case 0x00:
str.Format("Transfer Type: Control\r\n");
strShow+=str;
break;
case 0x01:
str.Format("Transfer Type: Isochronous\r\n");
strShow+=str;
break;
case 0x02:
str.Format("Transfer Type: Bulk\r\n");
strShow+=str;
break;
case 0x03:
str.Format("Transfer Type: Interrupt\r\n");
strShow+=str;
break;
}
str.Format("wMaxPacketSize: 0x%04X (%d)\r\n",EndpointDesc->wMaxPacketSize,EndpointDesc->wMaxPacketSize);
strShow+=str;
if (EndpointDesc->bLength == sizeof(USB_ENDPOINT_DESCRIPTOR))
{
str.Format("bInterval: 0x%02X\r\n",EndpointDesc->bInterval);
strShow+=str;
}
else
{
PUSB_ENDPOINT_DESCRIPTOR2 endpointDesc2;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -