对所有USB设备使用RegisterDeviceNotification() [英] Use RegisterDeviceNotification() for ALL USB devices
问题描述
我目前有一些代码,用于在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,这是一个我的交易崩溃。
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?
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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!