C++ 对所有 USB 设备使用 RegisterDeviceNotification()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/706352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:51:15  来源:igfitidea点击:

Use RegisterDeviceNotification() for ALL USB devices

c++windowsusbnotifications

提问by Adam Haile

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:

我目前有一些代码可以在 Windows 服务(用 C++ 编写)中设置连接的 USB HID 设备的通知。代码如下:

   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);

A notification is then received via the SERVICE_CONTROL_DEVICEEVENT event. (Remember, this is a Service so no WM_DEVICECHANGE).

然后通过 SERVICE_CONTROL_DEVICEEVENT 事件接收通知。(请记住,这是一项服务,因此没有 WM_DEVICECHANGE)。

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.

我以为我可以只在 RegisterDeviceNotification() 调用中指定 DEV_BROADCAST_DEVICEINTERFACE 标志,这样它就会覆盖 dbcc_classguid 并获取所有设备,但事实证明 Windows 2000 不支持该标志,这对我来说是一个破坏者。另外,我猜这将返回的不仅仅是 USB 设备。

How should I modify this to get allUSB devices, not just USB HID? Should it be as simple as just giving a different GUID? Is there even a GUID for all USB?

我应该如何修改它以获取所有USB 设备,而不仅仅是 USB HID?它应该像提供不同的 GUID 一样简单吗?甚至所有 USB 都有一个 GUID 吗?

采纳答案by user121584

Used GUID_DEVINTERFACE_USB_DEVICE (in "usbiodef.h") to watch for all USB devices.

使用 GUID_DEVINTERFACE_USB_DEVICE(在“usbiodef.h”中)来监视所有 USB 设备。

  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);

回答by snowcrash09

Have you tried GUID_DEVCLASS_USB? (defined in devguid.h, Windows SDK)

你试过GUID_DEVCLASS_USB吗?(在 devguid.h 中定义,Windows SDK)

Did you mean the DEVICE_NOTIFY_ALL_INTERFACE_CLASSESflag?

你说的是DEVICE_NOTIFY_ALL_INTERFACE_CLASSES国旗吗?

Also, I found the following article helpful - it's about device GUIDs vs interface GUIDs:

此外,我发现以下文章很有帮助 - 它是关于设备 GUID 与界面 GUID:

http://blogs.msdn.com/doronh/archive/2006/02/15/532679.aspx

http://blogs.msdn.com/doronh/archive/2006/02/15/532679.aspx