📄 usbkeypnpactivexctl.cpp
字号:
break;
}
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");
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
break;
}
}//end if (rootHubName != NULL)
int port;
port=HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
free(HubInfo);
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");
break;
}
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");
break;
}
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
strShow.Format("第%d端口有USB设备连接\r\n",index);
}
else
{
strShow.Format("第%d端口没有USB设备联接\r\n",index);
}
if (connectionInfo->DeviceIsHub)
{
strShow="HUB";
}
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
WORD vid,pid;
vid = connectionInfo->DeviceDescriptor.idVendor;
pid = connectionInfo->DeviceDescriptor.idProduct;
//0x058F,0x9254
if((vid == 0x058F) &&(pid == 0x9254))//海泰u+key
{
vid = 0x5448;
pid = 0x0001;
}
#ifdef _DEBUG
CString Msg;
Msg.Format("vid:0x%04x pid:0x%04x",vid,pid);
AfxMessageBox(Msg);
if(connectionInfo->DeviceIsHub)//继续读取usb
{
AfxMessageBox("connectionInfo->DeviceIsHub");
PCHAR HubName;
HubName = GetExternalHubName(hHubDevice,index);
if(HubName == NULL)
continue;
}
#endif
for(int i=0;;i++)
{
if(g_bjcakeycallway_suport[i].dwVendorID == 0)
break;
if( (g_bjcakeycallway_suport[i].dwVendorID == vid) &&(g_bjcakeycallway_suport[i].dwProductID==pid))
{
g_bjcakeycallway_suport[i].isExist ++;
break;
}
}
}
free(connectionInfo);
}//end for (index=1; index <= port; index++)
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
}//end for (HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
}*/
PCHAR CUSBKeyPnPActiveXCtrl::GetRootHubName(HANDLE HostController)
{
BOOL success;
ULONG nBytes;
USB_ROOT_HUB_NAME rootHubName;
PUSB_ROOT_HUB_NAME rootHubNameW;
PCHAR rootHubNameA;
rootHubNameW = NULL;
rootHubNameA = NULL;
success = DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
0,
0,
&rootHubName,
sizeof(rootHubName),
&nBytes,
NULL);
if (!success)
{
goto GetRootHubNameError;
}
nBytes = rootHubName.ActualLength;
rootHubNameW =(PUSB_ROOT_HUB_NAME) malloc(nBytes);
if (rootHubNameW == NULL)
{
goto GetRootHubNameError;
}
success = DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
NULL,
0,
rootHubNameW,
nBytes,
&nBytes,
NULL);
if (!success)
{
goto GetRootHubNameError;
}
rootHubNameA = WideStrToMultiStr(rootHubNameW->RootHubName);
free(rootHubNameW);
return rootHubNameA;
GetRootHubNameError:
if (rootHubNameW != NULL)
{
free(rootHubNameW);
rootHubNameW = NULL;
}
return NULL;
}
PCHAR CUSBKeyPnPActiveXCtrl::WideStrToMultiStr(PWCHAR WideStr)
{
ULONG nBytes;
PCHAR MultiStr;
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
NULL,
0,
NULL,
NULL);
if (nBytes == 0)
{
return NULL;
}
MultiStr =(PCHAR) malloc(nBytes);
if (MultiStr == NULL)
{
return NULL;
}
nBytes = WideCharToMultiByte(
CP_ACP,
0,
WideStr,
-1,
MultiStr,
nBytes,
NULL,
NULL);
if (nBytes == 0)
{
free(MultiStr);
return NULL;
}
return MultiStr;
}
PCHAR CUSBKeyPnPActiveXCtrl::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;
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;
}
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName))
{
goto GetDriverKeyNameError;
}
driverKeyNameW = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)malloc(nBytes);
if (driverKeyNameW == NULL)
{
goto GetDriverKeyNameError;
}
driverKeyNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
driverKeyNameW,
nBytes,
driverKeyNameW,
nBytes,
&nBytes,
NULL);
if (!success)
{
goto GetDriverKeyNameError;
}
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
free(driverKeyNameW);
return driverKeyNameA;
GetDriverKeyNameError:
if (driverKeyNameW != NULL)
{
free(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;
}
PCHAR CUSBKeyPnPActiveXCtrl::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;
// Get the length of the name of the external hub attached to the
// specified port.
//
extHubName.ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
&extHubName,
sizeof(extHubName),
&extHubName,
sizeof(extHubName),
&nBytes,
NULL);
if (!success)
{
goto GetExternalHubNameError;
}
// Allocate space to hold the external hub name
//
nBytes = extHubName.ActualLength;
if (nBytes <= sizeof(extHubName))
{
goto GetExternalHubNameError;
}
extHubNameW = (PUSB_NODE_CONNECTION_NAME)malloc(nBytes);
if (extHubNameW == NULL)
{
goto GetExternalHubNameError;
}
// Get the name of the external hub attached to the specified port
//
extHubNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_NAME,
extHubNameW,
nBytes,
extHubNameW,
nBytes,
&nBytes,
NULL);
if (!success)
{
goto GetExternalHubNameError;
}
// Convert the External Hub name
//
extHubNameA = WideStrToMultiStr(extHubNameW->NodeName);
// All done, free the uncoverted external hub name and return the
// converted external hub name
//
free(extHubNameW);
return extHubNameA;
GetExternalHubNameError:
// There was an error, free anything that was allocated
//
if (extHubNameW != NULL)
{
free(extHubNameW);
extHubNameW = NULL;
}
return NULL;
}
void CUSBKeyPnPActiveXCtrl::EnumerateHub(PCHAR HubName)
{
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
HANDLE hHubDevice;
PUSB_NODE_INFORMATION HubInfo;
PCHAR deviceName;
ULONG nBytes;
if(HubName == NULL)
return;
HubInfo = (PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
deviceName = (PCHAR)malloc(strlen(HubName) + sizeof("\\\\.\\"));
strcpy(deviceName, "\\\\.\\");
strcpy(deviceName + sizeof("\\\\.\\") - 1, HubName);
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
free(deviceName);
if (hHubDevice == INVALID_HANDLE_VALUE)
{
free(HubInfo);
return ;
}
if(!DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL))
{
free(HubInfo);
return;
}
int port = HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
free(HubInfo);
for (int index=1; index <= port; index++)
{
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
if(connectionInfo == NULL)
{
break;
}
connectionInfo->ConnectionIndex = index;
if(!DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL))
{
break;
}
if(connectionInfo->ConnectionStatus == DeviceConnected)
{
if(connectionInfo->DeviceIsHub)
{
PCHAR HubName;
HubName = GetExternalHubName(hHubDevice,index);
EnumerateHub(HubName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -