Published v0.5.2
authorStephaneLenclud
Wed, 02 Sep 2015 00:24:34 +0200
changeset 1517f62f2c35288
parent 150 728fe28168e2
child 152 03a1757a38df
Published v0.5.2
Media ejection now working with hardcoded E drive.
Server/MainForm.Hid.cs
Server/SharpDisplayManager.csproj
Server/packages.config
     1.1 --- a/Server/MainForm.Hid.cs	Tue Sep 01 20:57:06 2015 +0200
     1.2 +++ b/Server/MainForm.Hid.cs	Wed Sep 02 00:24:34 2015 +0200
     1.3 @@ -136,9 +136,27 @@
     1.4              }
     1.5          }
     1.6  
     1.7 +        /// <summary>
     1.8 +        /// 
     1.9 +        /// </summary>
    1.10 +        /// <param name="data"></param>
    1.11 +        /// <returns></returns>
    1.12 +        private IntPtr MarshalToPointer(object data)
    1.13 +        {
    1.14 +            IntPtr buf = Marshal.AllocHGlobal(
    1.15 +                Marshal.SizeOf(data));
    1.16 +            Marshal.StructureToPtr(data,
    1.17 +                buf, false);
    1.18 +            return buf;
    1.19 +        }
    1.20 +
    1.21 +        /// <summary>
    1.22 +        /// 
    1.23 +        /// </summary>
    1.24 +        /// <returns></returns>
    1.25          private SafeFileHandle OpenVolume()
    1.26          {
    1.27 -            return Function.CreateFile(  "E:",
    1.28 +            return Function.CreateFile("\\\\.\\E:",
    1.29                                 SharpLib.Win32.FileAccess.GENERIC_READ,
    1.30                                 SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
    1.31                                 IntPtr.Zero,
    1.32 @@ -150,9 +168,109 @@
    1.33          /// <summary>
    1.34          /// 
    1.35          /// </summary>
    1.36 +        /// <param name="aVolume"></param>
    1.37 +        /// <returns></returns>
    1.38 +        private bool LockVolume(SafeFileHandle aVolume)
    1.39 +        {
    1.40 +            //Hope that's doing what I think it does
    1.41 +            IntPtr dwBytesReturned=new IntPtr();
    1.42 +            //Should not be needed but I'm not sure how to pass NULL in there.
    1.43 +            OVERLAPPED overlapped=new OVERLAPPED();
    1.44 +
    1.45 +            int tries = 0;
    1.46 +            const int KMaxTries = 100;
    1.47 +            const int KSleepTime = 10;
    1.48 +            bool success = false;
    1.49 +
    1.50 +            while (!success && tries < KMaxTries)
    1.51 +            {
    1.52 +                success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
    1.53 +                System.Threading.Thread.Sleep(KSleepTime);
    1.54 +                tries++;
    1.55 +            }
    1.56 +
    1.57 +            return success;
    1.58 +        }
    1.59 +
    1.60 +        /// <summary>
    1.61 +        /// 
    1.62 +        /// </summary>
    1.63 +        /// <param name="aVolume"></param>
    1.64 +        /// <returns></returns>
    1.65 +        private bool DismountVolume(SafeFileHandle aVolume)
    1.66 +        {
    1.67 +            //Hope that's doing what I think it does
    1.68 +            IntPtr dwBytesReturned = new IntPtr();
    1.69 +            //Should not be needed but I'm not sure how to pass NULL in there.
    1.70 +            OVERLAPPED overlapped=new OVERLAPPED();
    1.71 +
    1.72 +            return Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
    1.73 +        }
    1.74 +
    1.75 +
    1.76 +
    1.77 +        /// <summary>
    1.78 +        /// 
    1.79 +        /// </summary>
    1.80 +        /// <param name="aVolume"></param>
    1.81 +        /// <param name="aPreventRemoval"></param>
    1.82 +        /// <returns></returns>
    1.83 +        private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval)
    1.84 +        {
    1.85 +            //Hope that's doing what I think it does
    1.86 +            IntPtr dwBytesReturned = new IntPtr();
    1.87 +            //Should not be needed but I'm not sure how to pass NULL in there.
    1.88 +            OVERLAPPED overlapped = new OVERLAPPED();
    1.89 +            //
    1.90 +            PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL();
    1.91 +            preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval);
    1.92 +            IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval);
    1.93 +
    1.94 +            bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
    1.95 +
    1.96 +            Marshal.FreeHGlobal(preventMediaRemovalParam);
    1.97 +
    1.98 +            return result;
    1.99 +        }
   1.100 +
   1.101 +        /// <summary>
   1.102 +        /// 
   1.103 +        /// </summary>
   1.104 +        /// <param name="aVolume"></param>
   1.105 +        /// <returns></returns>
   1.106 +        private bool AutoEjectVolume(SafeFileHandle aVolume)
   1.107 +        {
   1.108 +            //Hope that's doing what I think it does
   1.109 +            IntPtr dwBytesReturned = new IntPtr();
   1.110 +            //Should not be needed but I'm not sure how to pass NULL in there.
   1.111 +            OVERLAPPED overlapped=new OVERLAPPED();
   1.112 +
   1.113 +            return Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
   1.114 +        }
   1.115 +
   1.116 +        
   1.117 +
   1.118 +
   1.119 +        /// <summary>
   1.120 +        /// Perform media ejection.
   1.121 +        /// </summary>
   1.122          private void HandleEject()
   1.123          {
   1.124              SafeFileHandle handle = OpenVolume();
   1.125 +            if (handle.IsInvalid)
   1.126 +            {
   1.127 +                return;
   1.128 +            }
   1.129 +
   1.130 +            if (LockVolume(handle) && DismountVolume(handle))
   1.131 +            {
   1.132 +                Debug.Write("Volume was dismounted.");
   1.133 +
   1.134 +                if (PreventRemovalOfVolume(handle,false) && AutoEjectVolume(handle))
   1.135 +                {
   1.136 +                    Debug.Write("Media was Ejected");
   1.137 +                }
   1.138 +            }
   1.139          }
   1.140  
   1.141          /// <summary>
     2.1 --- a/Server/SharpDisplayManager.csproj	Tue Sep 01 20:57:06 2015 +0200
     2.2 +++ b/Server/SharpDisplayManager.csproj	Wed Sep 02 00:24:34 2015 +0200
     2.3 @@ -33,7 +33,7 @@
     2.4      <WebPage>index.htm</WebPage>
     2.5      <OpenBrowserOnPublish>false</OpenBrowserOnPublish>
     2.6      <ApplicationRevision>0</ApplicationRevision>
     2.7 -    <ApplicationVersion>0.5.1.%2a</ApplicationVersion>
     2.8 +    <ApplicationVersion>0.5.2.%2a</ApplicationVersion>
     2.9      <UseApplicationTrust>false</UseApplicationTrust>
    2.10      <CreateDesktopShortcut>true</CreateDesktopShortcut>
    2.11      <PublishWizardCompleted>true</PublishWizardCompleted>
    2.12 @@ -121,7 +121,7 @@
    2.13      </Reference>
    2.14      <Reference Include="SharpLibWin32, Version=1.0.0.0, Culture=neutral, processorArchitecture=x86">
    2.15        <SpecificVersion>False</SpecificVersion>
    2.16 -      <HintPath>..\packages\SharpLibWin32.0.0.4\lib\net20\SharpLibWin32.dll</HintPath>
    2.17 +      <HintPath>..\packages\SharpLibWin32.0.0.6\lib\net20\SharpLibWin32.dll</HintPath>
    2.18      </Reference>
    2.19      <Reference Include="System" />
    2.20      <Reference Include="System.Configuration" />
     3.1 --- a/Server/packages.config	Tue Sep 01 20:57:06 2015 +0200
     3.2 +++ b/Server/packages.config	Wed Sep 02 00:24:34 2015 +0200
     3.3 @@ -3,5 +3,5 @@
     3.4    <package id="LibMiniDisplay" version="1.1.4" targetFramework="net45" />
     3.5    <package id="NAudio" version="1.7.3" targetFramework="net45" />
     3.6    <package id="SharpLibHid" version="1.2.1" targetFramework="net45" />
     3.7 -  <package id="SharpLibWin32" version="0.0.4" targetFramework="net45" />
     3.8 +  <package id="SharpLibWin32" version="0.0.6" targetFramework="net45" />
     3.9  </packages>
    3.10 \ No newline at end of file