【问题标题】:How to restart USB Device without exiting Application如何在不退出应用程序的情况下重新启动 USB 设备
【发布时间】:2018-10-07 06:52:46
【问题描述】:

在我的应用程序中,我使用 USB 设备进行某些操作,有时会发生 USB 断开并再次连接,之后我无法从我的应用程序中使用该设备并继续使用它,我需要重新启动应用程序, 我如何在不重新启动应用程序的情况下使用它?

经过一些帮助,我通过添加代码处理了插入/拔出设备时的事件”

    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:
                MessageBox.Show(m.ToString(), "USB Detected",MessageBoxButtons.OK,MessageBoxIcon.Information);
                break;
        }
        base.WndProc(ref m);
    }

输出是:

{msg=0x219 (WM_DEVICECHANGE) hwnd=0x90242 wparam=0x7 lparam=0x0 结果=0x0}

问题是我需要更多信息来表明它是正确的设备

试图为此目的使用 SharpUSBLib dll 没有成功,我可以使用什么来达到这个目的?

谢谢

【问题讨论】:

  • @GSerg - 它可以帮助,谢谢
  • 你不知道你在什么设备上打开了手柄吗?
  • 您从该链接获得的代码中有几个严重错误。最糟糕的是插入base.WndProc(ref m);after switch ((WM)m.Msg)。这将使任何结果静音。您必须确定消息的类型。在wParam 中传递了一堆不同的状态。您对uint DBT_DEVICEARRIVAL = 0x8000;uint DBT_DEVICEREMOVECOMPLETE = 0x8004; 感兴趣。 lParam 将报告:1) 设备类型(您可能对 uint DBT_DEVTYP_VOLUME = 0x0002; 感兴趣)2) 逻辑磁盘 ID(驱动器号)=> @ 987654331@ (...)
  • => uint DeviceIDNumber = Marshal.ReadInt32(m.LParam, 12); char DeviceLetter = (char)(65 + (int)Math.Log(DeviceIDNumber , 2)); 您可以使用我链接的问题中的代码(测试和工作)。另外,获取有关 USB 设备的详细信息:Get serial number of usb storage device。如果您认为需要写下WndProc 检测样式代码,请告诉我。

标签: c# winforms unsafe


【解决方案1】:

解决方案之一

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    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:

                var usbDevices = GetUSBDevices();
                txtInfo.Text = string.Empty;

                foreach (var usbDevice in usbDevices)
                {
                    if (usbDevice.Name.Contains("Name of my usb device"))
                    {
                        // Code ..
                    }
                }
                break;
        }
        base.WndProc(ref m);
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        ManagementObjectCollection collection;
        using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnPEntity where DeviceID Like ""USB%"""))
            collection = searcher.Get();

        foreach (var device in collection)
        {
            devices.Add(new USBDeviceInfo(
            (string)device.GetPropertyValue("Name"),
            (string)device.GetPropertyValue("DeviceID"),
            (string)device.GetPropertyValue("PNPDeviceID"),
            (string)device.GetPropertyValue("Description")
            ));
        }

        collection.Dispose();
        return devices;
    }
}

class USBDeviceInfo
{
    public string Name { get; private set; }
    public string DeviceID { get; private set; }
    public string PnpDeviceID { get; private set; }
    public string Description { get; private set; }

    public USBDeviceInfo(string name, string deviceID, string pnpDeviceID, string description)
    {
        this.Name = name;
        this.DeviceID = deviceID;
        this.PnpDeviceID = pnpDeviceID;
        this.Description = description;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多