# HG changeset patch # User StephaneLenclud # Date 1441146274 -7200 # Node ID 7f62f2c352884e4a5563ce0239cf09b2bf6f8c5d # Parent 728fe28168e22b609c64a3f92663ab4a6af26412 Published v0.5.2 Media ejection now working with hardcoded E drive. diff -r 728fe28168e2 -r 7f62f2c35288 Server/MainForm.Hid.cs --- a/Server/MainForm.Hid.cs Tue Sep 01 20:57:06 2015 +0200 +++ b/Server/MainForm.Hid.cs Wed Sep 02 00:24:34 2015 +0200 @@ -136,9 +136,27 @@ } } + /// + /// + /// + /// + /// + private IntPtr MarshalToPointer(object data) + { + IntPtr buf = Marshal.AllocHGlobal( + Marshal.SizeOf(data)); + Marshal.StructureToPtr(data, + buf, false); + return buf; + } + + /// + /// + /// + /// private SafeFileHandle OpenVolume() { - return Function.CreateFile( "E:", + return Function.CreateFile("\\\\.\\E:", SharpLib.Win32.FileAccess.GENERIC_READ, SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE, IntPtr.Zero, @@ -150,9 +168,109 @@ /// /// /// + /// + /// + private bool LockVolume(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(); + + int tries = 0; + const int KMaxTries = 100; + const int KSleepTime = 10; + bool success = false; + + while (!success && tries < KMaxTries) + { + success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped); + System.Threading.Thread.Sleep(KSleepTime); + tries++; + } + + return success; + } + + /// + /// + /// + /// + /// + private bool DismountVolume(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(); + + return Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped); + } + + + + /// + /// + /// + /// + /// + /// + private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval) + { + //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(); + // + PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL(); + preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval); + IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval); + + bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped); + + Marshal.FreeHGlobal(preventMediaRemovalParam); + + return result; + } + + /// + /// + /// + /// + /// + private bool AutoEjectVolume(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(); + + return Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped); + } + + + + + /// + /// Perform media ejection. + /// private void HandleEject() { SafeFileHandle handle = OpenVolume(); + if (handle.IsInvalid) + { + return; + } + + if (LockVolume(handle) && DismountVolume(handle)) + { + Debug.Write("Volume was dismounted."); + + if (PreventRemovalOfVolume(handle,false) && AutoEjectVolume(handle)) + { + Debug.Write("Media was Ejected"); + } + } } /// diff -r 728fe28168e2 -r 7f62f2c35288 Server/SharpDisplayManager.csproj --- a/Server/SharpDisplayManager.csproj Tue Sep 01 20:57:06 2015 +0200 +++ b/Server/SharpDisplayManager.csproj Wed Sep 02 00:24:34 2015 +0200 @@ -33,7 +33,7 @@ index.htm false 0 - 0.5.1.%2a + 0.5.2.%2a false true true @@ -121,7 +121,7 @@ False - ..\packages\SharpLibWin32.0.0.4\lib\net20\SharpLibWin32.dll + ..\packages\SharpLibWin32.0.0.6\lib\net20\SharpLibWin32.dll diff -r 728fe28168e2 -r 7f62f2c35288 Server/packages.config --- a/Server/packages.config Tue Sep 01 20:57:06 2015 +0200 +++ b/Server/packages.config Wed Sep 02 00:24:34 2015 +0200 @@ -3,5 +3,5 @@ - + \ No newline at end of file