前言
usb也是目前常出现的设备接口类型。本篇是从需求的接口出发,目前分如下三类
- hidapi
- win32
- libusb
需求方面,基本功能是连接和读写数据,进阶需求例如热插拔
本文不详细介绍测试示例,仅列举一些开发过程中的笔记
HIDAPI
source
LIBUSB
//待更新
Win32
文档
doc - CM_Register_Notification function
doc - RegisterDeviceNotificationA function (winuser.h)
Use RegisterDeviceNotificationA instead of CM_Register_Notification if your code targets Windows 7 or earlier versions of Windows.
You can use CM_Register_Notification instead of RegisterDeviceNotification if your code targets Windows 8 or newer versions of Windows. The advantage of CM_Register_Notification is that it does not require a window handle to work.
Note
对USB的支持,换而言之是在检测硬件设备的改变。Windows操作系统会对上层程序发送WM_DEVICECHANGE消息来通知设备的改变,对应的,要实现一个数据处理的事件。
LRESULT CALLBACK DevPluginInterface::windowMessageProcess(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_DEVICECHANGE)
{
//do something
}
}
使用接口 RegisterDeviceNotification 要注意创建一个窗口句柄(另一个不用,但我还没测试)
hwnd = ::CreateWindowW(wc.lpszClassName, // classname
wc.lpszClassName, // window name
0, // style
0, // x
0, // y
0, // width
0, // height
0, // parent
0, // menu handle
hi, // application
0); // windows creation data.