How can I get notifications when a USB device is connected?

I am writing a program to monitor a specific device. This device may or may not always be connected, and when connected, it can be connected to any of several different ports; I would like my program to handle this gracefully.

Is there a way to receive notifications when a specific USB device is connected, and from there to determine which port it is connected to?

+3
source share
2 answers

Mark this topic on SO. In particular, the accepted answer that uses SharpUSBLib .

+1
source

, - , :

/// <summary>
/// Windows Messages
/// Defined in winuser.h from Windows SDK v6.1
/// Documentation pulled from MSDN.
/// For more look at: http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html
/// </summary>
public enum WM : uint
{
    /// <summary>
    /// Notifies an application of a change to the hardware configuration of a device or the computer.
    /// </summary>
    DEVICECHANGE = 0x0219,
}

protected override void WndProc(ref Message m)
{
    switch ((WM)m.Msg)
    {
        case WM.DEVICECHANGE:
            //ToDo: put your code here.
            break;
    }
    base.WndProc(ref m);
}
+1

Source: https://habr.com/ru/post/1706787/


All Articles