⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test3dlg.cpp

📁 实现对现有USB主控制器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    {
		ShowMessage("出错!\r\n");
        free(driverKeyNameW);
        driverKeyNameW = NULL;
    }

    return NULL;
}


PCHAR CTest3Dlg::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;
		ShowMessage("出错!\r\n");
    }

    return NULL;
}

PCHAR CTest3Dlg::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;
}
//显示设备具体信息
void CTest3Dlg::ShowCenctInfo(PUSB_NODE_CONNECTION_INFORMATION connectionInfo)
{
		CString strShow;
		strShow.Format("connection index:[Port:%d]\r\n",connectionInfo->ConnectionIndex);
		ShowMessage(strShow);
			strShow.Format("Device class:0x%X,idVendor:0x%X,\r\nidProduct:0x%X,\r\n ",
				connectionInfo->DeviceDescriptor.bDeviceClass,
				connectionInfo->DeviceDescriptor.idVendor,
				connectionInfo->DeviceDescriptor.idProduct);
			ShowMessage(strShow);
			  strShow.Format("iManufacturer:0x%X,\r\niSerialNumber:0x%X,\r\n",
				  connectionInfo->DeviceDescriptor.iManufacturer,
				  connectionInfo->DeviceDescriptor.iSerialNumber);
			  ShowMessage(strShow);
			  if(connectionInfo->LowSpeed)
				  strShow="bus speed:low,\r\n";
			  else
				  strShow="bus speed:full,\r\n";
			  ShowMessage(strShow);
			  strShow.Format("Device Address:0x%X,\r\nOpen Pipes:0x%X\r\n",
			  connectionInfo->DeviceAddress,connectionInfo->NumberOfOpenPipes);
			  ShowMessage(strShow);

}

PCHAR CTest3Dlg::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;
}
/*
PUSB_DESCRIPTOR_REQUEST CTest3Dlg::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;


    nBytes = sizeof(configDescReqBuf);

    configDescReq = (PUSB_DESCRIPTOR_REQUEST)configDescReqBuf;
    configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);

    memset(configDescReq, 0, nBytes);
    configDescReq->ConnectionIndex = ConnectionIndex;

    configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
                                        | DescriptorIndex;

    configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_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;
    }
    nBytes = sizeof(USB_DESCRIPTOR_REQUEST) + configDesc->wTotalLength;

    configDescReq = (PUSB_DESCRIPTOR_REQUEST)malloc(nBytes);

    if (configDescReq == NULL)
    {

        return NULL;
    }

    configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);

    configDescReq->ConnectionIndex = ConnectionIndex;

    configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
                                        | DescriptorIndex;

    configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_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;
}
*/



/*
void CTest3Dlg::ShowMore(HDEVINFO hDevInfo, SP_DEVICE_INTERFACE_DATA strtInterfaceData)
{
	CString strShow;
PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;

	DWORD strSzie=0,requiesize=0;
	SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,NULL,0,&strSzie,NULL);

	requiesize=strSzie;
	strtDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
	strtDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

	SP_DEVINFO_DATA infodata;
	infodata.cbSize=sizeof(SP_DEVINFO_DATA);
	if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,
		strtDetailData,strSzie,&requiesize,NULL))
	{
		ShowMessage("查找设备路径时出错!");
		SetupDiDestroyDeviceInfoList(hDevInfo);
		free(strtDetailData);
		return;
	}
	strShow.Format("设备路径:\r\n%s\r\n",strtDetailData->DevicePath);
	ShowMessage(strShow);
	ShowMessage("打开通信端口....\r\n");
		HANDLE hCom = CreateFile (
                  strtDetailData->DevicePath,
                  GENERIC_READ | GENERIC_WRITE,
                  FILE_SHARE_READ | FILE_SHARE_WRITE,
                  NULL, 
                  OPEN_EXISTING, 0, 
                  NULL);
	if (hCom == INVALID_HANDLE_VALUE)
	{
	ShowMessage("无法打开通信端口。不能读取设备信息!(设备连接可能不正确?)\r\n");
		SetupDiDestroyDeviceInfoList(hDevInfo);
		free(strtDetailData);
		return;
	}
	ShowMessage("打开通信端口成功!\r\n");
		HIDD_ATTRIBUTES strtAttrib;
	ShowMessage("正在读取设备的标识...\r\n");

	strtAttrib.Size=sizeof(HIDD_ATTRIBUTES);
	if (!HidD_GetAttributes(hCom,&strtAttrib))
	{
		ShowMessage("查询设备状态时出错!");
		CloseHandle(hCom);

		free(strtDetailData);
		return;	
	}

	ShowMessage("已读取,如下所示:\r\n");
	strShow.Format("VendorID:0x%X\r\n",strtAttrib.VendorID);
	ShowMessage(strShow);
	strShow.Format("ProductID:0x%X\r\n",strtAttrib.ProductID);
	ShowMessage(strShow);
	strShow.Format("VersionNumber:0x%X\r\n",strtAttrib.VersionNumber);
	ShowMessage(strShow);

}

BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
{
	STORAGE_PROPERTY_QUERY	Query;	
	DWORD dwOutBytes;				
	BOOL bResult;					

	Query.PropertyId = StorageDeviceProperty;
	Query.QueryType = PropertyStandardQuery;

	bResult = ::DeviceIoControl(hDevice,			
			IOCTL_STORAGE_QUERY_PROPERTY,			
			&Query, sizeof(STORAGE_PROPERTY_QUERY),	
			pDevDesc, pDevDesc->Size,				
			&dwOutBytes,							
			(LPOVERLAPPED)NULL);					

	return bResult;
}

BOOL CTest3Dlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CDialog::PreTranslateMessage(pMsg);
}

char CTest3Dlg::chFirstDriveFromMask(ULONG unitmask)
{
      char i;
      for (i = 0; i < 26; ++i)  
      {
           if (unitmask & 0x1) 
				break;
            unitmask = unitmask >> 1;
      }
    return (i + 'A');
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -