- UID
- 390967
- 帖子
- 1660
- 主題
- 855
- 精華
- 0
- 積分
- 869
- 楓幣
- 12244
- 威望
- 409
- 存款
- 10100
- 贊助金額
- 1800
- 推廣
- 0
- GP
- 2803
- 閱讀權限
- 150
- 在線時間
- 197 小時
- 註冊時間
- 2023-5-18
- 最後登入
- 2024-12-22
|
要修改偵測裝置或是增加可以修改這兩行
USB為GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
HID為GUID_DEVINTERFACE_HID = new Guid("4D1E55B2-F16F-11CF-88CB-001111000030");- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.windows.Forms;
- using System.Runtime.InteropServices;
- namespace WindowsFormsApplication3
- {
- //public partial class Form1 : Form
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- RegisterHidNotification();
- }
- protected override void WndProc(ref Message m)
- {
- switch (m.Msg)
- {
- case Win32.WM_DEVICECHANGE:
- OnDeviceChange(ref m);
- break;
- }
- base.WndProc(ref m);
- }
- void OnDeviceChange(ref Message msg)
- {
- int wParam = (int)msg.WParam;
- if (wParam == Win32.DBT_DEVICEARRIVAL)
- label1.Text = "Arrival";
- else if (wParam == Win32.DBT_DEVICEREMOVECOMPLETE)
- label1.Text = "Remove";
- }
- void RegisterHidNotification()
- {
- Win32.DEV_BROADCAST_DEVICEINTERFACE dbi = new Win32.DEV_BROADCAST_DEVICEINTERFACE();
- int size = Marshal.SizeOf(dbi);
- dbi.dbcc_size = size;
- dbi.dbcc_devicetype = Win32.DBT_DEVTYP_DEVICEINTERFACE;
- dbi.dbcc_reserved = 0;
- dbi.dbcc_classguid = Win32.GUID_DEVINTERFACE_HID;
- dbi.dbcc_name = 0;
- IntPtr buffer = Marshal.AllocHGlobal(size);
- Marshal.StructureToPtr(dbi, buffer, true);
- IntPtr r = Win32.RegisterDeviceNotification(Handle, buffer, Win32.DEVICE_NOTIFY_WINDOW_HANDLE);
- if (r == IntPtr.Zero)
- label1.Text = Win32.GetLastError().ToString();
- }
- }
- class Win32
- {
- public const int WM_DEVICECHANGE = 0x0219;
- public const int DBT_DEVICEARRIVAL = 0x8000;
- public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
- public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0;
- public const int DEVICE_NOTIFY_SERVICE_HANDLE = 1;
- public const int DBT_DEVTYP_DEVICEINTERFACE = 5;
- public static Guid GUID_DEVINTERFACE_HID = new Guid("4D1E55B2-F16F-11CF-88CB-001111000030");
- public static Guid GUID_DEVINTERFACE_USB_DEVICE = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED");
- [StructLayout(LayoutKind.Sequential)]
- public class DEV_BROADCAST_DEVICEINTERFACE
- {
- public int dbcc_size;
- public int dbcc_devicetype;
- public int dbcc_reserved;
- public Guid dbcc_classguid;
- public short dbcc_name;
- }
- [DllImport("user32.dll", SetLastError = true)]
- public static extern IntPtr RegisterDeviceNotification(IntPtr hRecipient, IntPtr NotificationFilter, Int32 Flags);
- [DllImport("kernel32.dll")]
- public static extern int GetLastError();
- }
- }
複製代碼 |
|