冰楓論壇

標題: [C#] 偵測(Detection)USB的插入(Insert)以及HID的插入和拔除(Remove) [打印本頁]

作者: whitefox    時間: 2023-7-8 13:11
標題: [C#] 偵測(Detection)USB的插入(Insert)以及HID的插入和拔除(Remove)
要修改偵測裝置或是增加可以修改這兩行
USB為GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
HID為GUID_DEVINTERFACE_HID        = new Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.InteropServices;

  10. namespace WindowsFormsApplication3
  11. {
  12.     //public partial class Form1 : Form
  13.     public partial class Form1 : Form
  14.     {
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             RegisterHidNotification();
  19.         }

  20.         protected override void WndProc(ref Message m)
  21.         {
  22.             switch (m.Msg)
  23.             {
  24.             case Win32.WM_DEVICECHANGE:
  25.                 OnDeviceChange(ref m);
  26.                 break;
  27.             }
  28.             base.WndProc(ref m);
  29.         }

  30.         void OnDeviceChange(ref Message msg)
  31.         {
  32.             int wParam = (int)msg.WParam;

  33.             if (wParam == Win32.DBT_DEVICEARRIVAL)
  34.                 label1.Text = "Arrival";
  35.             else if (wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
  36.                 label1.Text = "Remove";
  37.         }

  38.         void RegisterHidNotification()
  39.         {
  40.             Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new Win32.DEV_BROADCAST_DEVICEINTERFACE();

  41.             int size            = Marshal.SizeOf(dbi);
  42.             dbi.dbcc_size       = size;
  43.             dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
  44.             dbi.dbcc_reserved   = 0;
  45.             dbi.dbcc_classguid  = Win32.GUID_DEVINTERFACE_HID;
  46.             dbi.dbcc_name       = 0;

  47.             IntPtr buffer = Marshal.AllocHGlobal(size);
  48.             Marshal.StructureToPtr(dbi, buffer, true);
  49.             IntPtr r = Win32.RegisterDeviceNotification(Handle, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE);

  50.             if (r == IntPtr.Zero)
  51.                 label1.Text = Win32.GetLastError().ToString();
  52.         }
  53.     }

  54.     class Win32
  55.     {
  56.         public const int WM_DEVICECHANGE              = 0x0219;
  57.         public const int DBT_DEVICEARRIVAL            = 0x8000;
  58.         public const int DBT_DEVICEREMOVECOMPLETE     = 0x8004;
  59.         public const int DEVICE_NOTIFY_WINDOW_HANDLE  = 0;
  60.         public const int DEVICE_NOTIFY_SERVICE_HANDLE = 1;

  61.         public const int DBT_DEVTYP_DEVICEINTERFACE   = 5;

  62.         public static Guid GUID_DEVINTERFACE_HID        = new Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
  63.         public static Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");

  64.         [StructLayout(LayoutKind.Sequential)]
  65.         public class DEV_BROADCAST_DEVICEINTERFACE
  66.         {
  67.             public int   dbcc_size;
  68.             public int   dbcc_devicetype;
  69.             public int   dbcc_reserved;
  70.             public Guid  dbcc_classguid;
  71.             public short dbcc_name;
  72.         }

  73.         [DllImport("user32.dll", SetLastError = true)]
  74.         public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, Int32 Flags);
  75.         [DllImport("kernel32.dll")]
  76.         public static extern int GetLastError();
  77.     }
  78. }
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