📄 scanusbdlg.cpp
字号:
0,
0,
&rootHubName,
sizeof(rootHubName),
&nBytes,
NULL);
if (!success) goto GetRootHubNameError;
// Allocate space to hold the Root Hub name
nBytes = rootHubName.ActualLength;
rootHubNameW = (PUSB_ROOT_HUB_NAME)ALLOC(nBytes);
if (rootHubNameW == NULL) goto GetRootHubNameError;
// Get the name of the Root Hub attached to the Host Controller
success = DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
NULL,
0,
rootHubNameW,
nBytes,
&nBytes,
NULL);
if (!success) goto GetRootHubNameError;
// Convert the Root Hub name
rootHubNameA = WideStrToMultiStr(rootHubNameW->RootHubName);
// All done, free the uncoverted Root Hub name and return the
// converted Root Hub name
FREE(rootHubNameW);
return rootHubNameA;
GetRootHubNameError:
// There was an error, free anything that was allocated
if (rootHubNameW != NULL)
{
FREE(rootHubNameW);
rootHubNameW = NULL;
}
return NULL;
}
PCHAR CScanUsbDlg::WideStrToMultiStr(PWCHAR WideStr)
{
ULONG nBytes;
PCHAR MultiStr;
// Get the length of the converted string
//
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
NULL,
0,
NULL,
NULL);
if (nBytes == 0) return NULL;
// Allocate space to hold the converted string //
MultiStr = (PCHAR)ALLOC(nBytes);
if (MultiStr == NULL) return NULL;
// Convert the string
//
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
MultiStr,
nBytes,
NULL,
NULL);
if (nBytes == 0) return NULL;
return MultiStr;
}
PUSB_DESCRIPTOR_REQUEST CScanUsbDlg::GetConfigDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex)
{
BOOL success;
ULONG nBytes;
ULONG nBytesReturned;
UCHAR configDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST)+sizeof(USB_CONFIGURATION_DESCRIPTOR)];
PUSB_DESCRIPTOR_REQUEST configDescReq;
PUSB_CONFIGURATION_DESCRIPTOR configDesc;
// Request the Configuration Descriptor the first time using our
// local buffer, which is just big enough for the Cofiguration
// Descriptor itself.
//
nBytes = sizeof(configDescReqBuf);
configDescReq = (PUSB_DESCRIPTOR_REQUEST)configDescReqBuf;
configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
// Zero fill the entire request structure
//
memset(configDescReq, 0, nBytes);
// Indicate the port from which the descriptor will be requested
//
configDescReq->ConnectionIndex = ConnectionIndex;
//
// USBHUB uses URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE to process this
// IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION request.
//
// USBD will automatically initialize these fields:
// bmRequest = 0x80
// bRequest = 0x06
//
// We must inititialize these fields:
// wValue = Descriptor Type (high) and Descriptor Index (low byte)
// wIndex = Zero (or Language ID for String Descriptors)
// wLength = Length of descriptor buffer
//
configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
| DescriptorIndex;
configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
// Now issue the get descriptor request.
//
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
configDescReq,
nBytes,
configDescReq,
nBytes,
&nBytesReturned,
NULL);
if (!success) return NULL;
if (nBytes != nBytesReturned) return NULL;
if (configDesc->wTotalLength < sizeof(USB_CONFIGURATION_DESCRIPTOR)) return NULL;
// Now request the entire Configuration Descriptor using a dynamically
// allocated buffer which is sized big enough to hold the entire descriptor
//
nBytes = sizeof(USB_DESCRIPTOR_REQUEST) + configDesc->wTotalLength;
configDescReq = (PUSB_DESCRIPTOR_REQUEST)ALLOC(nBytes);
if (configDescReq == NULL) return NULL;
configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
// Indicate the port from which the descriptor will be requested
//
configDescReq->ConnectionIndex = ConnectionIndex;
//
// USBHUB uses URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE to process this
// IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION request.
//
// USBD will automatically initialize these fields:
// bmRequest = 0x80
// bRequest = 0x06
//
// We must inititialize these fields:
// wValue = Descriptor Type (high) and Descriptor Index (low byte)
// wIndex = Zero (or Language ID for String Descriptors)
// wLength = Length of descriptor buffer
//
configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
| DescriptorIndex;
configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
// Now issue the get descriptor request.
//
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
configDescReq,
nBytes,
configDescReq,
nBytes,
&nBytesReturned,
NULL);
if (!success)
{
FREE(configDescReq);
return NULL;
}
if (nBytes != nBytesReturned)
{
FREE(configDescReq);
return NULL;
}
if (configDesc->wTotalLength != (nBytes - sizeof(USB_DESCRIPTOR_REQUEST)))
{
FREE(configDescReq);
return NULL;
}
return configDescReq;
}
PCHAR CScanUsbDlg::GetDriverKeyName(HANDLE Hub, ULONG ConnectionIndex)
{
BOOL success;
ULONG nBytes;
USB_NODE_CONNECTION_DRIVERKEY_NAME driverKeyName;
PUSB_NODE_CONNECTION_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
// Get the length of the name of the driver key of the device attached to
// the specified port.
driverKeyName.ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
&driverKeyName,
sizeof(driverKeyName),
&driverKeyName,
sizeof(driverKeyName),
&nBytes,
NULL);
if (!success) goto GetDriverKeyNameError;
// Allocate space to hold the driver key name
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName)) goto GetDriverKeyNameError;
driverKeyNameW = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)ALLOC(nBytes);
if (driverKeyNameW == NULL) goto GetDriverKeyNameError;
// Get the name of the driver key of the device attached to
// the specified port.
driverKeyNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
driverKeyNameW,
nBytes,
driverKeyNameW,
nBytes,
&nBytes,
NULL);
if (!success) goto GetDriverKeyNameError;
// Convert the driver key name
//
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
// All done, free the uncoverted driver key name and return the
// converted driver key name
FREE(driverKeyNameW);
return driverKeyNameA;
GetDriverKeyNameError:
// There was an error, free anything that was allocated
if (driverKeyNameW != NULL)
{
FREE(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;
}
PCHAR CScanUsbDlg::GetHCDDriverKeyName(HANDLE HCD)
{
BOOL success;
ULONG nBytes;
USB_HCD_DRIVERKEY_NAME driverKeyName;
PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
/*Get the length of the name of the driver key of the HCD*/
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
&driverKeyName,
sizeof(driverKeyName),
&driverKeyName,
sizeof(driverKeyName),
&nBytes,
NULL);
if (!success) goto GetHCDDriverKeyNameError;
/* Allocate space to hold the driver key name */
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName)) goto GetHCDDriverKeyNameError;
driverKeyNameW = (PUSB_HCD_DRIVERKEY_NAME)ALLOC(nBytes);
if (driverKeyNameW == NULL) goto GetHCDDriverKeyNameError;
/* Get the name of the driver key of the device attached to the specified port.*/
success = DeviceIoControl(HCD,
IOCTL_GET_HCD_DRIVERKEY_NAME,
driverKeyNameW,
nBytes,
driverKeyNameW,
nBytes,
&nBytes,
NULL);
if (!success) goto GetHCDDriverKeyNameError;
/* // Convert the driver key name */
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
/*All done, free the uncoverted driver key name and return the converted driver key name */
FREE(driverKeyNameW);
return driverKeyNameA;
GetHCDDriverKeyNameError:
/* There was an error, free anything that was allocated */
if (driverKeyNameW != NULL)
{
FREE(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;
}
void CScanUsbDlg::EnumerateHubPorts(CString& strShow, HANDLE hHubDevice, ULONG NumPorts)
{
ULONG index;
BOOL success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
PUSB_DESCRIPTOR_REQUEST configDesc;
PSTRING_DESCRIPTOR_NODE stringDescs;
PUSBDEVICEINFO info;
PCHAR driverKeyName;
PCHAR deviceDesc;
CHAR leafName[512]; // XXXXX how big does this have to be?
CString str;
// Loop over all ports of the hub.
//
// Port indices are 1 based, not 0 based.
//
for (index=1; index <= NumPorts; index++)
{
ULONG nBytes;
// Allocate space to hold the connection info for this port.
// For now, allocate it big enough to hold info for 30 pipes.
//
// Endpoint numbers are 0-15. Endpoint number 0 is the standard
// control endpoint which is not explicitly listed in the Configuration
// Descriptor. There can be an IN endpoint and an OUT endpoint at
// endpoint numbers 1-15 so there can be a maximum of 30 endpoints
// per device configuration.
//
// Should probably size this dynamically at some point.
//
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)ALLOC(nBytes);
if (connectionInfo == NULL) break;
//
// Now query USBHUB for the USB_NODE_CONNECTION_INFORMATION structure
// for this port. This will tell us if a device is attached to this
// port, among other things.
//
connectionInfo->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL);
if (!success)
{
FREE(connectionInfo);
continue;
}
// Update the count of connected devices
//
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
// TotalDevicesConnected++;
}
if (connectionInfo->DeviceIsHub)
{
// TotalHubs++;
}
// If there is a device connected, get the Device Description
//
deviceDesc = NULL;
if (connectionInfo->ConnectionStatus != NoDeviceConnected)
{
driverKeyName = GetDriverKeyName(hHubDevice,index);
str.Format("DriverKeyName:%s\r\n",driverKeyName);
strShow+=str;
if (driverKeyName)
{
deviceDesc = DriverNameToDeviceDesc(driverKeyName);
str.Format("DriverDesc:%s\r\n",deviceDesc);
strShow+=str;
FREE(driverKeyName);
}
}
// If there is a device connected to the port, try to retrieve the
// Configuration Descriptor from the device.
//
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
configDesc = GetConfigDescriptor(hHubDevice,index,0);
}
else
{
configDesc = NULL;
}
if (
configDesc != NULL &&
AreThereStringDescriptors(&connectionInfo->DeviceDescriptor,(PUSB_CONFIGURATION_DESCRIPTOR)(configDesc+1))
)
{
stringDescs = GetAllStringDescriptors(
hHubDevice,
index,
&connectionInfo->DeviceDescriptor,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -