Server/MainForm.Hid.cs
author StephaneLenclud
Fri, 15 Apr 2016 14:32:45 +0200
changeset 198 ac5fb7de7f1b
parent 161 7b19bea5d73a
permissions -rw-r--r--
Adding empty FX control.
StephaneLenclud@125
     1
using System;
StephaneLenclud@126
     2
using System.IO;
StephaneLenclud@125
     3
using System.Collections.Generic;
StephaneLenclud@125
     4
using System.Linq;
StephaneLenclud@125
     5
using System.Text;
StephaneLenclud@125
     6
using System.Threading.Tasks;
StephaneLenclud@125
     7
using System.Diagnostics;
StephaneLenclud@125
     8
using System.Runtime.InteropServices;
StephaneLenclud@125
     9
using System.Windows.Forms;
StephaneLenclud@150
    10
using Microsoft.Win32.SafeHandles;
StephaneLenclud@155
    11
using System.ComponentModel;
StephaneLenclud@125
    12
//
StephaneLenclud@125
    13
using Hid = SharpLib.Hid;
StephaneLenclud@125
    14
using SharpLib.Win32;
StephaneLenclud@125
    15
StephaneLenclud@125
    16
namespace SharpDisplayManager
StephaneLenclud@125
    17
{
StephaneLenclud@138
    18
    /// <summary>
StephaneLenclud@138
    19
    /// Implement handling of HID input reports notably to be able to launch an application using the Green Start button from IR remotes.
StephaneLenclud@138
    20
    /// </summary>
StephaneLenclud@131
    21
    [System.ComponentModel.DesignerCategory("Code")]
StephaneLenclud@131
    22
    public class MainFormHid : Form
StephaneLenclud@131
    23
    {
StephaneLenclud@131
    24
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SwitchToThisWindow")]
StephaneLenclud@131
    25
        public static extern void SwitchToThisWindow([System.Runtime.InteropServices.InAttribute()] System.IntPtr hwnd, [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fUnknown);
StephaneLenclud@131
    26
        //
StephaneLenclud@131
    27
        public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent);
StephaneLenclud@126
    28
StephaneLenclud@131
    29
        /// <summary>
StephaneLenclud@131
    30
        /// Use notably to handle green start key from IR remote control
StephaneLenclud@131
    31
        /// </summary>
StephaneLenclud@131
    32
        private Hid.Handler iHidHandler;
StephaneLenclud@126
    33
StephaneLenclud@131
    34
        /// <summary>
StephaneLenclud@131
    35
        /// Register HID devices so that we receive corresponding WM_INPUT messages.
StephaneLenclud@131
    36
        /// </summary>
StephaneLenclud@131
    37
        protected void RegisterHidDevices()
StephaneLenclud@131
    38
        {
StephaneLenclud@131
    39
            // Register the input device to receive the commands from the
StephaneLenclud@131
    40
            // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
StephaneLenclud@131
    41
            // for the vendor defined usage page.
StephaneLenclud@128
    42
StephaneLenclud@131
    43
            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[5];
StephaneLenclud@128
    44
StephaneLenclud@131
    45
            int i = 0;
StephaneLenclud@131
    46
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
StephaneLenclud@131
    47
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
StephaneLenclud@131
    48
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    49
            rid[i].hwndTarget = Handle;
StephaneLenclud@128
    50
StephaneLenclud@131
    51
            i++;
StephaneLenclud@131
    52
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    53
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
StephaneLenclud@131
    54
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    55
            rid[i].hwndTarget = Handle;
StephaneLenclud@126
    56
StephaneLenclud@131
    57
            i++;
StephaneLenclud@131
    58
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
StephaneLenclud@131
    59
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
StephaneLenclud@131
    60
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    61
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    62
StephaneLenclud@131
    63
            i++;
StephaneLenclud@131
    64
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    65
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
StephaneLenclud@131
    66
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    67
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    68
StephaneLenclud@131
    69
            i++;
StephaneLenclud@131
    70
            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    71
            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
StephaneLenclud@131
    72
            rid[i].dwFlags = Const.RIDEV_INPUTSINK;
StephaneLenclud@131
    73
            rid[i].hwndTarget = Handle;
StephaneLenclud@125
    74
StephaneLenclud@131
    75
            //i++;
StephaneLenclud@131
    76
            //rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    77
            //rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
StephaneLenclud@131
    78
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    79
            //rid[i].hwndTarget = Handle;
StephaneLenclud@125
    80
StephaneLenclud@131
    81
            //i++;
StephaneLenclud@131
    82
            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
StephaneLenclud@131
    83
            //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
StephaneLenclud@131
    84
            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
StephaneLenclud@131
    85
            //rid[i].hwndTarget = aHWND;
StephaneLenclud@125
    86
StephaneLenclud@125
    87
StephaneLenclud@131
    88
            iHidHandler = new SharpLib.Hid.Handler(rid);
StephaneLenclud@131
    89
            if (!iHidHandler.IsRegistered)
StephaneLenclud@131
    90
            {
StephaneLenclud@131
    91
                Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
StephaneLenclud@131
    92
            }
StephaneLenclud@131
    93
            iHidHandler.OnHidEvent += HandleHidEventThreadSafe;
StephaneLenclud@159
    94
StephaneLenclud@131
    95
        }
StephaneLenclud@125
    96
StephaneLenclud@159
    97
StephaneLenclud@159
    98
StephaneLenclud@159
    99
StephaneLenclud@131
   100
        /// <summary>
StephaneLenclud@131
   101
        /// Here we receive HID events from our HID library.
StephaneLenclud@131
   102
        /// </summary>
StephaneLenclud@131
   103
        /// <param name="aSender"></param>
StephaneLenclud@131
   104
        /// <param name="aHidEvent"></param>
StephaneLenclud@131
   105
        public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
StephaneLenclud@131
   106
        {
StephaneLenclud@131
   107
            if (aHidEvent.IsStray)
StephaneLenclud@131
   108
            {
StephaneLenclud@131
   109
                //Stray event just ignore it
StephaneLenclud@131
   110
                return;
StephaneLenclud@131
   111
            }
StephaneLenclud@125
   112
StephaneLenclud@131
   113
            if (this.InvokeRequired)
StephaneLenclud@131
   114
            {
StephaneLenclud@131
   115
                //Not in the proper thread, invoke ourselves
StephaneLenclud@131
   116
                OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
StephaneLenclud@131
   117
                this.Invoke(d, new object[] { aSender, aHidEvent });
StephaneLenclud@131
   118
            }
StephaneLenclud@131
   119
            else
StephaneLenclud@131
   120
            {
StephaneLenclud@150
   121
                if (aHidEvent.Usages.Count == 0)
StephaneLenclud@150
   122
                {
StephaneLenclud@150
   123
                    //No usage, nothing to do then
StephaneLenclud@150
   124
                    return;
StephaneLenclud@150
   125
                }
StephaneLenclud@150
   126
StephaneLenclud@131
   127
                //We are in the proper thread
StephaneLenclud@150
   128
                if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl)
StephaneLenclud@131
   129
                {
StephaneLenclud@150
   130
                    switch (aHidEvent.Usages[0])
StephaneLenclud@131
   131
                    {
StephaneLenclud@150
   132
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
StephaneLenclud@150
   133
                            HandleGreenStart();
StephaneLenclud@150
   134
                            break;
StephaneLenclud@150
   135
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
StephaneLenclud@150
   136
                        case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
StephaneLenclud@150
   137
                            HandleEject();
StephaneLenclud@150
   138
                            break;
StephaneLenclud@131
   139
                    }
StephaneLenclud@131
   140
                }
StephaneLenclud@152
   141
StephaneLenclud@152
   142
                //Keep this for debug when only ThinkPad keyboard is available
StephaneLenclud@152
   143
                if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier)
StephaneLenclud@152
   144
                {
StephaneLenclud@152
   145
                    HandleEject();
StephaneLenclud@152
   146
                }
StephaneLenclud@152
   147
StephaneLenclud@131
   148
            }
StephaneLenclud@131
   149
        }
