public DeviceEventNotifier(IntPtr windowHandle, Guid deviceClass) { base.AssignHandle(windowHandle); var filterInterface = new User32.BroadcastDeviceInterface { DeviceClass = deviceClass }; var filter = new User32.BroadcastHdr { Size = Marshal.SizeOf(typeof (User32.BroadcastHdr)), DeviceType = User32.DeviceType.Interface, Interface = filterInterface }; notificationHandle = User32.RegisterDeviceNotification(base.Handle, filter, User32.DeviceNotificationFlags.WindowHandle); }
public DeviceEventNotifier(IntPtr windowHandle, Guid deviceClass) { base.AssignHandle(windowHandle); var filterInterface = new User32.BroadcastDeviceInterface { DeviceClass = deviceClass }; var filter = new User32.BroadcastHdr { Size = Marshal.SizeOf(typeof(User32.BroadcastHdr)), DeviceType = User32.DeviceType.Interface, Interface = filterInterface }; notificationHandle = User32.RegisterDeviceNotification(base.Handle, filter, User32.DeviceNotificationFlags.WindowHandle); }
protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == (int)User32.WindowMessage.DeviceChange) { User32.BroadcastHdr hdr = new User32.BroadcastHdr(); if (m.LParam != IntPtr.Zero) { hdr = (User32.BroadcastHdr)Marshal.PtrToStructure( m.LParam, typeof(User32.BroadcastHdr)); } switch (m.WParam.ToInt32()) { case (int)User32.DeviceEvent.DeviceArrival: OnDeviceArrived(hdr.Interface.Name); break; case (int)User32.DeviceEvent.DeviceRemoveComplete: OnDeviceRemoval(hdr.Interface.Name); break; } } base.WndProc(ref m); }