C# 检查设备更改(添加/删除)事件

C# 检查设备更改(添加/删除)事件,c#,.net,windows,C#,.net,Windows,我想知道在系统中添加或删除设备时是否有方法触发事件。我希望能够检测到是否添加了USB闪存驱动器、鼠标或其他任何东西。我试着四处搜索,但我找不到任何关于如何做到这一点的信息 有什么想法吗?如果您的应用程序中有一个窗口,您可以使用以下内容: using System; using System.Runtime.InteropServices; internal static class UsbNotification { public const int DbtDevicearrival

我想知道在系统中添加或删除设备时是否有方法触发事件。我希望能够检测到是否添加了USB闪存驱动器、鼠标或其他任何东西。我试着四处搜索,但我找不到任何关于如何做到这一点的信息


有什么想法吗?

如果您的应用程序中有一个窗口,您可以使用以下内容:

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

公认的答案非常好,但它仅适用于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;
    }
}
静态类设备化{
//https://msdn.microsoft.com/en-us/library/aa363480(v=vs.85).aspx
public const int DbtDeviceArrival=0x8000;//系统检测到新设备
public const int dbtdeviceremoveplete=0x8004;//设备已丢失
public const int DbtDevNodesChanged=0x0007;//设备已添加到系统或从系统中删除。
public const int WmDevicechange=0x0219;//设备更改事件
私有常量int dbtdevtypedeviceinterface=5;
//https://msdn.microsoft.com/en-us/library/aa363431(v=vs.85).aspx
私有const int设备通知所有接口类=4;
专用静态只读Guid GuidDevice=new Guid(“A5DCBF10-6530-11D2-901F-00C04FB951ED”);//USB设备
私有静态IntPtr notificationHandle;
/// 
///注册一个窗口,以便在设备插入或拔出时接收通知。
/// 
///接收通知的窗口的句柄。
///true仅适用于USB设备,false适用于所有设备。
公共静态无效注册表设备化(IntPtr windowHandle,bool usbOnly=false){
var dbi=新的DevBroadcastDeviceinterface{
DeviceType=DBTDevtypeDeviceInterface,
保留=0,
ClassGuid=GuidDevinterfaceUSBDevice,
Name=0
};
dbi.Size=Marshal.SizeOf(dbi);
IntPtr buffer=Marshal.AllocHGlobal(dbi.Size);
StructureToPtr(dbi,buffer,true);
notificationHandle=RegisterDeviceNotification(窗口句柄、缓冲区、usbOnly?0:设备通知所有接口类);
}
/// 
///注销设备通知窗口
/// 
公共静态无效注销设备身份验证(){
未注册设备通知(notificationHandle);
}
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
专用静态外部IntPtr注册表项设备(IntPtr收件人、IntPtr通知筛选器、int标志);
[DllImport(“user32.dll”)]
私有静态外部布尔注销设备化(IntPtr句柄);
[StructLayout(LayoutKind.Sequential)]
私有结构DevBroadcastDeviceinterface{
内部整数大小;
内部int设备类型;
保留内部int;
内部Guid类Guid;
内部简称;
}
}

调用
RegisterDeviceNotification
时,关键更改是
标志
参数(请参阅),如果将其设置为
4
而不是
0
将忽略
ClassGuid
参数,并为所有设备注册。

我来到这篇文章是为了一个比原始问题更具体的情况,因为我希望在添加或删除端口时得到通知。对于这种情况,答案要简单得多,并且不需要调用RegisterDeviceNotification:

DBT_DEVICEARRIVAL和DBT_DeviceMoveComplete事件是 自动广播到端口设备的所有顶级窗口。 因此,不必为其调用RegisterDeviceNotification 港口

因此,解决方案类似于:

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;
        }
    }
}
如上所述:

DBT_DEVICEARRIVAL和DBT_DeviceMoveComplete事件将自动广播到端口设备的所有顶级窗口。因此,不必为端口调用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;
        }
    }
}

这里是更好的版本,因为它可以获取端口名。 对于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;
};

如果没有窗口,则需要创建一个窗口。A工作得很好。您可以在WinForms中使用!!这很有效。我要把这个作为答案。现在我有了一些有效的东西,深入研究和理解它是如何工作的就容易多了。:)不过我有个问题。有没有办法获取添加的设备的相关信息?如产品ID或供应商ID?如何在windows服务上处理设备更改?请你解释一下。这个代码有效,但我得到了两次回调。。。如何避免这种情况?关于“IntPtr buffer”的问题,在调用RegisterDeviceNotification()之后,是否应该调用Marshal.FreeHGlobal(buffer)?如果不释放“buffer”,是否会出现内存泄漏?我刚刚使用此代码作为检测到音频设备被删除的基础。但是,我注意到,对于移除的设备类中的每个设备,都会调用处理程序。所以我有11个音频设备,我拔下一个设备,导致它被调用11次。这是预期的行为吗?@Jammer,这可能发生,因为所有设备都属于同一类别,并且音频设备有使用优先级。如果一个被删除了,所有的人都必须刷新才能得到新的序列。我在DEV#u BROADCAST#HDR中的字段周围添加了
#pragma warning disable CS0649
#pragma warning restore
,以消除警告:)我制作了一个适用于Windows、MacOS和Linux的NuGet数据包:您介意在代码中添加一些信息吗。例如,为什么这应该是一个“更好的版本”,因为它可以获得端口名,而所有其他解决方案都不能?
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;
        }
    }
}
    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;
};