插入USB时如何获得pid vid? [英] How can I get pid vid of a usb when it is plugged in?

查看:775
本文介绍了插入USB时如何获得pid vid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio,Windows窗体应用程序中使用C#进行了编码.插入ıt时,我想查看USB的pid和vid.我写了这段代码,但是foreach (ManagementObject service in s.Get())中有错误.在s.Get()部分.它在Form1_Load部分中运行,但卡在WNDProc中.

I coded with c# in visual studio, windows form application. I want to see pid and vid of a USB when ıt is plugged in. I wrote this code but there is an error in foreach (ManagementObject service in s.Get()). In s.Get() part. It runs in Form1_Load part but it gets stuck in WNDProc.

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    if(m.Msg == WM_DEVICECHANGE)
            switch ((int)m.WParam)
            {
                case DBT_DEVICEARRIVAL:
                    listBox1.Items.Add("New Device Connected");

                 ManagementObjectSearcher s = new ManagementObjectSearcher("@SELECT * FROM Win32_USBHub");

                foreach (ManagementObject service in s.Get())
                {

                    listBox1.Items.Add(service);
                }


                string[] ports = SerialPort.GetPortNames();

                Console.WriteLine("The following serial ports were found:");
                foreach (string port in ports)
                {
                    Console.WriteLine(port);
                    listBox1.Items.Add(port);
                 }

                 break;

                case DBT_DEVICEREMOVECOMPLETE:
                    listBox1.Items.Add("Device Removed");
                    break; 
}

推荐答案

主要问题是

The main issue is that the WM_DEVICECHANGE only comes for information you registered, with a few exceptions as can be read in the remarks of RegisterDeviceNotification.

以下是您需要执行的详细操作:

Here are the details what you need to do:

要获取 WM_DEVICECHANGE DBT_DEVICEARRIVAL ,您需要在以下位置调用Win32 API:名为 RegisterDeviceNotification 的user32.dll正确填充的 DEV_BROADCAST_DEVICEINTERFACE_W 结构.

To get the WM_DEVICECHANGE with DBT_DEVICEARRIVAL for devices, you need to call the Win32 API in user32.dll called RegisterDeviceNotification with a correctly filled DEV_BROADCAST_DEVICEINTERFACE_W struct.

如果操作正确,您将获得 WM_DEVICECHANGE 消息,其中包含事件类型(在我们的示例中为 DBT_DEVICEARRIVAL ),如设备管理事件,以及指向详细信息的指针.指针需要读取为 DEV_BROADCAST_HDR struct,使您可以识别是否确实是

If done so correctly you will get WM_DEVICECHANGE messages, which contain the event type (in our case DBT_DEVICEARRIVAL) as described in the Device Management Events, and a pointer to details. The pointer needs to be read as the DEV_BROADCAST_HDR struct , allowing you to recognise if this is indeed the DEV_BROADCAST_DEVICEINTERFACE_W struct. If so this struct will contain a name, which you will need to parse at it contains the VID & PID.

要处理的事情很多,我花了几个小时才把它弄对.如果您需要快速解决方案,而无需执行可怕的细节,请添加NuGet包 Dapplo. Windows.Messages (VID和PID在0.9.7及更高版本中可用)到您的项目.请仅使用以下代码一次,否则您的代码将被多次调用,无需从Window进行此操作,但必须从Windows Forms或WPF应用程序进行

That is quite a lot to process, and it took me a couple of hours to get it right. If you need a quick solution, and skip implementing the horrible details, add the NuGet package Dapplo.Windows.Messages (VID & PID are available with 0.9.7 and later) to your project. Use the following code only once, otherwise your code will be called multiple times, there is no need to do this from a Window but it must be from a Windows Forms or WPF application:

    var deviceNotificationSubscription = DeviceNotification
        .OnDeviceArrival()
        .Subscribe(deviceInterfaceChangeInfo => {
            // Your code goes here, and will be automatically called
            var vid = deviceInterfaceChangeInfo.Device.VendorId;
            var pid = deviceInterfaceChangeInfo.Device.ProductId;
        });

我的图书馆高度依赖 System.Reactive ,在此我不做详细介绍.允许对您的应用程序使用更实用的方法.您可以通过调用deviceNotificationSubscription.Dispose();停止接收事件,该库将创建其自己的隐藏消息窗口以接收窗口消息,因此您甚至可以继续在后台接收信息.

My library highly depend on System.Reactive, I won't go into details here, which allows a more functional approach to your application. You can stop receiving the events by calling deviceNotificationSubscription.Dispose(); The library creates it's own hidden message window to receive the window messages, so you can even continue receiving the information in the background.

设备属性DeviceInterfaceChangeInfo DevBroadcastDeviceInterface 结构,它包含原始的Win32信息,但还具有一些更高级别的属性,例如:

The Device property of the DeviceInterfaceChangeInfo has the DevBroadcastDeviceInterface struct, which contains the original Win32 information, but additionally has some higher level properties like:

  1. 从注册表中检索到的友好名称
  2. 设备类型,例如USB,HID等,包括IsUSB
  3. 供应商ID
  4. 产品ID
  5. a DeviceInterfaceClass 枚举可简化对该类的代码访问
  6. 生成的URL,以获取有关设备的更多信息
  1. a friendly name, which is retrieved from the registry
  2. device type like USB, HID etc including an IsUSB
  3. vendor ID
  4. product ID
  5. a DeviceInterfaceClass enum for easier code access to the class
  6. a generated URL to get more information on the device

让我知道这是否行得通,并且在您遇到任何问题时,在我的 Dapplo.Windows GitHub项目!这个库中还有很多东西,但是不幸的是,大多数文档仍然需要编写.

Let me know if this works and helps here by and if you have any questions raise an issue on my Dapplo.Windows GitHub project! There is a lot more in this library but unfortunately most documentation still needs writing.

这篇关于插入USB时如何获得pid vid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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