/// <summary>
        /// Handles the window message when a drive change is in progress or finished
        /// </summary>
        /// <param name="msg">Window message</param>
        /// <returns>true, if the message was handled</returns>
        public static bool HandleDeviceChangedMessage(Message msg)
        {
            DEV_BROADCAST_HDR    hdr = new DEV_BROADCAST_HDR();
            DEV_BROADCAST_VOLUME vol = new DEV_BROADCAST_VOLUME();

            try
            {
                if (msg.WParam.ToInt32() == DBT_DEVICEARRIVAL)
                {
                    // new device
                    hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
                    if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
                    {
                        vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
                        return(DeviceNew(vol));
                    }
                }
                else if (msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
                {
                    // device remove
                    hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
                    if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
                    {
                        vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
                        return(DeviceRemoved(vol));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error in handling device changed message: {0}", ex);
            }
            return(false);
        }
예제 #2
0
    /// <summary>
    /// Handles the window message when a drive change is in progress or finished
    /// </summary>
    /// <param name="msg">Window message</param>
    /// <returns>true, if the message was handled</returns>
    public static bool HandleDeviceChangedMessage(Message msg)
    {
      DEV_BROADCAST_HDR hdr = new DEV_BROADCAST_HDR();
      DEV_BROADCAST_VOLUME vol = new DEV_BROADCAST_VOLUME();

      try
      {
        if (msg.WParam.ToInt32() == DBT_DEVICEARRIVAL)
        {
          // new device
          hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
          if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
          {
            vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
            return DeviceNew(vol);
          }
        }
        else if (msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
        {
          // device remove
          hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
          if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
          {
            vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
            return DeviceRemoved(vol);
          }
        }
      }
      catch (Exception ex)
      {
        Log.Error("Error in handling device changed message: {0}", ex);
      }
      return false;
    }
예제 #3
0
        /// <summary>
        /// Handles the window message when a drive change is in progress or finished
        /// </summary>
        /// <param name="msg">Window message</param>
        /// <returns>true, if the message was handled</returns>
        public static bool HandleDeviceChangedMessage(Message msg)
        {
            DEV_BROADCAST_HDR    hdr = new DEV_BROADCAST_HDR();
            DEV_BROADCAST_VOLUME vol = new DEV_BROADCAST_VOLUME();

            try
            {
                if (msg.LParam != IntPtr.Zero)
                {
                    var deviceInterface = (DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE));

                    // get friendly device name
                    string   deviceName = String.Empty;
                    string[] values     = deviceInterface.dbcc_name.Split('#');
                    if (values.Length >= 3)
                    {
                        string deviceType       = values[0].Substring(values[0].IndexOf(@"?\", StringComparison.Ordinal) + 2);
                        string deviceInstanceID = values[1];
                        string deviceUniqueID   = values[2];
                        string regPath          = @"SYSTEM\CurrentControlSet\Enum\" + deviceType + "\\" + deviceInstanceID + "\\" +
                                                  deviceUniqueID;
                        Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath);
                        if (regKey != null)
                        {
                            // use the friendly name if it exists
                            object result = regKey.GetValue("FriendlyName");
                            if (result != null)
                            {
                                deviceName = result.ToString();
                            }
                            // if not use the device description's last part
                            else
                            {
                                result = regKey.GetValue("DeviceDesc");
                                if (result != null)
                                {
                                    deviceName = result.ToString().Contains(@"%;")
                    ? result.ToString().Substring(result.ToString().IndexOf(@"%;", StringComparison.Ordinal) + 2)
                    : result.ToString();
                                }
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(deviceName) && deviceName.Contains("Microsoft Virtual DVD-ROM"))
                    {
                        Log.Debug("Ignoring Microsoft Virtual DVD-ROM device change event");

                        return(true);
                    }
                }
            }
            catch
            { }

            try
            {
                if (msg.WParam.ToInt32() == DBT_DEVICEARRIVAL)
                {
                    // new device
                    hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
                    if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
                    {
                        vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
                        return(DeviceNew(vol));
                    }
                }
                else if (msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
                {
                    // device remove
                    hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
                    if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
                    {
                        vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
                        return(DeviceRemoved(vol));
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error in handling device changed message: {0}", ex);
            }
            return(false);
        }
예제 #4
0
    /// <summary>
    /// Handles the window message when a drive change is in progress or finished
    /// </summary>
    /// <param name="msg">Window message</param>
    /// <returns>true, if the message was handled</returns>
    public static bool HandleDeviceChangedMessage(Message msg)
    {
      DEV_BROADCAST_HDR hdr = new DEV_BROADCAST_HDR();
      DEV_BROADCAST_VOLUME vol = new DEV_BROADCAST_VOLUME();

      try
      {
        var deviceInterface = (DEV_BROADCAST_DEVICEINTERFACE)Marshal.PtrToStructure(msg.LParam, typeof(DEV_BROADCAST_DEVICEINTERFACE));

        // get friendly device name
        string deviceName = String.Empty;
        string[] values = deviceInterface.dbcc_name.Split('#');
        if (values.Length >= 3)
        {
          string deviceType = values[0].Substring(values[0].IndexOf(@"?\", StringComparison.Ordinal) + 2);
          string deviceInstanceID = values[1];
          string deviceUniqueID = values[2];
          string regPath = @"SYSTEM\CurrentControlSet\Enum\" + deviceType + "\\" + deviceInstanceID + "\\" + deviceUniqueID;
          Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath);
          if (regKey != null)
          {
            // use the friendly name if it exists
            object result = regKey.GetValue("FriendlyName");
            if (result != null)
            {
              deviceName = result.ToString();
            }
            // if not use the device description's last part
            else
            {
              result = regKey.GetValue("DeviceDesc");
              if (result != null)
              {
                deviceName = result.ToString().Contains(@"%;") ? result.ToString().Substring(result.ToString().IndexOf(@"%;", StringComparison.Ordinal) + 2) : result.ToString();
              }
            }
          }
        }
        if (!string.IsNullOrEmpty(deviceName) && deviceName.Contains("Microsoft Virtual DVD-ROM"))
        {
          Log.Debug("Ignoring Microsoft Virtual DVD-ROM device change event");

          return true;
        }
      }
      catch
      { }

      try
      {
        if (msg.WParam.ToInt32() == DBT_DEVICEARRIVAL)
        {
          // new device
          hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
          if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
          {
            vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
            return DeviceNew(vol);
          }
        }
        else if (msg.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
        {
          // device remove
          hdr = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(msg.LParam, hdr.GetType());
          if (hdr.devicetype == DBT_DEVTYPE_VOLUME)
          {
            vol = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(msg.LParam, vol.GetType());
            return DeviceRemoved(vol);
          }
        }
      }
      catch (Exception ex)
      {
        Log.Error("Error in handling device changed message: {0}", ex);
      }
      return false;
    }