private void Init(Window window) { // Adds the windows message processing hook and registers USB device add/removal notification. HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(window).Handle); if (source != null) { var windowHandle = source.Handle; source.AddHook(HwndHandler); DeviceNotification.RegisterDeviceNotification(windowHandle); } }
/// <summary> /// Method that receives window messages. /// </summary> private IntPtr HwndHandler(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { if (msg == DeviceNotification.c_WmDevicechange) { DeviceNotification.DbtEventType eventType = (DeviceNotification.DbtEventType)wparam; DeviceData device = DeviceNotification.GetDevice(lparam); if (!m_FilterByType || device.DeviceType == m_DeviceType) { if (eventType == DeviceNotification.DbtEventType.Arrival) { OnDeviceArrival(device); } else if (eventType == DeviceNotification.DbtEventType.RemoveComplete) { OnDeviceRemoval(device); } } } handled = false; return(IntPtr.Zero); }
private void Close() { DeviceNotification.UnregisterDeviceNotification(); }