MFC USBHID

上传者: ken0927 | 上传时间: 2019-12-21 20:03:08 | 文件大小: 30.04MB | 文件类型: rar
USB
**正文** 在Windows操作系统开发中,MFC(Microsoft Foundation Classes)是C++库的一个重要组成部分,它为构建桌面应用程序提供了一种结构化的框架。而USB HID(Human Interface Device)是USB设备类规范的一种,主要用于人机交互设备,如键盘、鼠标、游戏控制器等。本文将深入探讨如何使用MFC来实现对USB HID设备的读写操作。 我们需要理解USB HID的基本概念。HID设备通过使用HID报告来与主机通信,这些报告包含了设备状态和用户输入的数据。HID类驱动程序是操作系统的一部分,负责解析和处理这些报告。开发者无需编写驱动程序,只需与设备的接口进行交互即可。 在MFC环境下,我们可以使用`CreateFile`函数打开USB HID设备,其参数通常包括设备的设备路径,例如`\\?\usb#vid_XXXX&pid_YYYY#...`,这里的`XXXX`和`YYYY`分别是设备的供应商ID和产品ID。接着,我们调用`DeviceIoControl`函数来进行读写操作,传递适当的控制代码,如`IOCTL_HID_GET_REPORT`或`IOCTL_HID_SET_REPORT`。 为了更方便地管理USB HID设备,我们可以创建一个MFC类来封装这些系统调用。这个类可以包含成员变量,如设备句柄、设备描述符和报告ID,以及成员函数,如`OpenDevice`、`ReadReport`、`WriteReport`和`CloseDevice`。以下是一个简单的MFC类设计示例: ```cpp class CHIDDevice : public CObject { public: CHIDDevice(); ~CHIDDevice(); bool OpenDevice(LPCTSTR devicePath); void CloseDevice(); bool ReadReport(void* buffer, DWORD size); bool WriteReport(void* buffer, DWORD size); private: HANDLE m_hDevice; }; ``` 在`OpenDevice`中,我们执行`CreateFile`,在`CloseDevice`中关闭句柄。`ReadReport`和`WriteReport`则分别使用`DeviceIoControl`进行读写操作,传递适当的缓冲区和大小。 在实际应用中,我们还需要处理USB HID设备的枚举和选择。可以遍历`SetupDiGetClassDevs`返回的设备信息集,获取HID设备的详细信息,并根据需求选择合适的设备。此外,为了处理异步读写,可以使用MFC的消息机制,如消息队列和消息映射,或者使用CAsyncSocket或CAsyncMonikerFile等异步I/O类。 利用MFC开发USB HID应用涉及以下几个关键步骤: 1. **设备枚举**:使用`SetupDiGetClassDevs`枚举HID设备,通过`SetupDiEnumDeviceInfo`获取设备详细信息。 2. **设备连接**:使用`CreateFile`打开设备,获得设备句柄。 3. **读写操作**:通过`DeviceIoControl`进行数据交换,读取或设置HID报告。 4. **错误处理**:适当处理可能的错误,如设备未找到、访问权限问题等。 5. **异步处理**:根据需要,使用MFC的消息机制实现异步读写。 通过以上步骤,开发者可以构建一个功能完备的MFC应用程序,实现对USB HID设备的高效控制。在实际项目中,还可以考虑添加设备事件监听、多设备管理等功能,以提升应用的灵活性和可扩展性。

文件下载

资源详情

