对所有 USB 设备使用 RegisterDeviceNotification()

2023-12-02C/C++开发问题
5

本文介绍了对所有 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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6