# HG changeset patch # User StephaneLenclud # Date 1441216234 -7200 # Node ID 5ecbb2f57a16514d43eb868344957f490fa72636 # Parent 95f253aaf5886e6ab4fa25609699df2606acaba8 File handle properly closed when ejecting optical drive media. Trying to get find a way to determine if optical drive tray is open. diff -r 95f253aaf588 -r 5ecbb2f57a16 Server/MainForm.Hid.cs --- a/Server/MainForm.Hid.cs Wed Sep 02 16:02:24 2015 +0200 +++ b/Server/MainForm.Hid.cs Wed Sep 02 19:50:34 2015 +0200 @@ -241,11 +241,11 @@ } /// - /// + /// Eject optical drive media opening the tray if any. /// /// /// - private bool AutoEjectVolume(SafeFileHandle aVolume) + private bool MediaEject(SafeFileHandle aVolume) { //Hope that's doing what I think it does IntPtr dwBytesReturned = new IntPtr(); @@ -256,11 +256,11 @@ } /// - /// Not working. + /// Close an optical drive tray. /// /// /// - private bool CloseTray(SafeFileHandle aVolume) + private bool MediaLoad(SafeFileHandle aVolume) { //Hope that's doing what I think it does IntPtr dwBytesReturned = new IntPtr(); @@ -270,7 +270,24 @@ return Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped); } - + /// + /// + /// + /// + /// + private bool StorageCheckVerify(SafeFileHandle aVolume) + { + //Hope that's doing what I think it does + IntPtr dwBytesReturned = new IntPtr(); + //Should not be needed but I'm not sure how to pass NULL in there. + OVERLAPPED overlapped = new OVERLAPPED(); + + bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped); + + Debug.WriteLine("Check Verify: " + Marshal.GetLastWin32Error().ToString()); + + return res; + } @@ -290,25 +307,35 @@ SafeFileHandle handle = OpenVolume(drive); if (handle.IsInvalid) { + Debug.WriteLine("ERROR: Failed to open volume."); return; } if (LockVolume(handle) && DismountVolume(handle)) { - Debug.Write("Volume was dismounted."); + Debug.WriteLine("Volume was dismounted."); if (PreventRemovalOfVolume(handle,false)) { - if (AutoEjectVolume(handle)) + //StorageCheckVerify(handle); + + if (MediaEject(handle)) { - Debug.Write("Media was ejected"); + Debug.WriteLine("Media was ejected"); } - //else if (CloseTray(handle)) - //{ - // Debug.Write("Media was loaded"); - //} + else if (MediaLoad(handle)) + { + Debug.WriteLine("Media was loaded"); + } } } + else + { + Debug.WriteLine("Volume lock or dismount failed."); + } + + //This is needed to make sure we can open the volume next time around + handle.Dispose(); } /// diff -r 95f253aaf588 -r 5ecbb2f57a16 Server/MainForm.cs --- a/Server/MainForm.cs Wed Sep 02 16:02:24 2015 +0200 +++ b/Server/MainForm.cs Wed Sep 02 19:50:34 2015 +0200 @@ -1062,7 +1062,7 @@ string opticalDriveToEject=Properties.Settings.Default.OpticalDriveToEject; foreach (object item in comboBoxOpticalDrives.Items) { - if (opticalDriveToEject.Equals(item.ToString())) + if (opticalDriveToEject == item.ToString()) { comboBoxOpticalDrives.SelectedIndex = opticalDriveItemIndex; driveNotFound = false;