internal virtual DeviceChangedEventArgs[] AddUsbDevice(String path, DeviceChangeEvent devEvent) { DeviceChangedEventArgs[] eventArgs = null; // see if it is already in our list of Devices() Device device = FindDeviceByUsbPath(path, false); if (device == null) { // it's not in our Collection of constructed devices // so lets get a new list of our devices from Windows // and see if it is there. device = FindDeviceByUsbPath(path, true); } if (device != null) { Trace.WriteLine(String.Format("{0}.AddUsbDevice() Created:{1}", this, path)); eventArgs = new DeviceChangedEventArgs[1]; eventArgs[0] = new DeviceChangedEventArgs(devEvent, device.DeviceInstanceId, device.GetType(), device.UsbHub == null ? 0 : device.UsbHub.Index, device.UsbPort); } return(eventArgs); }
internal virtual DeviceChangedEventArgs[] RemoveUsbDevice(String path, DeviceChangeEvent devEvent) { DeviceChangedEventArgs[] eventArgs = null; // see if it is in our list of Devices Device device = FindDeviceByUsbPath(path, false); if (device != null) { Trace.WriteLine(String.Format("{0}.RemoveUsbDevice() Removed:{1}", this, path)); eventArgs = new DeviceChangedEventArgs[1]; eventArgs[0] = new DeviceChangedEventArgs(devEvent, device.DeviceInstanceId, device.GetType(), device.UsbHub == null ? 0 : device.UsbHub.Index, device.UsbPort); // If we found it, remove it from our list of devices _Devices.Remove(device); if (device.UsbHub != null) { device.UsbHub[device.UsbPort].Refresh(); } } return(eventArgs); }
public DeviceChangedEventArgs( DeviceChangeEvent eventType, String deviceId, Type deviceType, int hub, int port) { this._Event = eventType; this._DeviceId = deviceId; this._DeviceType = deviceType; this._Hub = hub; this._Port = port; }
DeviceChangedEventArgs[] RemoveUsbDevice(String path, DeviceChangeEvent devEvent) { DeviceChangedEventArgs[] eventArgs = null; foreach (DeviceClass deviceClass in DeviceClassMgrs) { // don't look for USB removal in MSC class, should have already happened by Volume removal. // if ( !(deviceClass is VolumeDeviceClass) ) { eventArgs = deviceClass.RemoveUsbDevice(path, devEvent); if (eventArgs != null) { break; } } } return(eventArgs); }
DeviceChangedEventArgs[] AddUsbDevice(String path, DeviceChangeEvent devEvent) { DeviceChangedEventArgs[] eventArgs = null; foreach (DeviceClass deviceClass in DeviceClassMgrs) { // don't look for USB arrival in MSC class, wait for Volume arrival. // if ( !(deviceClass is VolumeDeviceClass) ) { eventArgs = deviceClass.AddUsbDevice(path, devEvent); if (eventArgs != null) { break; } } } return(eventArgs); }
public bool StartSmartCardReaderMonitor(DeviceChangeEvent OnInitialized, DeviceChangeEvent OnStatusChanged, DeviceMonitorExceptionEvent OnMonitorException) { try { var factory = DeviceMonitorFactory.Instance; var smartCardReaderMonitor = factory.Create(SCardScope.System); smartCardReaderMonitor.Initialized += OnInitialized; smartCardReaderMonitor.StatusChanged += OnStatusChanged; smartCardReaderMonitor.MonitorException += OnMonitorException; smartCardReaderMonitor.Start(); return(true); } catch (Exception ex) { Debug.WriteLine("StartSmartCardReaderMonitor Exception: " + ex.Message); return(false); } }
/// <summary> /// Worker function for WM_DEVICECHANGE messages. Invokes DeviceChangedMsg event. /// </summary> /// <param name="msg">Native Windows message - WM_DEVICECHANGE</param> private void OnDeviceChange(Message msg) { DeviceChangeEvent devEvent = DeviceChangeEvent.Unknown; String devDetails = String.Empty; if (msg.LParam != IntPtr.Zero) { NativeMethods.DEV_BROADCAST_HDR db = (NativeMethods.DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_HDR)); switch (msg.WParam.ToInt32()) { case NativeMethods.DBT_DEVICEARRIVAL: if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.DEVICEINTERFACE) { NativeMethods.DEV_BROADCAST_DEVICEINTERFACE dbdi = (NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)); if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_DEVICE) { devEvent = DeviceChangeEvent.DeviceArrival; devDetails = dbdi.dbcc_name; } else if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_HUB) { devEvent = DeviceChangeEvent.HubArrival; devDetails = dbdi.dbcc_name; } } else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.VOLUME) { NativeMethods.DEV_BROADCAST_VOLUME dbv = (NativeMethods.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_VOLUME)); devEvent = DeviceChangeEvent.VolumeArrival; devDetails = DrivesFromMask(dbv.dbcv_unitmask); } else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.PORT) { NativeMethods.DEV_BROADCAST_PORT dbp = (NativeMethods.DEV_BROADCAST_PORT)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_PORT)); devEvent = DeviceChangeEvent.PortArrival; devDetails = dbp.dbcp_name; } break; case NativeMethods.DBT_DEVICEREMOVECOMPLETE: if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.DEVICEINTERFACE) { NativeMethods.DEV_BROADCAST_DEVICEINTERFACE dbdi = (NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_DEVICEINTERFACE)); if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_DEVICE) { devEvent = DeviceChangeEvent.DeviceRemoval; devDetails = dbdi.dbcc_name; } else if (dbdi.dbcc_classguid == NativeMethods.GUID_DEVINTERFACE_USB_HUB) { devEvent = DeviceChangeEvent.HubRemoval; devDetails = dbdi.dbcc_name; } } else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.VOLUME) { NativeMethods.DEV_BROADCAST_VOLUME dbv = (NativeMethods.DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_VOLUME)); devEvent = DeviceChangeEvent.VolumeRemoval; devDetails = DrivesFromMask(dbv.dbcv_unitmask); } else if (db.dbch_devicetype == NativeMethods.DBT_DEVTYP.PORT) { NativeMethods.DEV_BROADCAST_PORT dbp = (NativeMethods.DEV_BROADCAST_PORT)Marshal.PtrToStructure(msg.LParam, typeof(NativeMethods.DEV_BROADCAST_PORT)); devEvent = DeviceChangeEvent.PortRemoval; devDetails = dbp.dbcp_name; } break; default: Trace.Assert(false, "Invalid msg.WParam."); break; } // end switch (nEventType) Trace.WriteLine(String.Format("*** DeviceChangeWindow.OnDeviceChange(), {0}, {1}, {2}({3})", devEvent, devDetails, Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode())); // let's figure out what to do with the WM_DEVICECHANGE message // after we get out of this loop so we don't miss any messages. if (DeviceChangedMsg != null) { DeviceChangedMsg.BeginInvoke(devEvent, devDetails, null, null); } } // end if (lpdb) msg.Result = new IntPtr(1); // true return; }
/// <summary> /// Handler for DeviceChangeWindow.DeviceChangedMsg event which gets signaled in response to /// WM_DEVICECHANGE Windows event. This handler is called asynchronously on a ThreadPool thread /// since it is called with BeginInvoke() from the DeviceChangeWindow.DeviceChangedMsg event handler. /// /// The work done here is to Add/Remove the changing device to/from the DeviceManager's static lists of devices, /// and then notify the "watcher(s)" (on their appropriate thread(s)) what type of change occured. /// /// Note: Watchers subscribing directly to the DeviceChange event will be called for all device /// changes monitored by the DeviceManager. If you want to filter the device change messages sent /// by the DeviceManager, you must call RegisterForDeviceChangeNotification with the desired /// filter info and delegate. /// </summary> /// <param name="devEvent"></param> /// <param name="devDetails"></param> private static void DevChangeWnd_DeviceChangedMsg(DeviceChangeEvent devEvent, String devDetails) { Thread.CurrentThread.Name = Thread.CurrentThread.IsThreadPoolThread ? "Some ThreadPool Thread" : "WhoKnows"; Trace.WriteLine(String.Format("*** DeviceManager.DevChangeWnd_DeviceChangedMsg(): {0} {1}, {2}({3})", devEvent, devDetails, String.IsNullOrEmpty(Thread.CurrentThread.Name) ? "thread" : Thread.CurrentThread.Name, Thread.CurrentThread.GetHashCode())); DeviceChangedEventArgs[] eventArgs = null; /// /// Add/Remove the device from our static list of devices. /// switch (devEvent) { case DeviceChangeEvent.VolumeArrival: // eventArgs = VolumeDeviceClass.Instance.AddUsbDevice(devDetails, devEvent); break; case DeviceChangeEvent.DeviceArrival: eventArgs = Instance.AddUsbDevice(devDetails, devEvent); break; case DeviceChangeEvent.HubArrival: eventArgs = UsbHubClass.Instance.AddUsbDevice(devDetails, devEvent); break; case DeviceChangeEvent.VolumeRemoval: // eventArgs = VolumeDeviceClass.Instance.RemoveUsbDevice(devDetails, devEvent); break; case DeviceChangeEvent.DeviceRemoval: eventArgs = Instance.RemoveUsbDevice(devDetails, devEvent); break; case DeviceChangeEvent.HubRemoval: eventArgs = UsbHubClass.Instance.RemoveUsbDevice(devDetails, devEvent); break; default: break; } // if (eventArgs == null) // { // eventArgs = new DeviceChangedEventArgs[1]; // eventArgs[0] = new DeviceChangedEventArgs(devEvent, devDetails); // } /// Signal anyone waiting on a device change to go see what changed. See FindDevice() for example. EventWaitHandle.Set(); if (eventArgs != null) { /// /// Notify all interested parties. /// bool doNotify = false; foreach (DeviceChangeWatcherArgs watcher in _DeviceChangeCallbackList) { foreach (DeviceChangedEventArgs eventArg in eventArgs) { switch (watcher.NotifyFilter) { case NotifyFilters.Any: doNotify = true; break; case NotifyFilters.Device: if (eventArg.DeviceId == watcher.DeviceId) { doNotify = true; } break; case NotifyFilters.DeviceType: if (eventArg.DeviceType == watcher.DeviceType) { doNotify = true; } break; case NotifyFilters.Port: if (eventArg.Port == watcher.Port && eventArg.Hub == watcher.Hub) { doNotify = true; } break; case NotifyFilters.None: default: break; } if (doNotify) { object[] args = new object[1] { eventArg }; System.Windows.Application.Current.Dispatcher.BeginInvoke(watcher.Delegate, args); // if (watcher.Delegate.Target is ISynchronizeInvoke) // ((ISynchronizeInvoke)watcher.Delegate.Target).BeginInvoke(watcher.Delegate, args); // else // watcher.Delegate(eventArg); } } // foreach eventArg } // foreach watcher } // if ( eventArgs != null ) }