File handle properly closed when ejecting optical drive media.
authorStephaneLenclud
Wed, 02 Sep 2015 19:50:34 +0200
changeset 1545ecbb2f57a16
parent 153 95f253aaf588
child 155 703daa997f59
File handle properly closed when ejecting optical drive media.
Trying to get find a way to determine if optical drive tray is open.
Server/MainForm.Hid.cs
Server/MainForm.cs
     1.1 --- a/Server/MainForm.Hid.cs	Wed Sep 02 16:02:24 2015 +0200
     1.2 +++ b/Server/MainForm.Hid.cs	Wed Sep 02 19:50:34 2015 +0200
     1.3 @@ -241,11 +241,11 @@
     1.4          }
     1.5  
     1.6          /// <summary>
     1.7 -        /// 
     1.8 +        /// Eject optical drive media opening the tray if any.
     1.9          /// </summary>
    1.10          /// <param name="aVolume"></param>
    1.11          /// <returns></returns>
    1.12 -        private bool AutoEjectVolume(SafeFileHandle aVolume)
    1.13 +        private bool MediaEject(SafeFileHandle aVolume)
    1.14          {
    1.15              //Hope that's doing what I think it does
    1.16              IntPtr dwBytesReturned = new IntPtr();
    1.17 @@ -256,11 +256,11 @@
    1.18          }
    1.19  
    1.20          /// <summary>
    1.21 -        /// Not working.
    1.22 +        /// Close an optical drive tray.
    1.23          /// </summary>
    1.24          /// <param name="aVolume"></param>
    1.25          /// <returns></returns>
    1.26 -        private bool CloseTray(SafeFileHandle aVolume)
    1.27 +        private bool MediaLoad(SafeFileHandle aVolume)
    1.28          {
    1.29              //Hope that's doing what I think it does
    1.30              IntPtr dwBytesReturned = new IntPtr();
    1.31 @@ -270,7 +270,24 @@
    1.32              return Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
    1.33          }
    1.34  
    1.35 -        
    1.36 +        /// <summary>
    1.37 +        /// 
    1.38 +        /// </summary>
    1.39 +        /// <param name="aVolume"></param>
    1.40 +        /// <returns></returns>
    1.41 +        private bool StorageCheckVerify(SafeFileHandle aVolume)
    1.42 +        {
    1.43 +            //Hope that's doing what I think it does
    1.44 +            IntPtr dwBytesReturned = new IntPtr();
    1.45 +            //Should not be needed but I'm not sure how to pass NULL in there.
    1.46 +            OVERLAPPED overlapped = new OVERLAPPED();
    1.47 +
    1.48 +            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
    1.49 +
    1.50 +            Debug.WriteLine("Check Verify: " + Marshal.GetLastWin32Error().ToString());
    1.51 +
    1.52 +            return res;
    1.53 +        }        
    1.54          
    1.55  
    1.56  
    1.57 @@ -290,25 +307,35 @@
    1.58              SafeFileHandle handle = OpenVolume(drive);
    1.59              if (handle.IsInvalid)
    1.60              {
    1.61 +                Debug.WriteLine("ERROR: Failed to open volume.");
    1.62                  return;
    1.63              }
    1.64  
    1.65              if (LockVolume(handle) && DismountVolume(handle))
    1.66              {
    1.67 -                Debug.Write("Volume was dismounted.");
    1.68 +                Debug.WriteLine("Volume was dismounted.");
    1.69  
    1.70                  if (PreventRemovalOfVolume(handle,false))
    1.71                  {
    1.72 -                    if (AutoEjectVolume(handle))
    1.73 +                    //StorageCheckVerify(handle);
    1.74 +
    1.75 +                    if (MediaEject(handle))
    1.76                      {
    1.77 -                        Debug.Write("Media was ejected");
    1.78 +                        Debug.WriteLine("Media was ejected");
    1.79                      }
    1.80 -                    //else if (CloseTray(handle))
    1.81 -                    //{
    1.82 -                    //    Debug.Write("Media was loaded");
    1.83 -                    //}                    
    1.84 +                    else if (MediaLoad(handle))
    1.85 +                    {
    1.86 +                        Debug.WriteLine("Media was loaded");
    1.87 +                    }                    
    1.88                  }
    1.89              }
    1.90 +            else
    1.91 +            {
    1.92 +                Debug.WriteLine("Volume lock or dismount failed.");
    1.93 +            }
    1.94 +
    1.95 +            //This is needed to make sure we can open the volume next time around
    1.96 +            handle.Dispose();
    1.97          }
    1.98  
    1.99          /// <summary>
     2.1 --- a/Server/MainForm.cs	Wed Sep 02 16:02:24 2015 +0200
     2.2 +++ b/Server/MainForm.cs	Wed Sep 02 19:50:34 2015 +0200
     2.3 @@ -1062,7 +1062,7 @@
     2.4              string opticalDriveToEject=Properties.Settings.Default.OpticalDriveToEject;
     2.5              foreach (object item in comboBoxOpticalDrives.Items)
     2.6              {
     2.7 -                if (opticalDriveToEject.Equals(item.ToString()))
     2.8 +                if (opticalDriveToEject == item.ToString())
     2.9                  {
    2.10                      comboBoxOpticalDrives.SelectedIndex = opticalDriveItemIndex;
    2.11                      driveNotFound = false;