StephaneLenclud@125
   150
StephaneLenclud@151
   151
        /// <summary>
StephaneLenclud@151
   152
        /// 
StephaneLenclud@151
   153
        /// </summary>
StephaneLenclud@155
   154
        /// <param name="aPrefix"></param>
StephaneLenclud@155
   155
        private void CheckLastError(string aPrefix)
StephaneLenclud@155
   156
        {
StephaneLenclud@155
   157
            string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
StephaneLenclud@155
   158
            Debug.WriteLine(aPrefix + Marshal.GetLastWin32Error().ToString() + ": " + errorMessage);
StephaneLenclud@155
   159
        }
StephaneLenclud@155
   160
StephaneLenclud@155
   161
        /// <summary>
StephaneLenclud@155
   162
        /// 
StephaneLenclud@155
   163
        /// </summary>
StephaneLenclud@151
   164
        /// <param name="data"></param>
StephaneLenclud@151
   165
        /// <returns></returns>
StephaneLenclud@151
   166
        private IntPtr MarshalToPointer(object data)
StephaneLenclud@151
   167
        {
StephaneLenclud@151
   168
            IntPtr buf = Marshal.AllocHGlobal(
StephaneLenclud@151
   169
                Marshal.SizeOf(data));
StephaneLenclud@151
   170
            Marshal.StructureToPtr(data,
StephaneLenclud@151
   171
                buf, false);
StephaneLenclud@151
   172
            return buf;
StephaneLenclud@151
   173
        }
