对所有 USB 设备使用 RegisterDeviceNotification() [英] Use RegisterDeviceNotification() for ALL USB devices

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