public bool StartListening()
        {
            IScreenControl screenControl = ServiceRegistration.Get <IScreenControl>(false);

            _windowHandle = screenControl == null ? IntPtr.Zero : screenControl.MainWindowHandle;
            if (_windowHandle == IntPtr.Zero)
            {
                ServiceRegistration.Get <ILogger>().Warn("RemovableMediaTracker: No main window handle available, cannot start listening for removable media messages");
                return(false);
            }
            RemovableMediaTrackerSettings settings = ServiceRegistration.Get <ISettingsManager>().Load <RemovableMediaTrackerSettings>();

            try
            {
                _deviceMonitor = new DeviceVolumeMonitor(_windowHandle);
                _deviceMonitor.MediaInserted += OnMediaInserted;
                _deviceMonitor.MediaRemoved  += OnMediaRemoved;
                _deviceMonitor.Enabled        = settings.TrackRemovableMedia;

                ServiceRegistration.Get <ILogger>().Info("RemovableMediaTracker: Monitoring system for removable media changes");
                return(true);
            }
            catch (DeviceVolumeMonitorException ex)
            {
                ServiceRegistration.Get <ILogger>().Error("RemovableMediaTracker: Error enabling RemovableMediaTracker service", ex);
            }
            return(false);
        }
Example #2
0
 /// <summary>
 /// Creates and initializes a new internal <see cref="_DeviceVolumeMonitor"/> instance.
 /// </summary>
 /// <param name="monitor">The DeviceVolumeMonitor instance that ownes this object.</param>
 public _DeviceVolumeMonitor(DeviceVolumeMonitor monitor)
 {
     _monitor          = monitor;
     _knownDrives      = GetCurrentDriveLetters();
     _checkDrivesTimer = new DelayedEvent(2000);
     _checkDrivesTimer.OnEventHandler += CheckDrives;
 }
        public void StopListening()
        {
            if (_deviceMonitor != null)
            {
                _deviceMonitor.Dispose();
            }

            _deviceMonitor = null;
        }
Example #4
0
 /// <summary>
 /// WndProc method that traps all messages sent to the Handle.
 /// </summary>
 /// <param name="msg">A Windows message.</param>
 protected override void WndProc(ref Message msg)
 {
     base.WndProc(ref msg);
     if (!_monitor.Enabled)
     {
         return;
     }
     if (msg.Msg == WM_DEVICECHANGE)
     {
         DeviceEvent evt = (DeviceEvent)msg.WParam.ToInt32();
         if (evt == DeviceEvent.Arrival || evt == DeviceEvent.RemoveComplete)
         {
             BroadcastHeader broadcastHeader = (BroadcastHeader)Marshal.PtrToStructure(msg.LParam, typeof(BroadcastHeader));
             if (broadcastHeader.Type == DeviceType.Volume)
             {
                 Volume volume = (Volume)Marshal.PtrToStructure(msg.LParam, typeof(Volume));
                 if ((volume.Flags & (int)VolumeFlags.Media) != 0)
                 {
                     foreach (char drive in DeviceVolumeMonitor.MaskToDrives(volume.Mask).Select(d => d[0]))
                     {
                         if (evt == DeviceEvent.Arrival)
                         {
                             //Force media inserted event because the drive might already exist but was just mounted (DVD, CD etc.)
                             if (!_arrivedDrives.Contains(drive))
                             {
                                 _arrivedDrives.Add(drive);
                             }
                         }
                         else if (evt == DeviceEvent.RemoveComplete)
                         {
                             //Force media removed event because the drive might already exist but was just unmounted (DVD, CD etc.)
                             if (!_removedDrives.Contains(drive))
                             {
                                 _removedDrives.Add(drive);
                             }
                         }
                     }
                     //The Arrival and RemoveComplete events might be accompanied by DeviceNodeChanged events so have a delay to filter them out
                     //Also the known drives list needs to be updated which is handled by the delayed event
                     _checkDrivesTimer.EnqueueEvent(null, EventArgs.Empty);
                 }
             }
         }
         else if (evt == DeviceEvent.DeviceNodeChanged)
         {
             //A removal of a drive might cause multiple DeviceNodeChanged events so use a delay to filter them out
             //Also the drive is not yet mounted when receiving this event so delay the event trigger
             _checkDrivesTimer.EnqueueEvent(null, EventArgs.Empty);
         }
     }
 }
    public bool StartListening()
    {
      IScreenControl screenControl = ServiceRegistration.Get<IScreenControl>(false);
      _windowHandle = screenControl == null ? IntPtr.Zero : screenControl.MainWindowHandle;
      if (_windowHandle == IntPtr.Zero)
      {
        ServiceRegistration.Get<ILogger>().Warn("RemovableMediaTracker: No main window handle available, cannot start listening for removable media messages");
        return false;
      }
      RemovableMediaTrackerSettings settings = ServiceRegistration.Get<ISettingsManager>().Load<RemovableMediaTrackerSettings>();
      try
      {
        _deviceMonitor = new DeviceVolumeMonitor(_windowHandle);
        _deviceMonitor.MediaInserted += OnMediaInserted;
        _deviceMonitor.MediaRemoved += OnMediaRemoved;
        _deviceMonitor.Enabled = settings.TrackRemovableMedia;

        ServiceRegistration.Get<ILogger>().Info("RemovableMediaTracker: Monitoring system for removable media changes");
        return true;
      }
      catch (DeviceVolumeMonitorException ex)
      {
        ServiceRegistration.Get<ILogger>().Error("RemovableMediaTracker: Error enabling RemovableMediaTracker service", ex);
      }
      return false;
    }
    public void StopListening()
    {
      if (_deviceMonitor != null)
        _deviceMonitor.Dispose();

      _deviceMonitor = null;
    }
Example #7
0
 /// <summary>
 /// Creates and initializes a new internal <see cref="_DeviceVolumeMonitor"/> instance.
 /// </summary>
 /// <param name="monitor">The DeviceVolumeMonitor instance that ownes this object.</param>
 public _DeviceVolumeMonitor(DeviceVolumeMonitor monitor)
 {
     _monitor = monitor;
 }
 /// <summary>
 /// Creates and initializes a new internal <see cref="_DeviceVolumeMonitor"/> instance.
 /// </summary>
 /// <param name="monitor">The DeviceVolumeMonitor instance that ownes this object.</param>
 public _DeviceVolumeMonitor(DeviceVolumeMonitor monitor)
 {
   _monitor = monitor;
 }