StephaneLenclud@151
   174
StephaneLenclud@151
   175
        /// <summary>
StephaneLenclud@151
   176
        /// 
StephaneLenclud@151
   177
        /// </summary>
StephaneLenclud@151
   178
        /// <returns></returns>
StephaneLenclud@152
   179
        private SafeFileHandle OpenVolume(string aDriveName)
StephaneLenclud@150
   180
        {
StephaneLenclud@152
   181
            return Function.CreateFile("\\\\.\\" + aDriveName,
StephaneLenclud@150
   182
                               SharpLib.Win32.FileAccess.GENERIC_READ,
StephaneLenclud@150
   183
                               SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
StephaneLenclud@150
   184
                               IntPtr.Zero,
StephaneLenclud@150
   185
                               CreationDisposition.OPEN_EXISTING,
StephaneLenclud@150
   186
                               0,
StephaneLenclud@150
   187
                               IntPtr.Zero);
StephaneLenclud@150
   188
        }
StephaneLenclud@150
   189
StephaneLenclud@150
   190
        /// <summary>
StephaneLenclud@150
   191
        /// 
StephaneLenclud@150
   192
        /// </summary>
StephaneLenclud@151
   193
        /// <param name="aVolume"></param>
StephaneLenclud@151
   194
        /// <returns></returns>
StephaneLenclud@151
   195
        private bool LockVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   196
        {
StephaneLenclud@151
   197
            //Hope that's doing what I think it does
StephaneLenclud@151
   198
            IntPtr dwBytesReturned=new IntPtr();
StephaneLenclud@151
   199
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   200
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   201
StephaneLenclud@151
   202
            int tries = 0;
StephaneLenclud@151
   203
            const int KMaxTries = 100;
StephaneLenclud@151
   204
            const int KSleepTime = 10;
StephaneLenclud@151
   205
            bool success = false;
StephaneLenclud@151
   206
StephaneLenclud@151
   207
            while (!success && tries < KMaxTries)
StephaneLenclud@151
   208
            {
StephaneLenclud@151
   209
                success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@151
   210
                System.Threading.Thread.Sleep(KSleepTime);
StephaneLenclud@151
   211
                tries++;
StephaneLenclud@151
   212
            }
StephaneLenclud@151
   213
StephaneLenclud@155
   214
            CheckLastError("Lock volume: ");
StephaneLenclud@155
   215
StephaneLenclud@151
   216
            return success;
StephaneLenclud@151
   217
        }
