检查设备更改(添加/删除)事件
Posted
技术标签:
【中文标题】检查设备更改(添加/删除)事件【英文标题】:Check for device change (add/remove) events 【发布时间】:2021-09-07 04:18:30 【问题描述】:我想知道在系统中添加或删除设备时是否可以触发事件。我希望能够检测是否添加了 USB 闪存驱动器、鼠标或其他任何东西。我尝试四处搜索,但找不到任何说明如何执行此操作的内容。
有什么想法吗?
【问题讨论】:
我制作了一个适用于 Windows、MacOS 和 Linux 的 NuGet 数据包:github.com/Jinjinov/Usb.Events 【参考方案1】:如果您的应用程序中有一个窗口,您可以使用如下内容:
using System;
using System.Runtime.InteropServices;
internal static class UsbNotification
public const int DbtDevicearrival = 0x8000; // system detected a new device
public const int DbtDeviceremovecomplete = 0x8004; // device is gone
public const int WmDevicechange = 0x0219; // device change event
private const int DbtDevtypDeviceinterface = 5;
private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices
private static IntPtr notificationHandle;
/// <summary>
/// Registers a window to receive notifications when USB devices are plugged or unplugged.
/// </summary>
/// <param name="windowHandle">Handle to the window receiving notifications.</param>
public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
DeviceType = DbtDevtypDeviceinterface,
Reserved = 0,
ClassGuid = GuidDevinterfaceUSBDevice,
Name = 0
;
dbi.Size = Marshal.SizeOf(dbi);
IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
Marshal.StructureToPtr(dbi, buffer, true);
notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
/// <summary>
/// Unregisters the window for USB device notifications
/// </summary>
public static void UnregisterUsbDeviceNotification()
UnregisterDeviceNotification(notificationHandle);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);
[DllImport("user32.dll")]
private static extern bool UnregisterDeviceNotification(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct DevBroadcastDeviceinterface
internal int Size;
internal int DeviceType;
internal int Reserved;
internal Guid ClassGuid;
internal short Name;
在 WPF 窗口中使用它的方法如下(Windows 窗体类似):
protected override void OnSourceInitialized(EventArgs e)
base.OnSourceInitialized(e);
// Adds the windows message processing hook and registers USB device add/removal notification.
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if (source != null)
windowHandle = source.Handle;
source.AddHook(HwndHandler);
UsbNotification.RegisterUsbDeviceNotification(windowHandle);
/// <summary>
/// Method that receives window messages.
/// </summary>
private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
if (msg == UsbNotification.WmDevicechange)
switch ((int)wparam)
case UsbNotification.DbtDeviceremovecomplete:
Usb_DeviceRemoved(); // this is where you do your magic
break;
case UsbNotification.DbtDevicearrival:
Usb_DeviceAdded(); // this is where you do your magic
break;
handled = false;
return IntPtr.Zero;
这是 Windows 窗体的使用示例(更简单):
public Form1()
InitializeComponent();
UsbNotification.RegisterUsbDeviceNotification(this.Handle);
protected override void WndProc(ref Message m)
base.WndProc(ref m);
if (m.Msg == UsbNotification.WmDevicechange)
switch ((int)m.WParam)
case UsbNotification.DbtDeviceremovecomplete:
Usb_DeviceRemoved(); // this is where you do your magic
break;
case UsbNotification.DbtDevicearrival:
Usb_DeviceAdded(); // this is where you do your magic
break;
【讨论】:
如果您没有窗口,则需要创建一个。 message-only window 工作得很好。您可以在 WinForms 中使用NativeWindow
class。
哇!!这样可行。我不会将此标记为答案。现在我有了一些可以工作的东西,更容易深入了解它是如何工作的。 :) 我确实有一个问题。有没有办法获取有关已添加设备的一些信息?比如产品 ID 或供应商 ID?
如何处理 Windows 服务上的设备更改?请你解释一下。
此代码有效,但我收到了两次回调...如何避免这种情况?
关于'IntPtr buffer'的问题,我们应该在调用RegisterDeviceNotification()之后调用Marshal.FreeHGlobal(buffer)吗?如果不释放,“缓冲区”会发生内存泄漏吗?【参考方案2】:
公认的答案非常好,但它仅适用于 USB 设备。
要使其适用于所有设备(并可选择过滤 USB),请使用以下稍作修改的类:
static class DeviceNotification
//https://msdn.microsoft.com/en-us/library/aa363480(v=vs.85).aspx
public const int DbtDeviceArrival = 0x8000; // system detected a new device
public const int DbtDeviceRemoveComplete = 0x8004; // device is gone
public const int DbtDevNodesChanged = 0x0007; //A device has been added to or removed from the system.
public const int WmDevicechange = 0x0219; // device change event
private const int DbtDevtypDeviceinterface = 5;
//https://msdn.microsoft.com/en-us/library/aa363431(v=vs.85).aspx
private const int DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = 4;
private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices
private static IntPtr notificationHandle;
/// <summary>
/// Registers a window to receive notifications when devices are plugged or unplugged.
/// </summary>
/// <param name="windowHandle">Handle to the window receiving notifications.</param>
/// <param name="usbOnly">true to filter to USB devices only, false to be notified for all devices.</param>
public static void RegisterDeviceNotification(IntPtr windowHandle, bool usbOnly = false)
var dbi = new DevBroadcastDeviceinterface
DeviceType = DbtDevtypDeviceinterface,
Reserved = 0,
ClassGuid = GuidDevinterfaceUSBDevice,
Name = 0
;
dbi.Size = Marshal.SizeOf(dbi);
IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
Marshal.StructureToPtr(dbi, buffer, true);
notificationHandle = RegisterDeviceNotification(windowHandle, buffer, usbOnly ? 0 : DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
/// <summary>
/// Unregisters the window for device notifications
/// </summary>
public static void UnregisterDeviceNotification()
UnregisterDeviceNotification(notificationHandle);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);
[DllImport("user32.dll")]
private static extern bool UnregisterDeviceNotification(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct DevBroadcastDeviceinterface
internal int Size;
internal int DeviceType;
internal int Reserved;
internal Guid ClassGuid;
internal short Name;
关键变化是调用RegisterDeviceNotification
时的Flags
参数(参见https://msdn.microsoft.com/en-us/library/aa363431(v=vs.85).aspx),如果设置为4
而不是0
,将忽略ClassGuid
参数并注册所有设备。
【讨论】:
我刚刚使用此代码作为检测到音频设备被移除的基础。但是,我注意到处理程序会为已删除设备类中的每个设备调用。所以我有 11 个音频设备,我拔掉一个设备,这会导致它被调用 11 次。这是预期的行为吗? @Jammer,这可能是因为所有设备都属于同一类别并且音频设备具有使用优先级。如果删除了一个,则必须刷新才能获得新序列。【参考方案3】:我来这篇文章是为了比原来的问题更具体,因为我希望在添加或删除端口时收到通知。对于这种情况,答案要简单得多,不需要调用 RegisterDeviceNotification:
DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 事件是 自动广播到端口设备的所有***窗口。 因此,无需调用 RegisterDeviceNotification 港口......
https://docs.microsoft.com/en-us/windows/desktop/api/Winuser/nf-winuser-registerdevicenotificationa
所以解决方案变成这样:
using System.Runtime.InteropServices;
//Put all of the following code inside your Form's partial class:
private struct DEV_BROADCAST_HDR
internal UInt32 dbch_size;
internal UInt32 dbch_devicetype;
internal UInt32 dbch_reserved;
;
protected override void WndProc(ref Message m)
base.WndProc(ref m); //This allows window default behavior of base class to be executed
if (m.Msg == 0x0219) //WM_DEVICECHANGE = 0x0219
DEV_BROADCAST_HDR dbh;
switch ((int)m.WParam)
case 0x8000: //DBT_DEVICEARRIVAL = 0x8000
dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbh.dbch_devicetype == 0x00000003) //DBT_DEVTYP_PORT = 0x00000003
Console.WriteLine("Port added!");
//TODO
break;
case 0x8004: //DBT_DEVICEREMOVECOMPLETE = 0x8004
dbh = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if (dbh.dbch_devicetype == 0x00000003) //DBT_DEVTYP_PORT = 0x00000003
Console.WriteLine("Port removed!");
//TODO
break;
【讨论】:
伙计,这太棒了。我在 DEV_BROADCAST_HDR 的字段周围添加了#pragma warning disable CS0649
和 #pragma warning restore
以使警告静音:)【参考方案4】:
这里是更好的版本,因为它可以获取端口名称。 对于 DBT_DEVTYP_PORT 设备类型,lParam 指向 DEV_BROADCAST_PORT,其中包含 DEV_BROADCAST_HDR,然后以 Unicode 格式命名要删除或添加的设备,以零结尾。
protected override void WndProc(ref Message m)
switch ((WndMessage) m.Msg)
case WndMessage.WM_DEVICECHANGE:
DEV_BROADCAST_HDR dbh;
switch ((WM_DEVICECHANGE) m.WParam)
case WM_DEVICECHANGE.DBT_DEVICEARRIVAL:
case WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE:
dbh = (DEV_BROADCAST_HDR) Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
if ((WM_DEVICECHANGE) dbh.dbch_devicetype == WM_DEVICECHANGE.DBT_DEVTYP_PORT)
var portNameBytes = new byte[dbh.dbch_size - (int) WM_DEVICECHANGE.SIZE_OF_DBH];
Marshal.Copy(m.LParam + (int) WM_DEVICECHANGE.SIZE_OF_DBH, portNameBytes, 0, portNameBytes.Length);
string portName = Encoding.Unicode.GetString(portNameBytes).TrimEnd('\0');
if (portName == Settings.Instance.PortName)
if ((WM_DEVICECHANGE) m.WParam == WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE)
if (!_port.IsOpen)
ClosePort();
else
BeginInvoke((Action) (() => OpenPort()));
break;
break;
base.WndProc(ref m);
public enum WndMessage
WM_DEVICECHANGE = 0x0219, // device change event
public enum WM_DEVICECHANGE
// full list: https://docs.microsoft.com/en-us/windows/win32/devio/wm-devicechange
DBT_DEVICEARRIVAL = 0x8000, // A device or piece of media has been inserted and is now available.
DBT_DEVICEREMOVECOMPLETE = 0x8004, // A device or piece of media has been removed.
DBT_DEVTYP_DEVICEINTERFACE = 0x00000005, // Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
DBT_DEVTYP_HANDLE = 0x00000006, // File system handle. This structure is a DEV_BROADCAST_HANDLE structure.
DBT_DEVTYP_OEM = 0x00000000, // OEM- or IHV-defined device type. This structure is a DEV_BROADCAST_OEM structure.
DBT_DEVTYP_PORT = 0x00000003, // Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT structure.
DBT_DEVTYP_VOLUME = 0x00000002, // Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.
SIZE_OF_DBH = 12, // sizeof(DEV_BROADCAST_HDR)
public struct DEV_BROADCAST_HDR
internal UInt32 dbch_size;
internal UInt32 dbch_devicetype;
internal UInt32 dbch_reserved;
;
【讨论】:
您是否介意在代码中添加一些信息。例如,为什么这应该是一个“更好的版本” 因为它可以获取端口名称,而所有其他解决方案都没有?【参考方案5】:正如DatuPuti 提到的:
DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 事件会自动广播到端口设备的所有***窗口。因此,不需要为端口调用RegisterDeviceNotification
所以我采用了他的实现并将其改编为 Windows 窗体:
using System;
using System.Runtime.InteropServices;
private enum WM_DEVICECHANGE
// full list: https://docs.microsoft.com/en-us/windows/win32/devio/wm-devicechange
DBT_DEVICEARRIVAL = 0x8000, // A device or piece of media has been inserted and is now available.
DBT_DEVICEREMOVECOMPLETE = 0x8004, // A device or piece of media has been removed.
private int WmDevicechange = 0x0219; // device change event
protected override void WndProc(ref Message m)
base.WndProc(ref m); //This allows window default behavior of base class to be executed
if (m.Msg == WmDevicechange)
switch ((WM_DEVICECHANGE)m.WParam)
case WM_DEVICECHANGE.DBT_DEVICEREMOVECOMPLETE:
Console.WriteLine("USB Device removed");
break;
case WM_DEVICECHANGE.DBT_DEVICEARRIVAL:
Console.WriteLine("USB Device added");
break;
【讨论】:
【参考方案6】:如果您没有 Window,此解决方案会创建一个没有类名及其句柄的通用窗口:
using System;
using System.Windows.Forms;
internal class UsbService : NativeWindow, IDisposable
internal UsbService(IEventAggregator eventAggregator)
_eventAggregator = eventAggregator;
base.CreateHandle(new CreateParams());
protected override void WndProc(ref Message msg)
base.WndProc(ref msg);
if (msg.Msg == 0x0219) // Device change event
switch (msg.WParam.ToInt32())
case 0x8000: // Device added
case 0x8004: // Device removed
_eventAggregator.Publish(...);
break;
public void Dispose()
if (!_isDisposed)
base.DestroyHandle();
_isDisposed = true;
GC.SuppressFinalize(this);
private bool _isDisposed;
private IEventAggregator _eventAggregator;
我认为在 WPF 项目中使用它会更干净。在这个例子中,我使用 DI 注入了一个事件聚合器,它将发布一个我在不同类中订阅的事件。这可以删除并替换为事件以通知更改。要获得有关连接卷的基本和快速信息,我会使用 DriveInfo.GetDrives() 方法。
【讨论】:
以上是关于检查设备更改(添加/删除)事件的主要内容,如果未能解决你的问题,请参考以下文章