[{"title":"( 74 个子文件 30.04MB ) MFC USBHID","children":[{"title":"HIDtest","children":[{"title":"testhidDlg.h <span style='color:#111;'> 1.57KB </span>","children":null,"spread":false},{"title":"testhid.rc <span style='color:#111;'> 5.71KB </span>","children":null,"spread":false},{"title":"testhid.vcxproj <span style='color:#111;'> 7.81KB </span>","children":null,"spread":false},{"title":"SETUPAPI.H <span style='color:#111;'> 211.00KB </span>","children":null,"spread":false},{"title":"testhid.PS <span style='color:#111;'> 17.63KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"cl.command.1.tlog <span style='color:#111;'> 2.59KB </span>","children":null,"spread":false},{"title":"testhid.Build.CppClean.log <span style='color:#111;'> 2.58KB </span>","children":null,"spread":false},{"title":"testhid.obj <span style='color:#111;'> 26.14KB </span>","children":null,"spread":false},{"title":"testhid.exe.embed.manifest <span style='color:#111;'> 667B </span>","children":null,"spread":false},{"title":"rc.command.1.tlog <span style='color:#111;'> 1.27KB </span>","children":null,"spread":false},{"title":"CL.read.1.tlog <span style='color:#111;'> 31.03KB </span>","children":null,"spread":false},{"title":"vc100.idb <span style='color:#111;'> 1.00MB </span>","children":null,"spread":false},{"title":"mt.read.1.tlog <span style='color:#111;'> 548B </span>","children":null,"spread":false},{"title":"testhid_manifest.rc <span style='color:#111;'> 210B </span>","children":null,"spread":false},{"title":"link-cvtres.read.1.tlog <span style='color:#111;'> 2B </span>","children":null,"spread":false},{"title":"link.read.1.tlog <span style='color:#111;'> 5.50KB </span>","children":null,"spread":false},{"title":"StdAfx.obj <span style='color:#111;'> 541.17KB </span>","children":null,"spread":false},{"title":"rc.read.1.tlog <span style='color:#111;'> 3.23KB </span>","children":null,"spread":false},{"title":"link.write.1.tlog <span style='color:#111;'> 1.50KB </span>","children":null,"spread":false},{"title":"CL.write.1.tlog <span style='color:#111;'> 1.85KB </span>","children":null,"spread":false},{"title":"testhid.pdb <span style='color:#111;'> 3.72MB </span>","children":null,"spread":false},{"title":"vc100.pdb <span style='color:#111;'> 2.20MB </span>","children":null,"spread":false},{"title":"testhid.exe.embed.manifest.res <span style='color:#111;'> 732B </span>","children":null,"spread":false},{"title":"testhid.res <span style='color:#111;'> 2.80KB </span>","children":null,"spread":false},{"title":"link.command.1.tlog <span style='color:#111;'> 1.85KB </span>","children":null,"spread":false},{"title":"mt.write.1.tlog <span style='color:#111;'> 570B </span>","children":null,"spread":false},{"title":"testhid.ilk <span style='color:#111;'> 1.01MB </span>","children":null,"spread":false},{"title":"testhid.pch <span style='color:#111;'> 19.44MB </span>","children":null,"spread":false},{"title":"testhid.exe.intermediate.manifest <span style='color:#111;'> 381B </span>","children":null,"spread":false},{"title":"testhid.exe <span style='color:#111;'> 76.50KB </span>","children":null,"spread":false},{"title":"testhid.lastbuildstate <span style='color:#111;'> 89B </span>","children":null,"spread":false},{"title":"testhidDlg.obj <span style='color:#111;'> 56.19KB </span>","children":null,"spread":false},{"title":"mt.command.1.tlog <span style='color:#111;'> 774B </span>","children":null,"spread":false},{"title":"rc.write.1.tlog <span style='color:#111;'> 694B </span>","children":null,"spread":false},{"title":"link-cvtres.write.1.tlog <span style='color:#111;'> 2B </span>","children":null,"spread":false},{"title":"testhid.log <span style='color:#111;'> 4.25KB </span>","children":null,"spread":false}],"spread":false},{"title":"testhidDlg.cpp <span style='color:#111;'> 10.97KB </span>","children":null,"spread":false},{"title":"hidsdi.h <span style='color:#111;'> 14.89KB </span>","children":null,"spread":false},{"title":"testhid.opt <span style='color:#111;'> 54.50KB </span>","children":null,"spread":false},{"title":"ipch","children":[{"title":"testhid-a187abe3","children":[{"title":"testhid-4b5744ab.ipch <span style='color:#111;'> 66.63MB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"testhid.IMD <span style='color:#111;'> 368B </span>","children":null,"spread":false},{"title":"testhid.IAD <span style='color:#111;'> 408B </span>","children":null,"spread":false},{"title":"testhid.WK3 <span style='color:#111;'> 24.99KB </span>","children":null,"spread":false},{"title":"testhid.dsw <span style='color:#111;'> 537B </span>","children":null,"spread":false},{"title":"testhid.h <span style='color:#111;'> 1.30KB </span>","children":null,"spread":false},{"title":"hidusage.h <span style='color:#111;'> 11.94KB </span>","children":null,"spread":false},{"title":"testhid.ncb <span style='color:#111;'> 65.00KB </span>","children":null,"spread":false},{"title":"testhid.plg <span style='color:#111;'> 1.49KB </span>","children":null,"spread":false},{"title":"ReadMe.txt <span style='color:#111;'> 3.51KB </span>","children":null,"spread":false},{"title":"res","children":[{"title":"testhid.rc2 <span style='color:#111;'> 399B </span>","children":null,"spread":false},{"title":"testhid.ico <span style='color:#111;'> 1.05KB </span>","children":null,"spread":false},{"title":"Thumbs.db <span style='color:#111;'> 3.00KB </span>","children":null,"spread":false}],"spread":false},{"title":"hid.lib <span style='color:#111;'> 11.87KB </span>","children":null,"spread":false},{"title":"testhid.clw <span style='color:#111;'> 1.30KB </span>","children":null,"spread":false},{"title":"testhid.IMB <span style='color:#111;'> 4.00KB </span>","children":null,"spread":false},{"title":"Resource.h <span style='color:#111;'> 869B </span>","children":null,"spread":false},{"title":"StdAfx.cpp <span style='color:#111;'> 209B </span>","children":null,"spread":false},{"title":"testhid.vcxproj.filters <span style='color:#111;'> 2.23KB </span>","children":null,"spread":false},{"title":"hidpi.h <span style='color:#111;'> 79.09KB </span>","children":null,"spread":false},{"title":"testhid.dsp <span style='color:#111;'> 4.12KB </span>","children":null,"spread":false},{"title":"testhid.PFI <span style='color:#111;'> 36B </span>","children":null,"spread":false},{"title":"testhid.vcxproj.user <span style='color:#111;'> 143B </span>","children":null,"spread":false},{"title":"testhid.PO <span style='color:#111;'> 776B </span>","children":null,"spread":false},{"title":"testhid.suo <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"testhid.aps <span style='color:#111;'> 20.56KB </span>","children":null,"spread":false},{"title":"testhid.IAB <span style='color:#111;'> 8.00KB </span>","children":null,"spread":false},{"title":"StdAfx.h <span style='color:#111;'> 1.03KB </span>","children":null,"spread":false},{"title":"testhid.sln <span style='color:#111;'> 880B </span>","children":null,"spread":false},{"title":"testhid.SearchResults <span style='color:#111;'> 313B </span>","children":null,"spread":false},{"title":"testhid.cpp <span style='color:#111;'> 2.03KB </span>","children":null,"spread":false},{"title":"testhid.PR <span style='color:#111;'> 3.24KB </span>","children":null,"spread":false},{"title":"testhid.sdf <span style='color:#111;'> 52.58MB </span>","children":null,"spread":false},{"title":"setupapi.lib <span style='color:#111;'> 148.14KB </span>","children":null,"spread":false},{"title":"testhid.PRI <span style='color:#111;'> 18.29KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

  • shibomimi :
    可运行,有一定的启发意义
    2014-06-19
  • isi408 :
    资源相当丰富,值得下载,呵呵!!!
    2014-03-23

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明