Use RegisterDeviceNotification() for ALL USB devices(对所有 USB 设备使用 RegisterDeviceNotification())
问题描述
我目前有一些代码可以在 Windows 服务(用 C++ 编写)中设置连接的 USB HID 设备的通知.代码如下:
I currently have some code that sets up notifications of connected USB HID devices within a Windows Service (written in C++). The code is as follows:
GUID hidGuid;
HidD_GetHidGuid(&hidGuid);
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = hidGuid;
HDEVNOTIFY deviceNotify = RegisterDeviceNotification(StatusHandle, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
然后通过 SERVICE_CONTROL_DEVICEEVENT 事件接收通知.(请记住,这是一项服务,因此没有 WM_DEVICECHANGE).
A notification is then received via the SERVICE_CONTROL_DEVICEEVENT event. (Remember, this is a Service so no WM_DEVICECHANGE).
我以为我可以在 RegisterDeviceNotification() 调用中指定 DEV_BROADCAST_DEVICEINTERFACE 标志,这样它就会覆盖 dbcc_classguid 并获取所有设备,但结果证明该标志在 Windows 2000 上不受支持,这对我来说是一个交易破坏者.此外,我猜这将返回的不仅仅是 USB 设备.
I thought I could just specify the DEV_BROADCAST_DEVICEINTERFACE flag in the RegisterDeviceNotification() call so it would override dbcc_classguid and get all devices, but it turns out that that flag is not supported on Windows 2000, which is a dealbreaker for me. Also, I'm guessing that that would return more than just USB devices.
我应该如何修改它以获取所有 USB 设备,而不仅仅是 USB HID?它应该像只提供不同的 GUID 一样简单吗?甚至所有 USB 都有一个 GUID 吗?
How should I modify this to get all USB devices, not just USB HID? Should it be as simple as just giving a different GUID? Is there even a GUID for all USB?
推荐答案
使用 GUID_DEVINTERFACE_USB_DEVICE(在usbiodef.h"中)来监视所有 USB 设备.
Used GUID_DEVINTERFACE_USB_DEVICE (in "usbiodef.h") to watch for all USB devices.
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbcc_size = sizeof(NotificationFilter);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_reserved = 0;
NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;
HDEVNOTIFY hDevNotify = RegisterDeviceNotification(hwnd, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
这篇关于对所有 USB 设备使用 RegisterDeviceNotification()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对所有 USB 设备使用 RegisterDeviceNotification()
基础教程推荐
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01