testUSBDlg.cpp
Upload User: hnzyys
Upload Date: 2015-09-10
Package Size: 423k
Code Size: 27k
Category:

Communication

Development Platform:

Visual C++

  1. // testUSBDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testUSB.h"
  5. #include "testUSBDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. #define NUM_HCS_TO_CHECK 10
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. #include "ScrollerCtrl.h"
  15. class CAboutDlg : public CDialog
  16. {
  17. public:
  18. CAboutDlg();
  19. // Dialog Data
  20. //{{AFX_DATA(CAboutDlg)
  21. enum { IDD = IDD_ABOUTBOX };
  22. //}}AFX_DATA
  23. // ClassWizard generated virtual function overrides
  24. //{{AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. public:
  29. CScrollerCtrl m_scroller;
  30. // Implementation
  31. protected:
  32. //{{AFX_MSG(CAboutDlg)
  33. afx_msg void SwitchTip();
  34. virtual BOOL OnInitDialog();
  35. //}}AFX_MSG
  36. DECLARE_MESSAGE_MAP()
  37. };
  38. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  39. {
  40. //{{AFX_DATA_INIT(CAboutDlg)
  41. //}}AFX_DATA_INIT
  42. }
  43. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  44. {
  45. CDialog::DoDataExchange(pDX);
  46. //{{AFX_DATA_MAP(CAboutDlg)
  47. //}}AFX_DATA_MAP
  48. }
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  50. //{{AFX_MSG_MAP(CAboutDlg)
  51. ON_COMMAND(1, SwitchTip)
  52. //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CTestUSBDlg dialog
  56. CHAR buf[512];
  57. TCHAR   szMoveDiskName[33];
  58. TCHAR szDrvName[33];
  59. BOOL CAboutDlg::OnInitDialog() 
  60. {
  61. CDialog::OnInitDialog();
  62.    m_scroller.SetFont("Microsoft Sans Serif", 10, FW_SEMIBOLD);
  63.    m_scroller.SetText("tUSB例程说明");
  64.    m_scroller.SetWrapping(FALSE);
  65.    m_scroller.SetScrollDelay(10);
  66.    m_scroller.SetScrollPause(3000);
  67.    m_scroller.SetFgColor(RGB(255,0,0));
  68. CRect rect;
  69. GetClientRect(&rect);
  70. m_scroller.Create(rect, this, WS_CHILD|WS_VISIBLE, 1);
  71. return TRUE;  // return TRUE unless you set the focus to a control
  72.               // EXCEPTION: OCX Property Pages should return FALSE
  73. }
  74. void CAboutDlg::SwitchTip()
  75. {
  76.    static const CString astrTips[] = {
  77.       "  这是一个关于USB的例程。",
  78.       "  需要win2000或者winXP的DDK才能编译。",
  79.   "  如果你有更好的方法或者解决了其中的问题请电邮给我。",
  80.   "E-mail:daisun1998@163.net",
  81.       "tSuperCar",
  82.   "t2004.10",
  83.       "n"};
  84.    static const int nNumTips = sizeof(astrTips)/sizeof(CString);
  85.    static int nCurrentTip = 0;
  86.    m_scroller.SetText(astrTips[nCurrentTip%nNumTips]);
  87.    ++nCurrentTip;
  88. }
  89. CTestUSBDlg::CTestUSBDlg(CWnd* pParent /*=NULL*/)
  90. : CDialog(CTestUSBDlg::IDD, pParent)
  91. {
  92. //{{AFX_DATA_INIT(CTestUSBDlg)
  93. m_showMessage = _T("");
  94. //}}AFX_DATA_INIT
  95. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  96. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  97. }
  98. void CTestUSBDlg::DoDataExchange(CDataExchange* pDX)
  99. {
  100. CDialog::DoDataExchange(pDX);
  101. //{{AFX_DATA_MAP(CTestUSBDlg)
  102. DDX_Text(pDX, IDC_SHOWMESSAGE, m_showMessage);
  103. //}}AFX_DATA_MAP
  104. }
  105. BEGIN_MESSAGE_MAP(CTestUSBDlg, CDialog)
  106. //{{AFX_MSG_MAP(CTestUSBDlg)
  107. ON_WM_SYSCOMMAND()
  108. ON_WM_PAINT()
  109. ON_WM_QUERYDRAGICON()
  110. ON_BN_CLICKED(IDC_SHOWUSBALL, OnShowusball)
  111. ON_BN_CLICKED(IDC_SHOWUSBHID, OnShowusbhid)
  112. ON_BN_CLICKED(IDC_SHOWUSBDISK, OnShowusbdisk)
  113. //}}AFX_MSG_MAP
  114. END_MESSAGE_MAP()
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CTestUSBDlg message handlers
  117. BOOL CTestUSBDlg::OnInitDialog()
  118. {
  119. CDialog::OnInitDialog();
  120. // Add "About..." menu item to system menu.
  121. // IDM_ABOUTBOX must be in the system command range.
  122. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  123. ASSERT(IDM_ABOUTBOX < 0xF000);
  124. CMenu* pSysMenu = GetSystemMenu(FALSE);
  125. if (pSysMenu != NULL)
  126. {
  127. CString strAboutMenu;
  128. strAboutMenu.LoadString(IDS_ABOUTBOX);
  129. if (!strAboutMenu.IsEmpty())
  130. {
  131. pSysMenu->AppendMenu(MF_SEPARATOR);
  132. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  133. }
  134. }
  135. // Set the icon for this dialog.  The framework does this automatically
  136. //  when the application's main window is not a dialog
  137. SetIcon(m_hIcon, TRUE); // Set big icon
  138. SetIcon(m_hIcon, FALSE); // Set small icon
  139. // TODO: Add extra initialization here
  140. return TRUE;  // return TRUE  unless you set the focus to a control
  141. }
  142. void CTestUSBDlg::OnSysCommand(UINT nID, LPARAM lParam)
  143. {
  144. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  145. {
  146. CAboutDlg dlgAbout;
  147. dlgAbout.DoModal();
  148. }
  149. else
  150. {
  151. CDialog::OnSysCommand(nID, lParam);
  152. }
  153. }
  154. // If you add a minimize button to your dialog, you will need the code below
  155. //  to draw the icon.  For MFC applications using the document/view model,
  156. //  this is automatically done for you by the framework.
  157. void CTestUSBDlg::OnPaint() 
  158. {
  159. if (IsIconic())
  160. {
  161. CPaintDC dc(this); // device context for painting
  162. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  163. // Center icon in client rectangle
  164. int cxIcon = GetSystemMetrics(SM_CXICON);
  165. int cyIcon = GetSystemMetrics(SM_CYICON);
  166. CRect rect;
  167. GetClientRect(&rect);
  168. int x = (rect.Width() - cxIcon + 1) / 2;
  169. int y = (rect.Height() - cyIcon + 1) / 2;
  170. // Draw the icon
  171. dc.DrawIcon(x, y, m_hIcon);
  172. }
  173. else
  174. {
  175. CDialog::OnPaint();
  176. }
  177. }
  178. // The system calls this to obtain the cursor to display while the user drags
  179. //  the minimized window.
  180. HCURSOR CTestUSBDlg::OnQueryDragIcon()
  181. {
  182. return (HCURSOR) m_hIcon;
  183. }
  184. void CTestUSBDlg::OnShowusball() 
  185. {
  186. m_showMessage.Empty();
  187. CString str;
  188. str="查找所有USB设备:";
  189. ShowMessage(str);
  190. NumUsbAll();
  191. }
  192. void CTestUSBDlg::OnShowusbhid() 
  193. {
  194. // TODO: Add your control notification handler code here
  195. m_showMessage.Empty();
  196. ShowHidInfo();
  197. }
  198. void CTestUSBDlg::OnShowusbdisk() 
  199. {
  200. // TODO: Add your control notification handler code here
  201. m_showMessage.Empty();
  202. ShowUdisk();
  203. }
  204. void CTestUSBDlg::ShowMessage(CString str)
  205. {
  206. m_showMessage += str;
  207. SetDlgItemText(IDC_SHOWMESSAGE,m_showMessage);
  208. }
  209. void CTestUSBDlg::NumUsbAll()
  210. {
  211. CString strShow;
  212. char        HCName[16];
  213.     int         HCNum;
  214.     HANDLE      hHCDev;
  215.     PCHAR       rootHubName;
  216.     PCHAR       leafName;
  217. ULONG       index;
  218.     BOOL        success;
  219.     PUSB_NODE_CONNECTION_INFORMATION    connectionInfo;
  220. HANDLE hHubDevice;
  221.    for (HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
  222.     {
  223.         wsprintf(HCName, "\\.\HCD%d", HCNum);
  224.         hHCDev = CreateFile(HCName,
  225.                             GENERIC_WRITE,
  226.                             FILE_SHARE_WRITE,
  227.                             NULL,
  228.                             OPEN_EXISTING,
  229.                             0,
  230.                             NULL);
  231. if (hHCDev != INVALID_HANDLE_VALUE)
  232.         {
  233. strShow.Format("rn找到USB控制器:%drn",HCNum+1);
  234. ShowMessage(strShow);
  235. }
  236. else
  237. break;
  238.   PCHAR driverKeyName, deviceDesc;
  239.           driverKeyName = GetHCDDriverKeyName(hHCDev);
  240.   if(driverKeyName)
  241.   {   //想获得此设备的字符名,但不成功!!有高招请告诉我!
  242. //   deviceDesc = DriverNameToDeviceDesc(driverKeyName);
  243.   }
  244.   else
  245.   {
  246.  strShow.Format("错误!rn");
  247.  ShowMessage(strShow);
  248.   goto end;   
  249.   }
  250. strShow.Format("USB控制器:%srn",driverKeyName);
  251. ShowMessage(strShow);
  252.         leafName = HCName + sizeof("\\.\") - sizeof("");
  253. strShow.Format("USB控制器名称:%srn",leafName);
  254. ShowMessage(strShow);
  255. ULONG           nBytes;
  256.         rootHubName =(char*) GetRootHubName(hHCDev);
  257. if(rootHubName==NULL)
  258.  {
  259.  strShow.Format("错误!rn");
  260.  ShowMessage(strShow);
  261.   goto end;
  262.   }
  263. strShow.Format("USB Hub名称:rn%srn",rootHubName);
  264. ShowMessage(strShow);
  265.  PUSB_NODE_INFORMATION HubInfo;
  266.  HubInfo = (PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
  267.  PCHAR deviceName;
  268.  deviceName = (PCHAR)malloc(strlen(rootHubName) + sizeof("\\.\"));
  269.          if (rootHubName != NULL)
  270.          {
  271. strcpy(deviceName, "\\.\");
  272. strcpy(deviceName + sizeof("\\.\") - 1, rootHubName);
  273. hHubDevice = CreateFile(deviceName,
  274.                             GENERIC_WRITE,
  275.                             FILE_SHARE_WRITE,
  276.                             NULL,
  277.                             OPEN_EXISTING,
  278.                             0,
  279.                             NULL);
  280. free(deviceName);
  281. if (hHubDevice == INVALID_HANDLE_VALUE)
  282. {
  283.  strShow.Format("错误!rn");
  284.  ShowMessage(strShow);
  285. goto end;
  286. }
  287. success = DeviceIoControl(hHubDevice,
  288.                               IOCTL_USB_GET_NODE_INFORMATION,
  289.                               HubInfo,
  290.                               sizeof(USB_NODE_INFORMATION),
  291.                               HubInfo,
  292.                               sizeof(USB_NODE_INFORMATION),
  293.                               &nBytes,
  294.                               NULL);
  295. if (!success)
  296. {
  297.  strShow.Format("错误!rn");
  298.  ShowMessage(strShow);
  299. goto end;
  300. }
  301.  }
  302.  int port;
  303.  port=HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
  304.  for (index=1; index <= port; index++)
  305.  {
  306.  ULONG nBytes;
  307.  nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
  308.                 sizeof(USB_PIPE_INFO) * 30;
  309.  connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
  310.  if (connectionInfo == NULL)
  311.  {
  312.   strShow.Format("错误!rn");
  313.  ShowMessage(strShow);
  314.  goto end;
  315.  }
  316.  connectionInfo->ConnectionIndex = index;
  317.  success = DeviceIoControl(hHubDevice,
  318.                                   IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
  319.                                   connectionInfo,
  320.                                   nBytes,
  321.                                   connectionInfo,
  322.                                   nBytes,
  323.                                   &nBytes,
  324.                                   NULL);
  325.  if (!success)
  326.  {
  327.  free(connectionInfo);
  328.  strShow.Format("错误!rn");
  329.  ShowMessage(strShow);
  330.  goto end;
  331.  }
  332.  if(connectionInfo)
  333.       if (connectionInfo->ConnectionStatus == DeviceConnected)
  334.   {
  335. strShow.Format("第%d端口有USB设备连接rn",index);
  336.   }
  337.   else
  338.   {
  339.  strShow.Format("第%d端口没有USB设备联接rn",index);
  340.   }
  341.   ShowMessage(strShow);
  342.    if (connectionInfo->DeviceIsHub)
  343.    {
  344.    strShow="HUB";
  345.    ShowMessage(strShow);
  346.    }
  347.    deviceDesc = NULL;
  348.    if (connectionInfo->ConnectionStatus != NoDeviceConnected)
  349.    {
  350.    driverKeyName = GetDriverKeyName(hHubDevice,
  351.                                              index);
  352.    if (driverKeyName)
  353.    {
  354.    strShow.Format("状态:已连接!rn连接设备名:%srn",driverKeyName);
  355.    ShowMessage(strShow);
  356.    free(driverKeyName);
  357.    }
  358.    }
  359.       if (connectionInfo->ConnectionStatus == DeviceConnected)
  360.         {
  361. ShowCenctInfo(connectionInfo);
  362.         }
  363.  }
  364. end:; 
  365.              }
  366.   CloseHandle(hHubDevice);
  367. CloseHandle(hHCDev);
  368. }
  369. PCHAR CTestUSBDlg::GetHCDDriverKeyName(HANDLE HCD)
  370. {
  371.    BOOL                    success;
  372.     ULONG                   nBytes;
  373.     USB_HCD_DRIVERKEY_NAME  driverKeyName;
  374.     PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
  375.     PCHAR                   driverKeyNameA;
  376.     driverKeyNameW = NULL;
  377.     driverKeyNameA = NULL;
  378.     success = DeviceIoControl(HCD,
  379.                               IOCTL_GET_HCD_DRIVERKEY_NAME,
  380.                               &driverKeyName,
  381.                               sizeof(driverKeyName),
  382.                               &driverKeyName,
  383.                               sizeof(driverKeyName),
  384.                               &nBytes,
  385.                               NULL);
  386.     if (!success)
  387. {
  388.         goto GetHCDDriverKeyNameError;
  389.     }
  390.     nBytes = driverKeyName.ActualLength;
  391.     if (nBytes <= sizeof(driverKeyName))
  392.     {
  393.         goto GetHCDDriverKeyNameError;
  394.     }
  395.     driverKeyNameW =(PUSB_HCD_DRIVERKEY_NAME) malloc(nBytes);
  396.     if (driverKeyNameW == NULL)
  397.     {
  398.         goto GetHCDDriverKeyNameError;
  399.     }
  400.     success = DeviceIoControl(HCD,
  401.                               IOCTL_GET_HCD_DRIVERKEY_NAME,
  402.                               driverKeyNameW,
  403.                               nBytes,
  404.                               driverKeyNameW,
  405.                               nBytes,
  406.                               &nBytes,
  407.                               NULL);
  408.     if (!success)
  409.     {
  410.         goto GetHCDDriverKeyNameError;
  411.     }
  412.     driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
  413.     free(driverKeyNameW);
  414.     return driverKeyNameA;
  415. GetHCDDriverKeyNameError:
  416.     if (driverKeyNameW != NULL)
  417.     {
  418. ShowMessage("出错!rn");
  419.         free(driverKeyNameW);
  420.         driverKeyNameW = NULL;
  421.     }
  422.     return NULL;
  423. }
  424. PCHAR CTestUSBDlg::GetRootHubName(HANDLE HostController)
  425. {
  426.     BOOL                success;
  427.     ULONG               nBytes;
  428.     USB_ROOT_HUB_NAME   rootHubName;
  429.     PUSB_ROOT_HUB_NAME  rootHubNameW;
  430.     PCHAR               rootHubNameA;
  431.     rootHubNameW = NULL;
  432.     rootHubNameA = NULL;
  433.    success = DeviceIoControl(HostController,
  434.                               IOCTL_USB_GET_ROOT_HUB_NAME,
  435.                               0,
  436.                               0,
  437.                               &rootHubName,
  438.                               sizeof(rootHubName),
  439.                               &nBytes,
  440.                               NULL);
  441.     if (!success)
  442.     {
  443.         goto GetRootHubNameError;
  444.     }
  445.     nBytes = rootHubName.ActualLength;
  446.     rootHubNameW =(PUSB_ROOT_HUB_NAME) malloc(nBytes);
  447.     if (rootHubNameW == NULL)
  448.     {
  449.         goto GetRootHubNameError;
  450.     }
  451.     success = DeviceIoControl(HostController,
  452.                               IOCTL_USB_GET_ROOT_HUB_NAME,
  453.                               NULL,
  454.                               0,
  455.                               rootHubNameW,
  456.                               nBytes,
  457.                               &nBytes,
  458.                               NULL);
  459.     if (!success)
  460.     {
  461.        goto GetRootHubNameError;
  462.     }
  463.     rootHubNameA = WideStrToMultiStr(rootHubNameW->RootHubName);
  464.     free(rootHubNameW);
  465.     return rootHubNameA;
  466. GetRootHubNameError:
  467.     if (rootHubNameW != NULL)
  468.     {
  469.         free(rootHubNameW);
  470.         rootHubNameW = NULL;
  471. ShowMessage("出错!rn");
  472.     }
  473.     return NULL;
  474. }
  475. PCHAR CTestUSBDlg::GetDriverKeyName(HANDLE Hub, ULONG ConnectionIndex)
  476. {
  477.     BOOL                                success;
  478.     ULONG                               nBytes;
  479.     USB_NODE_CONNECTION_DRIVERKEY_NAME  driverKeyName;
  480.     PUSB_NODE_CONNECTION_DRIVERKEY_NAME driverKeyNameW;
  481.     PCHAR                               driverKeyNameA;
  482.     driverKeyNameW = NULL;
  483.     driverKeyNameA = NULL;
  484.     driverKeyName.ConnectionIndex = ConnectionIndex;
  485.     success = DeviceIoControl(Hub,
  486.                               IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
  487.                               &driverKeyName,
  488.                               sizeof(driverKeyName),
  489.                               &driverKeyName,
  490.                               sizeof(driverKeyName),
  491.                               &nBytes,
  492.                               NULL);
  493.     if (!success)
  494.     {
  495.         goto GetDriverKeyNameError;
  496.     }
  497.     nBytes = driverKeyName.ActualLength;
  498.     if (nBytes <= sizeof(driverKeyName))
  499.     {
  500.         goto GetDriverKeyNameError;
  501.     }
  502.     driverKeyNameW = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)malloc(nBytes);
  503.     if (driverKeyNameW == NULL)
  504.     {
  505.         goto GetDriverKeyNameError;
  506.     }
  507.     driverKeyNameW->ConnectionIndex = ConnectionIndex;
  508.     success = DeviceIoControl(Hub,
  509.                               IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
  510.                               driverKeyNameW,
  511.                               nBytes,
  512.                               driverKeyNameW,
  513.                               nBytes,
  514.                               &nBytes,
  515.                               NULL);
  516.     if (!success)
  517.     {
  518.         goto GetDriverKeyNameError;
  519.     }
  520.     driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
  521.     free(driverKeyNameW);
  522.     return driverKeyNameA;
  523. GetDriverKeyNameError:
  524.     if (driverKeyNameW != NULL)
  525.     {
  526.         free(driverKeyNameW);
  527.         driverKeyNameW = NULL;
  528.     }
  529.     return NULL;
  530. }
  531. PUSB_DESCRIPTOR_REQUEST CTestUSBDlg::GetConfigDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex)
  532. {
  533.    BOOL    success;
  534.     ULONG   nBytes;
  535.     ULONG   nBytesReturned;
  536.     UCHAR   configDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST) +
  537.                              sizeof(USB_CONFIGURATION_DESCRIPTOR)];
  538.     PUSB_DESCRIPTOR_REQUEST         configDescReq;
  539.     PUSB_CONFIGURATION_DESCRIPTOR   configDesc;
  540.     nBytes = sizeof(configDescReqBuf);
  541.     configDescReq = (PUSB_DESCRIPTOR_REQUEST)configDescReqBuf;
  542.     configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
  543.     memset(configDescReq, 0, nBytes);
  544.     configDescReq->ConnectionIndex = ConnectionIndex;
  545.     configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
  546.                                         | DescriptorIndex;
  547.     configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
  548.     success = DeviceIoControl(hHubDevice,
  549.                               IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
  550.                               configDescReq,
  551.                               nBytes,
  552.                               configDescReq,
  553.                               nBytes,
  554.                               &nBytesReturned,
  555.                               NULL);
  556.     if (!success)
  557.     {
  558.         return NULL;
  559.     }
  560.     if (nBytes != nBytesReturned)
  561.     {
  562.         return NULL;
  563.     }
  564.     if (configDesc->wTotalLength < sizeof(USB_CONFIGURATION_DESCRIPTOR))
  565.     {
  566.         return NULL;
  567.     }
  568.     nBytes = sizeof(USB_DESCRIPTOR_REQUEST) + configDesc->wTotalLength;
  569.     configDescReq = (PUSB_DESCRIPTOR_REQUEST)malloc(nBytes);
  570.     if (configDescReq == NULL)
  571.     {
  572.         return NULL;
  573.     }
  574.     configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
  575.     configDescReq->ConnectionIndex = ConnectionIndex;
  576.     configDescReq->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
  577.                                         | DescriptorIndex;
  578.     configDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
  579.     success = DeviceIoControl(hHubDevice,
  580.                               IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
  581.                               configDescReq,
  582.                               nBytes,
  583.                               configDescReq,
  584.                               nBytes,
  585.                               &nBytesReturned,
  586.                               NULL);
  587.     if (!success)
  588.     {
  589.         free(configDescReq);
  590.         return NULL;
  591.     }
  592.     if (nBytes != nBytesReturned)
  593.     {
  594.         free(configDescReq);
  595.         return NULL;
  596.     }
  597.     if (configDesc->wTotalLength != (nBytes - sizeof(USB_DESCRIPTOR_REQUEST)))
  598.     {
  599.         free(configDescReq);
  600.         return NULL;
  601.     }
  602.     return configDescReq;
  603. }
  604. void CTestUSBDlg::ShowCenctInfo(PUSB_NODE_CONNECTION_INFORMATION connectionInfo)
  605. {
  606. CString strShow;
  607. strShow.Format("connection index:[Port:%d]rn",connectionInfo->ConnectionIndex);
  608. ShowMessage(strShow);
  609. strShow.Format("Device class:0x%X,idVendor:0x%X,rnidProduct:0x%X,rn ",
  610. connectionInfo->DeviceDescriptor.bDeviceClass,
  611. connectionInfo->DeviceDescriptor.idVendor,
  612. connectionInfo->DeviceDescriptor.idProduct);
  613. ShowMessage(strShow);
  614.   strShow.Format("iManufacturer:0x%X,rniSerialNumber:0x%X,rn",
  615.   connectionInfo->DeviceDescriptor.iManufacturer,
  616.   connectionInfo->DeviceDescriptor.iSerialNumber);
  617.   ShowMessage(strShow);
  618.   if(connectionInfo->LowSpeed)
  619.   strShow="bus speed:low,rn";
  620.   else
  621.   strShow="bus speed:full,rn";
  622.   ShowMessage(strShow);
  623.   strShow.Format("Device Address:0x%X,rnOpen Pipes:0x%Xrn",
  624.   connectionInfo->DeviceAddress,connectionInfo->NumberOfOpenPipes);
  625.   ShowMessage(strShow);
  626. }
  627. PCHAR CTestUSBDlg::WideStrToMultiStr(PWCHAR WideStr)
  628. {
  629.    ULONG nBytes;
  630.     PCHAR MultiStr;
  631.     nBytes = WideCharToMultiByte(
  632.                  CP_ACP,
  633.                  0,
  634.                  WideStr,
  635.                  -1,
  636.                  NULL,
  637.                  0,
  638.                  NULL,
  639.                  NULL);
  640.     if (nBytes == 0)
  641.     {
  642.         return NULL;
  643.     }
  644.     MultiStr =(PCHAR) malloc(nBytes);
  645.     if (MultiStr == NULL)
  646.     {
  647.         return NULL;
  648.     }
  649.     nBytes = WideCharToMultiByte(
  650.                  CP_ACP,
  651.                  0,
  652.                  WideStr,
  653.                  -1,
  654.                  MultiStr,
  655.                  nBytes,
  656.                  NULL,
  657.                  NULL);
  658.     if (nBytes == 0)
  659.     {
  660.         free(MultiStr);
  661.         return NULL;
  662.     }
  663.     return MultiStr;
  664. }
  665. void CTestUSBDlg::ShowHidInfo()//不能打开USB鼠标。能打开USB游戏手柄等
  666. {
  667. CString strShow;
  668. strShow="正在查找系统中HID类的GUID标识...rn";
  669. ShowMessage(strShow);
  670. GUID guidHID;
  671. HidD_GetHidGuid(&guidHID);
  672. strShow="HID类的GUID标识为:rn";
  673. ShowMessage(strShow);
  674. strShow.Format("%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02xrn",
  675. guidHID.Data1,guidHID.Data2,guidHID.Data3,guidHID.Data4[0],
  676. guidHID.Data4[1],guidHID.Data4[2],guidHID.Data4[3],guidHID.Data4[4],
  677. guidHID.Data4[5],guidHID.Data4[6],guidHID.Data4[7]);
  678. ShowMessage(strShow);
  679. HDEVINFO hDevInfo = SetupDiGetClassDevs(&guidHID,NULL,0,
  680. DIGCF_PRESENT|DIGCF_DEVICEINTERFACE );
  681. if(hDevInfo==INVALID_HANDLE_VALUE)
  682. {
  683. ShowMessage("查找USB设备出错!rn");
  684. return;
  685. }
  686. else
  687. ShowMessage("找到USB设备.rn");
  688. UINT nIndex = 0;
  689. strShow= "正在查找可用的USB设备...rn";
  690. ShowMessage(strShow);
  691. SP_DEVICE_INTERFACE_DATA strtInterfaceData;
  692. strtInterfaceData.cbSize=sizeof(SP_DEVICE_INTERFACE_DATA);
  693. BOOL bSuccess ;
  694. DWORD index=0;
  695. CString temp;
  696. for(int i=0;i<10;i++)
  697. {
  698. bSuccess= SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,index,
  699. &strtInterfaceData);
  700. if (!bSuccess)
  701. {
  702. ShowMessage("查找USB设备出错!rn");
  703. break;
  704. }
  705. else
  706. {
  707. if(strtInterfaceData.Flags==SPINT_ACTIVE )
  708. {
  709. temp.Format("%d 连接 rn",index+1);
  710. ShowMessage(temp);
  711. ShowMore(hDevInfo,strtInterfaceData);
  712. }
  713. }
  714. index++;
  715. }
  716. temp.Format("总共%d个设备连接",index);
  717. ShowMessage(temp);
  718. }
  719. void CTestUSBDlg::ShowMore(HDEVINFO hDevInfo, SP_DEVICE_INTERFACE_DATA strtInterfaceData)
  720. {
  721. CString strShow;
  722. PSP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
  723. DWORD strSzie=0,requiesize=0;
  724. SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,NULL,0,&strSzie,NULL);
  725. requiesize=strSzie;
  726. strtDetailData=(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(requiesize);
  727. strtDetailData->cbSize=sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  728. SP_DEVINFO_DATA infodata;
  729. infodata.cbSize=sizeof(SP_DEVINFO_DATA);
  730. if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,
  731. strtDetailData,strSzie,&requiesize,NULL))
  732. {
  733. ShowMessage("查找设备路径时出错!");
  734. SetupDiDestroyDeviceInfoList(hDevInfo);
  735. free(strtDetailData);
  736. return;
  737. }
  738. strShow.Format("设备路径:rn%srn",strtDetailData->DevicePath);
  739. ShowMessage(strShow);
  740. ShowMessage("打开通信端口....rn");
  741. HANDLE hCom = CreateFile (
  742.                   strtDetailData->DevicePath,
  743.                   GENERIC_READ | GENERIC_WRITE,
  744.                   FILE_SHARE_READ | FILE_SHARE_WRITE,
  745.                   NULL, 
  746.                   OPEN_EXISTING, 0, 
  747.                   NULL);
  748. if (hCom == INVALID_HANDLE_VALUE)
  749. {
  750. ShowMessage("无法打开通信端口。不能读取设备信息!(设备连接可能不正确?)rn");
  751. SetupDiDestroyDeviceInfoList(hDevInfo);
  752. free(strtDetailData);
  753. return;
  754. }
  755. ShowMessage("打开通信端口成功!rn");
  756. HIDD_ATTRIBUTES strtAttrib;
  757. ShowMessage("正在读取设备的标识...rn");
  758. strtAttrib.Size=sizeof(HIDD_ATTRIBUTES);
  759. if (!HidD_GetAttributes(hCom,&strtAttrib))
  760. {
  761. ShowMessage("查询设备状态时出错!");
  762. CloseHandle(hCom);
  763. free(strtDetailData);
  764. return;
  765. }
  766. ShowMessage("已读取,如下所示:rn");
  767. strShow.Format("VendorID:0x%Xrn",strtAttrib.VendorID);
  768. ShowMessage(strShow);
  769. strShow.Format("ProductID:0x%Xrn",strtAttrib.ProductID);
  770. ShowMessage(strShow);
  771. strShow.Format("VersionNumber:0x%Xrn",strtAttrib.VersionNumber);
  772. ShowMessage(strShow);
  773. }
  774. BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc)
  775. {
  776. STORAGE_PROPERTY_QUERY Query;
  777. DWORD dwOutBytes;
  778. BOOL bResult;
  779. Query.PropertyId = StorageDeviceProperty;
  780. Query.QueryType = PropertyStandardQuery;
  781. bResult = ::DeviceIoControl(hDevice,
  782. IOCTL_STORAGE_QUERY_PROPERTY,
  783. &Query, sizeof(STORAGE_PROPERTY_QUERY),
  784. pDevDesc, pDevDesc->Size,
  785. &dwOutBytes,
  786. (LPOVERLAPPED)NULL);
  787. return bResult;
  788. }
  789. BOOL CTestUSBDlg::PreTranslateMessage(MSG* pMsg) 
  790. {
  791. // TODO: Add your specialized code here and/or call the base class
  792. return CDialog::PreTranslateMessage(pMsg);
  793. }
  794. char CTestUSBDlg::chFirstDriveFromMask(ULONG unitmask)
  795. {
  796.       char i;
  797.       for (i = 0; i < 26; ++i)  
  798.       {
  799.            if (unitmask & 0x1) 
  800. break;
  801.             unitmask = unitmask >> 1;
  802.       }
  803.     return (i + 'A');
  804. }
  805. void CTestUSBDlg::ShowUdisk()
  806. {
  807. int k = 0;
  808. DWORD MaxDriveSet, CurDriveSet;
  809. DWORD drive, drivetype;
  810. TCHAR szBuf[300];
  811. HANDLE hDevice;
  812. PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
  813. char* p;
  814. CString str;
  815. for(k=0; k<26; k++)
  816. szMoveDiskName[k] = '';
  817. k = 1;
  818. MaxDriveSet = CurDriveSet = 0;
  819. MaxDriveSet = GetLogicalDrives();
  820. CurDriveSet = MaxDriveSet;
  821. for ( drive = 0; drive < 32; ++drive )  
  822. {
  823. if ( MaxDriveSet & (1 << drive) )  
  824. {
  825. DWORD temp = 1<<drive;
  826. _stprintf( szDrvName, _T("%c:\"), 'A'+drive );
  827. if(GetDriveType( szDrvName )== DRIVE_REMOVABLE)
  828. {
  829. drivetype = DRVREMOVE;
  830. sprintf(szBuf, "\\?\%c:", 'A'+drive);
  831. hDevice = CreateFile(szBuf, GENERIC_READ,
  832. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
  833. if (hDevice != INVALID_HANDLE_VALUE)
  834. {
  835. pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1];
  836. pDevDesc->Size = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
  837. if(GetDisksProperty(hDevice, pDevDesc))
  838. {
  839. szMoveDiskName[k] = chFirstDriveFromMask(temp);
  840. szMoveDiskName[0]=k;
  841. k++;
  842. ShowMessage("rn找到U盘,名称:rn");
  843. p= (char*)pDevDesc;
  844. str = (pDevDesc->VendorIdOffset ? &p[pDevDesc->VendorIdOffset]:"(NULL)");
  845. ShowMessage(str);
  846. ShowMessage("rn产品ID:rn");
  847. str=(pDevDesc->ProductIdOffset ? &p[pDevDesc->ProductIdOffset]:"(NULL)");
  848. ShowMessage(str);
  849. ShowMessage("rn产品版本:rn");
  850. str = (pDevDesc->ProductRevisionOffset ? &p[pDevDesc->ProductRevisionOffset] : "(NULL)");
  851. ShowMessage(str);
  852. ShowMessage("rn序列号:rn");
  853. str = (pDevDesc->SerialNumberOffset ? &p[pDevDesc->SerialNumberOffset] : "(NULL)");
  854. ShowMessage(str);
  855. }
  856. delete pDevDesc;
  857. CloseHandle(hDevice);
  858. }
  859. }
  860. }
  861. }
  862. str.Format("rn U盘符:rn%s",&szMoveDiskName[1]);
  863. if(szMoveDiskName[0]==0)
  864. {
  865. str="rn没有U盘rn";
  866. m_showMessage.Empty();
  867. }
  868. ShowMessage(str);
  869. }