MFC枚举USB设备碰到的一个疑难,还没解决

时间:2024-01-19 10:34:14
代码如下:
打开USB Hub设备之后,返回句柄hHubDevice,然后使用EnumerateHubPorts来枚举Hub的端
口。疑问在代码的中文注释中。
 bool CUsbEnumHub::EnumerateHubPorts(HANDLE hHubDevice, ULONG NumPorts, UsbItem* pRootItem)
{
ULONG index;
BOOL bSuccess;
PUSB_NODE_CONNECTION_INFORMATION_EX connectionInfo;
CString strDriverKeyName; // Loop over all ports of the hub.
// Port indices are 1 based, not 0 based.
for (index=; index <= NumPorts; index++)
{
ULONG nBytes;
bAwUsb = FALSE;
// 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_EX) +
sizeof(USB_PIPE_INFO) * ; connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION_EX)ALLOC(nBytes); if (connectionInfo == NULL)
{
OOPS();
break;
} //
// Now query USBHUB for the USB_NODE_CONNECTION_INFORMATION_EX structure
// for this port. This will tell us if a device is attached to this
// port, among other things.
//
connectionInfo->ConnectionIndex = index; bSuccess = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL); if (!bSuccess)
{
FREE(connectionInfo);
continue;
}
UsbItem *pItemChild = new UsbItem;
pItemChild->parent = pRootItem;
pItemChild->strDisplayName.Format(_T("[Port%d]"), index); // Update the count of connected devices
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
//获取Dirver Key Name
GetDriverKeyName(hHubDevice, index, pItemChild->strName);
//在这里,判断到端口有设备连接,请教怎么获取该设备的路径,就是可以使用CreateFile打开的那种路径。
//这里获取的Driver Key Name并不是路径,无法使用CreateFile来打开。
} // 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)
{
CString strExtHubName;
bSuccess = GetExternalHubName(hHubDevice, index, strExtHubName);
if (bSuccess)
{
pItemChild->strName = strExtHubName;
pItemChild->usbType = USB_TYPE_HUB;
pRootItem->childArray.Add(pItemChild);
EnumerateHub(strExtHubName, pItemChild);
// On to the next port
FREE(connectionInfo);
continue;
}
else
{
delete pItemChild;
}
}
else
{
pItemChild->usbType = USB_TYPE_PORT;
pRootItem->childArray.Add(pItemChild);
}
FREE(connectionInfo);
} return bSuccess ? true : false;
}
另一端代码,这段代码直接枚举USB设备,忽略USB Hub的枚举。这里的枚举可以获取到每
个连接到USB端口的设备的路径,可以使用CreateFile打开操作。
有没有办法把这里枚举到的USB设备和上面枚举到的USB设备建立联系呢?,代码如下:
 void CTurboDemoDlg::OnBnClickedSusb()
{
HANDLE hHCDev;
HDEVINFO hDevInfo;
SP_DEVICE_INTERFACE_DATA hDevInfoData;
PSP_DEVICE_INTERFACE_DETAIL_DATA deviceDetailData;
ULONG index;
ULONG requiredLength; hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_CLASS_USB_DEVICE,
NULL,
NULL,
(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
if (hDevInfo == INVALID_HANDLE_VALUE)
{
return ;
}
hDevInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (index=; SetupDiEnumDeviceInterfaces(hDevInfo,
,
(LPGUID)&GUID_CLASS_USB_DEVICE,
index,
&hDevInfoData); index++)
{
SetupDiGetDeviceInterfaceDetail(hDevInfo,
&hDevInfoData,
NULL,
,
&requiredLength,
NULL); deviceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)GlobalAlloc(GPTR, requi
redLength); deviceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); SetupDiGetDeviceInterfaceDetail(hDevInfo,
&hDevInfoData,
deviceDetailData,
requiredLength,
&requiredLength,
NULL); //deviceDetailData->DevicePath就是USB设备的路径
//但是使用该方法却又无法知道该设备在哪个Hub端口,苦恼。
hHCDev = CreateFile(deviceDetailData->DevicePath,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
,
NULL); // If the handle is valid, then we've successfully opened a Host
// Controller. Display some info about the Host Controller itself,
// then enumerate the Root Hub attached to the Host Controller.
if (hHCDev != INVALID_HANDLE_VALUE)
{
CString strMsg;
strMsg.Format(_T("Found: %s\n"), deviceDetailData->DevicePath);
OutputDebugString(strMsg); CloseHandle(hHCDev);
} GlobalFree(deviceDetailData);
} SetupDiDestroyDeviceInfoList(hDevInfo);
}