StephaneLenclud@151
   218
StephaneLenclud@151
   219
        /// <summary>
StephaneLenclud@151
   220
        /// 
StephaneLenclud@151
   221
        /// </summary>
StephaneLenclud@151
   222
        /// <param name="aVolume"></param>
StephaneLenclud@151
   223
        /// <returns></returns>
StephaneLenclud@151
   224
        private bool DismountVolume(SafeFileHandle aVolume)
StephaneLenclud@151
   225
        {
StephaneLenclud@151
   226
            //Hope that's doing what I think it does
StephaneLenclud@151
   227
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   228
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   229
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   230
StephaneLenclud@155
   231
            bool res = Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   232
            CheckLastError("Dismount volume: ");
StephaneLenclud@155
   233
            return res;
StephaneLenclud@151
   234
        }
StephaneLenclud@151
   235
StephaneLenclud@151
   236
StephaneLenclud@151
   237
StephaneLenclud@151
   238
        /// <summary>
StephaneLenclud@151
   239
        /// 
StephaneLenclud@151
   240
        /// </summary>
StephaneLenclud@151
   241
        /// <param name="aVolume"></param>
StephaneLenclud@151
   242
        /// <param name="aPreventRemoval"></param>
StephaneLenclud@151
   243
        /// <returns></returns>
StephaneLenclud@151
   244
        private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval)
StephaneLenclud@151
   245
        {
StephaneLenclud@151
   246
            //Hope that's doing what I think it does
StephaneLenclud@151
   247
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   248
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   249
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@151
   250
            //
StephaneLenclud@151
   251
            PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL();
StephaneLenclud@151
   252
            preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval);
StephaneLenclud@151
   253
            IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval);
StephaneLenclud@151
   254
StephaneLenclud@151
   255
            bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   256
            CheckLastError("Media removal: ");
StephaneLenclud@151
   257
            Marshal.FreeHGlobal(preventMediaRemovalParam);
StephaneLenclud@151
   258
StephaneLenclud@151
   259
            return result;
StephaneLenclud@151
   260
        }
StephaneLenclud@151
   261
StephaneLenclud@151
   262
        /// <summary>
StephaneLenclud@154
   263
        /// Eject optical drive media opening the tray if any.
StephaneLenclud@151
   264
        /// </summary>
StephaneLenclud@151
   265
        /// <param name="aVolume"></param>
StephaneLenclud@151
   266
        /// <returns></returns>
StephaneLenclud@154
   267
        private bool MediaEject(SafeFileHandle aVolume)
StephaneLenclud@151
   268
        {
StephaneLenclud@151
   269
            //Hope that's doing what I think it does
StephaneLenclud@151
   270
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@151
   271
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@151
   272
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@151
   273
StephaneLenclud@155
   274
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   275
            CheckLastError("Media eject: ");
StephaneLenclud@155
   276
            return res;
StephaneLenclud@151
   277
        }
StephaneLenclud@151
   278
StephaneLenclud@152
   279
        /// <summary>
StephaneLenclud@154
   280
        /// Close an optical drive tray.
StephaneLenclud@152
   281
        /// </summary>
StephaneLenclud@152
   282
        /// <param name="aVolume"></param>
StephaneLenclud@152
   283
        /// <returns></returns>
StephaneLenclud@154
   284
        private bool MediaLoad(SafeFileHandle aVolume)
StephaneLenclud@152
   285
        {
StephaneLenclud@152
   286
            //Hope that's doing what I think it does
StephaneLenclud@152
   287
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@152
   288
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@152
   289
            OVERLAPPED overlapped=new OVERLAPPED();
StephaneLenclud@152
   290
StephaneLenclud@155
   291
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@155
   292
            CheckLastError("Media load: ");
StephaneLenclud@155
   293
            return res;
StephaneLenclud@152
   294
        }
StephaneLenclud@152
   295
StephaneLenclud@154
   296
        /// <summary>
StephaneLenclud@154
   297
        /// 
