利用多线程进行USB的读写
void CTestUSBDlg::NumUsbAll()
{
CString strShow;
char HCName[16];
int HCNum;
HANDLE hHCDev;
PCHAR rootHubName;
PCHAR leafName;
ULONG index;
BOOL success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
HANDLE hHubDevice;
for (HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
{
wsprintf(HCName, "\\\\.\\HCD%d", HCNum);
hHCDev = CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hHCDev != INVALID_HANDLE_VALUE)
{
strShow.Format("\r\n找到USB控制器:%d\r\n",HCNum+1);
ShowMessage(strShow);
}
else
break;
PCHAR driverKeyName, deviceDesc;
driverKeyName = GetHCDDriverKeyName(hHCDev);
if(driverKeyName)
{ //想获得此设备的字符名,但不成功!!有高招请告诉我!
// deviceDesc = DriverNameToDeviceDesc(driverKeyName);
}
else
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
strShow.Format("USB控制器:%s\r\n",driverKeyName);
ShowMessage(strShow);
leafName = HCName + sizeof("\\\\.\\") - sizeof("");
strShow.Format("USB控制器名称:%s\r\n",leafName);
ShowMessage(strShow);
ULONG nBytes;
rootHubName =(char*) GetRootHubName(hHCDev);
if(rootHubName==NULL)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
strShow.Format("USB Hub名称:\r\n%s\r\n",rootHubName);
ShowMessage(strShow);
PUSB_NODE_INFORMATION HubInfo;
HubInfo = (PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
PCHAR deviceName;
deviceName = (PCHAR)malloc(strlen(rootHubName) + sizeof("\\\\.\\"));
if (rootHubName != NULL)
{
strcpy(deviceName, "\\\\.\\");
strcpy(deviceName + sizeof("\\\\.\\") - 1, rootHubName);
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
free(deviceName);
if (hHubDevice == INVALID_HANDLE_VALUE)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL);
if (!success)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
}
int port;
port=HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
for (index=1; index <= port; index++)
{
ULONG nBytes;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
if (connectionInfo == NULL)
{
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
connectionInfo->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL);
if (!success)
{
free(connectionInfo);
strShow.Format("错误!\r\n");
ShowMessage(strShow);
goto end;
}
if(connectionInfo)
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
strShow.Format("第%d端口有USB设备连接\r\n",index);
}
else
{
strShow.Format("第%d端口没有USB设备联接\r\n",index);
}
ShowMessage(strShow);
if (connectionInfo->DeviceIsHub)
{
strShow="HUB";
ShowMessage(strShow);
}
deviceDesc = NULL;
if (connectionInfo->ConnectionStatus != NoDeviceConnected)
{
driverKeyName = GetDriverKeyName(hHubDevice,
index);
if (driverKeyName)
{
strShow.Format("状态:已连接!\r\n连接设备名:%s\r\n",driverKeyName);
ShowMessage(strShow);
free(driverKeyName);
}
}
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
ShowCenctInfo(connectionInfo);
}
}
end:;
}
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
}