USBPortDlg.cpp
上传用户:chinacm168
上传日期:2007-04-11
资源大小:193k
文件大小:7k
源码类别:

USB编程

开发平台:

Visual C++

  1. // USBPortDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "USBPort.h"
  5. #include "USBPortDlg.h"
  6. #include "setupapi.h"
  7. #include "hidsdi.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CAboutDlg dialog used for App About
  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. // Implementation
  29. protected:
  30. //{{AFX_MSG(CAboutDlg)
  31. //}}AFX_MSG
  32. DECLARE_MESSAGE_MAP()
  33. };
  34. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CAboutDlg)
  37. //}}AFX_DATA_INIT
  38. }
  39. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. //{{AFX_DATA_MAP(CAboutDlg)
  43. //}}AFX_DATA_MAP
  44. }
  45. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  46. //{{AFX_MSG_MAP(CAboutDlg)
  47. // No message handlers
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CUSBPortDlg dialog
  52. CUSBPortDlg::CUSBPortDlg(CWnd* pParent /*=NULL*/)
  53. : CDialog(CUSBPortDlg::IDD, pParent)
  54. {
  55. //{{AFX_DATA_INIT(CUSBPortDlg)
  56. // NOTE: the ClassWizard will add member initialization here
  57. //}}AFX_DATA_INIT
  58. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  59. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  60. }
  61. void CUSBPortDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CUSBPortDlg)
  65. // NOTE: the ClassWizard will add DDX and DDV calls here
  66. //}}AFX_DATA_MAP
  67. }
  68. BEGIN_MESSAGE_MAP(CUSBPortDlg, CDialog)
  69. //{{AFX_MSG_MAP(CUSBPortDlg)
  70. ON_WM_SYSCOMMAND()
  71. ON_WM_PAINT()
  72. ON_WM_QUERYDRAGICON()
  73. ON_BN_CLICKED(IDC_FIND, OnSearch)
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CUSBPortDlg message handlers
  78. BOOL CUSBPortDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. GetDlgItem(IDC_FIND)->SetFocus();
  102. return FALSE;  // return TRUE  unless you set the focus to a control
  103. }
  104. void CUSBPortDlg::OnSysCommand(UINT nID, LPARAM lParam)
  105. {
  106. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  107. {
  108. CAboutDlg dlgAbout;
  109. dlgAbout.DoModal();
  110. }
  111. else
  112. {
  113. CDialog::OnSysCommand(nID, lParam);
  114. }
  115. }
  116. // If you add a minimize button to your dialog, you will need the code below
  117. //  to draw the icon.  For MFC applications using the document/view model,
  118. //  this is automatically done for you by the framework.
  119. void CUSBPortDlg::OnPaint() 
  120. {
  121. if (IsIconic())
  122. {
  123. CPaintDC dc(this); // device context for painting
  124. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  125. // Center icon in client rectangle
  126. int cxIcon = GetSystemMetrics(SM_CXICON);
  127. int cyIcon = GetSystemMetrics(SM_CYICON);
  128. CRect rect;
  129. GetClientRect(&rect);
  130. int x = (rect.Width() - cxIcon + 1) / 2;
  131. int y = (rect.Height() - cyIcon + 1) / 2;
  132. // Draw the icon
  133. dc.DrawIcon(x, y, m_hIcon);
  134. }
  135. else
  136. {
  137. CDialog::OnPaint();
  138. }
  139. }
  140. // The system calls this to obtain the cursor to display while the user drags
  141. //  the minimized window.
  142. HCURSOR CUSBPortDlg::OnQueryDragIcon()
  143. {
  144. return (HCURSOR) m_hIcon;
  145. }
  146. void CUSBPortDlg::OnSearch() 
  147. {
  148. // TODO: Add your control notification handler code here
  149. m_strLog.Empty();
  150. m_strLog = _T("正在查找系统中HID类的GUID标识...rn");
  151. SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
  152. // 查找本系统中HID类的GUID标识
  153. GUID guidHID;
  154. HidD_GetHidGuid(&guidHID);
  155. m_strLog += _T("HID类的GUID标识为rn");
  156. CString strShow;
  157. strShow.Format("%08x-%04x-%04x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02xrn",
  158. guidHID.Data1,guidHID.Data2,guidHID.Data3,guidHID.Data4[0],
  159. guidHID.Data4[1],guidHID.Data4[2],guidHID.Data4[3],guidHID.Data4[4],
  160. guidHID.Data4[5],guidHID.Data4[6],guidHID.Data4[7]);
  161. m_strLog += strShow;
  162. SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
  163. // 准备查找符合HID规范的USB设备
  164. HDEVINFO hDevInfo = SetupDiGetClassDevs(&guidHID,NULL,0,
  165. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
  166. UINT nIndex = 0;
  167. m_strLog += _T("正在查找可用的USB设备...rn");
  168. SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
  169. // 查找USB设备接口
  170. SP_DEVICE_INTERFACE_DATA strtInterfaceData;
  171. BOOL bSuccess = SetupDiEnumDeviceInterfaces(hDevInfo,NULL,&guidHID,0,
  172. &strtInterfaceData);
  173. if (!bSuccess)
  174. {
  175. m_strLog += _T("抱歉,未找到可用的USB设备");
  176. SetupDiDestroyDeviceInfoList(hDevInfo);
  177. return;
  178. }
  179. // 若找到了设备,则读取设备路径名
  180. SP_DEVICE_INTERFACE_DETAIL_DATA strtDetailData;
  181. if (!SetupDiGetDeviceInterfaceDetail(hDevInfo,&strtInterfaceData,
  182. &strtDetailData,sizeof(strtDetailData),NULL,NULL))
  183. {
  184. AfxMessageBox(_T("查找设备路径时出错!"));
  185. SetupDiDestroyDeviceInfoList(hDevInfo);
  186. return;
  187. }
  188. // 找到了设备路径
  189. m_strPath = strtDetailData.DevicePath;
  190. strShow.Format("可以通过路径%s访问设备rn",m_strPath);
  191. m_strLog += strShow;
  192. SetDlgItemText(IDC_EDIT_REPORT,m_strLog);
  193. // 开放与设备的通信
  194. HANDLE hCom = CreateFile(m_strPath,GENERIC_READ | GENERIC_WRITE,
  195. 0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  196. if (hCom = INVALID_HANDLE_VALUE)
  197. {
  198. AfxMessageBox(_T("无法打开通信端口"));
  199. SetupDiDestroyDeviceInfoList(hDevInfo);
  200. return;
  201. }
  202. // 查询设备标识
  203. HIDD_ATTRIBUTES strtAttrib;
  204. m_strPath += _T("正在读取设备的标识...rn");
  205. SetDlgItemText(IDC_EDIT_REPORT,m_strPath);
  206. if (!HidD_GetAttributes(hCom,&strtAttrib))
  207. {
  208. AfxMessageBox(_T("查询设备状态时出错!"));
  209. CloseHandle(hCom);
  210. SetupDiDestroyDeviceInfoList(hDevInfo);
  211. }
  212. // 将有关该设备的标识显示出来
  213. m_strPath += _T("已读取,见右边所示rn");
  214. SetDlgItemText(IDC_EDIT_REPORT,m_strPath);
  215. // 显示供应商标识
  216. strShow.Format("0x%x",strtAttrib.VendorID);
  217. SetDlgItemText(IDC_ID_VENDOR,strShow);
  218. // 显示产品标识
  219. strShow.Format("0x%x",strtAttrib.ProductID);
  220. SetDlgItemText(IDC_ID_PRODUCT,strShow);
  221. // 显示产品版本号
  222. strShow.Format("%d",strtAttrib.VersionNumber);
  223. SetDlgItemText(IDC_ID_VERSION,strShow);
  224. // 释放资源
  225. CloseHandle(hCom);
  226. SetupDiDestroyDeviceInfoList(hDevInfo);
  227. }