StephaneLenclud@154
   298
        /// </summary>
StephaneLenclud@154
   299
        /// <param name="aVolume"></param>
StephaneLenclud@154
   300
        /// <returns></returns>
StephaneLenclud@154
   301
        private bool StorageCheckVerify(SafeFileHandle aVolume)
StephaneLenclud@154
   302
        {
StephaneLenclud@154
   303
            //Hope that's doing what I think it does
StephaneLenclud@154
   304
            IntPtr dwBytesReturned = new IntPtr();
StephaneLenclud@154
   305
            //Should not be needed but I'm not sure how to pass NULL in there.
StephaneLenclud@154
   306
            OVERLAPPED overlapped = new OVERLAPPED();
StephaneLenclud@154
   307
StephaneLenclud@154
   308
            bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
StephaneLenclud@154
   309
StephaneLenclud@155
   310
            CheckLastError("Check verify: ");
StephaneLenclud@154
   311
StephaneLenclud@154
   312
            return res;
StephaneLenclud@154
   313
        }        
StephaneLenclud@151
   314
        
StephaneLenclud@151
   315
StephaneLenclud@151
   316
StephaneLenclud@151
   317
        /// <summary>
StephaneLenclud@151
   318
        /// Perform media ejection.
StephaneLenclud@151
   319
        /// </summary>
StephaneLenclud@150
   320
        private void HandleEject()
StephaneLenclud@150
   321
        {
StephaneLenclud@153
   322
            string drive = ((MainForm)this).OpticalDriveToEject();
StephaneLenclud@153
   323
            if (drive.Length!=2)
StephaneLenclud@153
   324
            {
StephaneLenclud@153
   325
                //Not a proper drive spec.
StephaneLenclud@153
   326
                //Probably 'None' selected.
StephaneLenclud@153
   327
                return;
StephaneLenclud@153
   328
            }
StephaneLenclud@153
   329
StephaneLenclud@153
   330
            SafeFileHandle handle = OpenVolume(drive);
StephaneLenclud@151
   331
            if (handle.IsInvalid)
StephaneLenclud@151
   332
            {
StephaneLenclud@155
   333
                CheckLastError("ERROR: Failed to open volume: ");
StephaneLenclud@151
   334
                return;
StephaneLenclud@151
   335
            }
StephaneLenclud@151
   336
StephaneLenclud@151
   337
            if (LockVolume(handle) && DismountVolume(handle))
StephaneLenclud@151
   338
            {
StephaneLenclud@154
   339
                Debug.WriteLine("Volume was dismounted.");
StephaneLenclud@151
   340
StephaneLenclud@152
   341
                if (PreventRemovalOfVolume(handle,false))
StephaneLenclud@151
   342
                {
StephaneLenclud@156
   343
                    //StorageCheckVerify(handle);
StephaneLenclud@154
   344
StephaneLenclud@156
   345
                    DateTime before;
StephaneLenclud@156
   346
                    before = DateTime.Now;
StephaneLenclud@156
   347
                    bool ejectSuccess = MediaEject(handle);
StephaneLenclud@156
   348
                    double ms = (DateTime.Now - before).TotalMilliseconds;
StephaneLenclud@156
   349
StephaneLenclud@156
   350
                    //We assume that if it take more than a certain time to for eject to execute it means we actually ejected.
StephaneLenclud@156
   351
                    //If our eject completes too rapidly we assume the tray is already open and we will try to close it. 
StephaneLenclud@156
   352
                    if (ejectSuccess && ms > 100)
StephaneLenclud@152
   353
                    {
StephaneLenclud@154
   354
                        Debug.WriteLine("Media was ejected");
StephaneLenclud@152
   355
                    }
StephaneLenclud@154
   356
                    else if (MediaLoad(handle))
StephaneLenclud@154
   357
                    {
StephaneLenclud@154
   358
                        Debug.WriteLine("Media was loaded");
StephaneLenclud@154
   359
                    }                    
StephaneLenclud@151
   360
                }
StephaneLenclud@151
   361
            }
StephaneLenclud@154
   362
            else
StephaneLenclud@154
   363
            {
StephaneLenclud@154
   364
                Debug.WriteLine("Volume lock or dismount failed.");
StephaneLenclud@154
   365
            }
StephaneLenclud@154
   366
StephaneLenclud@154
   367
            //This is needed to make sure we can open the volume next time around
StephaneLenclud@154
   368
            handle.Dispose();
StephaneLenclud@150
   369
        }
StephaneLenclud@150
   370
StephaneLenclud@150
   371
        /// <summary>
StephaneLenclud@150
   372
        /// 
StephaneLenclud@150
   373
        /// </summary>
StephaneLenclud@150
   374
        private void HandleGreenStart()
StephaneLenclud@150
   375
        {
StephaneLenclud@150
   376
            //First check if the process we want to launch already exists
StephaneLenclud@150
   377
            string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   378
            Process[] existingProcesses = Process.GetProcessesByName(procName);
StephaneLenclud@150
   379
            if (existingProcesses == null || existingProcesses.Length == 0)
StephaneLenclud@150
   380
            {
StephaneLenclud@150
   381
                // Process do not exists just try to launch it
StephaneLenclud@150
   382
                ProcessStartInfo start = new ProcessStartInfo();
StephaneLenclud@150
   383
                // Enter in the command line arguments, everything you would enter after the executable name itself
StephaneLenclud@150
   384
                //start.Arguments = arguments; 
StephaneLenclud@150
   385
                // Enter the executable to run, including the complete path
StephaneLenclud@150
   386
                start.FileName = Properties.Settings.Default.StartFileName;
StephaneLenclud@150
   387
                start.WindowStyle = ProcessWindowStyle.Normal;
StephaneLenclud@150
   388
                start.CreateNoWindow = true;
StephaneLenclud@150
   389
                start.UseShellExecute = true;
StephaneLenclud@150
   390
                // Run the external process & wait for it to finish
StephaneLenclud@150
   391
                Process proc = Process.Start(start);
StephaneLenclud@150
   392
StephaneLenclud@150
   393
                //SL: We could have used that too
StephaneLenclud@150
   394
                //Shell32.Shell shell = new Shell32.Shell();
StephaneLenclud@150
   395
                //shell.ShellExecute(Properties.Settings.Default.StartFileName);
StephaneLenclud@150
   396
            }
StephaneLenclud@150
   397
            else
StephaneLenclud@150
   398
            {
StephaneLenclud@150
   399
                //This won't work properly until we have a manifest that enables uiAccess.
StephaneLenclud@150
   400
                //However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
StephaneLenclud@150
   401
                SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
StephaneLenclud@150
   402
            }            
StephaneLenclud@150
   403
        }
StephaneLenclud@167
   404
StephaneLenclud@167
   405
StephaneLenclud@131
   406
        /// <summary>
StephaneLenclud@131
   407
        /// We need to handle WM_INPUT.
StephaneLenclud@131
   408
        /// </summary>
StephaneLenclud@131
   409
        /// <param name="message"></param>
StephaneLenclud@131
   410
        protected override void WndProc(ref Message message)
StephaneLenclud@131
   411
        {
StephaneLenclud@131
   412
            switch (message.Msg)
StephaneLenclud@131
   413
            {
StephaneLenclud@131
   414
                case Const.WM_INPUT:
StephaneLenclud@131
   415
                    //Returning zero means we processed that message.
StephaneLenclud@131
   416
                    message.Result = new IntPtr(0);
StephaneLenclud@131
   417
                    iHidHandler.ProcessInput(ref message);
StephaneLenclud@131
   418
                    break;
StephaneLenclud@131
   419
            }
StephaneLenclud@159
   420
StephaneLenclud@167
   421
            //Pass this on to base class.
StephaneLenclud@131
   422
            base.WndProc(ref message);
StephaneLenclud@131
   423
        }
StephaneLenclud@131
   424
    }
StephaneLenclud@125
   425
}