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@131
|
94 |
}
|
StephaneLenclud@125
|
95 |
|
StephaneLenclud@131
|
96 |
/// <summary>
|
StephaneLenclud@131
|
97 |
/// Here we receive HID events from our HID library.
|
StephaneLenclud@131
|
98 |
/// </summary>
|
StephaneLenclud@131
|
99 |
/// <param name="aSender"></param>
|
StephaneLenclud@131
|
100 |
/// <param name="aHidEvent"></param>
|
StephaneLenclud@131
|
101 |
public void HandleHidEventThreadSafe(object aSender, SharpLib.Hid.Event aHidEvent)
|
StephaneLenclud@131
|
102 |
{
|
StephaneLenclud@131
|
103 |
if (aHidEvent.IsStray)
|
StephaneLenclud@131
|
104 |
{
|
StephaneLenclud@131
|
105 |
//Stray event just ignore it
|
StephaneLenclud@131
|
106 |
return;
|
StephaneLenclud@131
|
107 |
}
|
StephaneLenclud@125
|
108 |
|
StephaneLenclud@131
|
109 |
if (this.InvokeRequired)
|
StephaneLenclud@131
|
110 |
{
|
StephaneLenclud@131
|
111 |
//Not in the proper thread, invoke ourselves
|
StephaneLenclud@131
|
112 |
OnHidEventDelegate d = new OnHidEventDelegate(HandleHidEventThreadSafe);
|
StephaneLenclud@131
|
113 |
this.Invoke(d, new object[] { aSender, aHidEvent });
|
StephaneLenclud@131
|
114 |
}
|
StephaneLenclud@131
|
115 |
else
|
StephaneLenclud@131
|
116 |
{
|
StephaneLenclud@150
|
117 |
if (aHidEvent.Usages.Count == 0)
|
StephaneLenclud@150
|
118 |
{
|
StephaneLenclud@150
|
119 |
//No usage, nothing to do then
|
StephaneLenclud@150
|
120 |
return;
|
StephaneLenclud@150
|
121 |
}
|
StephaneLenclud@150
|
122 |
|
StephaneLenclud@131
|
123 |
//We are in the proper thread
|
StephaneLenclud@150
|
124 |
if (aHidEvent.UsagePage == (ushort) Hid.UsagePage.WindowsMediaCenterRemoteControl)
|
StephaneLenclud@131
|
125 |
{
|
StephaneLenclud@150
|
126 |
switch (aHidEvent.Usages[0])
|
StephaneLenclud@131
|
127 |
{
|
StephaneLenclud@150
|
128 |
case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.GreenStart:
|
StephaneLenclud@150
|
129 |
HandleGreenStart();
|
StephaneLenclud@150
|
130 |
break;
|
StephaneLenclud@150
|
131 |
case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Eject:
|
StephaneLenclud@150
|
132 |
case (ushort)Hid.Usage.WindowsMediaCenterRemoteControl.Ext2:
|
StephaneLenclud@150
|
133 |
HandleEject();
|
StephaneLenclud@150
|
134 |
break;
|
StephaneLenclud@131
|
135 |
}
|
StephaneLenclud@131
|
136 |
}
|
StephaneLenclud@152
|
137 |
|
StephaneLenclud@152
|
138 |
//Keep this for debug when only ThinkPad keyboard is available
|
StephaneLenclud@152
|
139 |
if (aHidEvent.UsagePage == (ushort)Hid.UsagePage.Consumer && aHidEvent.Usages[0] == (ushort)Hid.Usage.ConsumerControl.ThinkPadFullscreenMagnifier)
|
StephaneLenclud@152
|
140 |
{
|
StephaneLenclud@152
|
141 |
HandleEject();
|
StephaneLenclud@152
|
142 |
}
|
StephaneLenclud@152
|
143 |
|
StephaneLenclud@131
|
144 |
}
|
StephaneLenclud@131
|
145 |
}
|
StephaneLenclud@125
|
146 |
|
StephaneLenclud@151
|
147 |
/// <summary>
|
StephaneLenclud@151
|
148 |
///
|
StephaneLenclud@151
|
149 |
/// </summary>
|
StephaneLenclud@155
|
150 |
/// <param name="aPrefix"></param>
|
StephaneLenclud@155
|
151 |
private void CheckLastError(string aPrefix)
|
StephaneLenclud@155
|
152 |
{
|
StephaneLenclud@155
|
153 |
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
|
StephaneLenclud@155
|
154 |
Debug.WriteLine(aPrefix + Marshal.GetLastWin32Error().ToString() + ": " + errorMessage);
|
StephaneLenclud@155
|
155 |
}
|
StephaneLenclud@155
|
156 |
|
StephaneLenclud@155
|
157 |
/// <summary>
|
StephaneLenclud@155
|
158 |
///
|
StephaneLenclud@155
|
159 |
/// </summary>
|
StephaneLenclud@151
|
160 |
/// <param name="data"></param>
|
StephaneLenclud@151
|
161 |
/// <returns></returns>
|
StephaneLenclud@151
|
162 |
private IntPtr MarshalToPointer(object data)
|
StephaneLenclud@151
|
163 |
{
|
StephaneLenclud@151
|
164 |
IntPtr buf = Marshal.AllocHGlobal(
|
StephaneLenclud@151
|
165 |
Marshal.SizeOf(data));
|
StephaneLenclud@151
|
166 |
Marshal.StructureToPtr(data,
|
StephaneLenclud@151
|
167 |
buf, false);
|
StephaneLenclud@151
|
168 |
return buf;
|
StephaneLenclud@151
|
169 |
}
|
StephaneLenclud@151
|
170 |
|
StephaneLenclud@151
|
171 |
/// <summary>
|
StephaneLenclud@151
|
172 |
///
|
StephaneLenclud@151
|
173 |
/// </summary>
|
StephaneLenclud@151
|
174 |
/// <returns></returns>
|
StephaneLenclud@152
|
175 |
private SafeFileHandle OpenVolume(string aDriveName)
|
StephaneLenclud@150
|
176 |
{
|
StephaneLenclud@152
|
177 |
return Function.CreateFile("\\\\.\\" + aDriveName,
|
StephaneLenclud@150
|
178 |
SharpLib.Win32.FileAccess.GENERIC_READ,
|
StephaneLenclud@150
|
179 |
SharpLib.Win32.FileShare.FILE_SHARE_READ | SharpLib.Win32.FileShare.FILE_SHARE_WRITE,
|
StephaneLenclud@150
|
180 |
IntPtr.Zero,
|
StephaneLenclud@150
|
181 |
CreationDisposition.OPEN_EXISTING,
|
StephaneLenclud@150
|
182 |
0,
|
StephaneLenclud@150
|
183 |
IntPtr.Zero);
|
StephaneLenclud@150
|
184 |
}
|
StephaneLenclud@150
|
185 |
|
StephaneLenclud@150
|
186 |
/// <summary>
|
StephaneLenclud@150
|
187 |
///
|
StephaneLenclud@150
|
188 |
/// </summary>
|
StephaneLenclud@151
|
189 |
/// <param name="aVolume"></param>
|
StephaneLenclud@151
|
190 |
/// <returns></returns>
|
StephaneLenclud@151
|
191 |
private bool LockVolume(SafeFileHandle aVolume)
|
StephaneLenclud@151
|
192 |
{
|
StephaneLenclud@151
|
193 |
//Hope that's doing what I think it does
|
StephaneLenclud@151
|
194 |
IntPtr dwBytesReturned=new IntPtr();
|
StephaneLenclud@151
|
195 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@151
|
196 |
OVERLAPPED overlapped=new OVERLAPPED();
|
StephaneLenclud@151
|
197 |
|
StephaneLenclud@151
|
198 |
int tries = 0;
|
StephaneLenclud@151
|
199 |
const int KMaxTries = 100;
|
StephaneLenclud@151
|
200 |
const int KSleepTime = 10;
|
StephaneLenclud@151
|
201 |
bool success = false;
|
StephaneLenclud@151
|
202 |
|
StephaneLenclud@151
|
203 |
while (!success && tries < KMaxTries)
|
StephaneLenclud@151
|
204 |
{
|
StephaneLenclud@151
|
205 |
success = Function.DeviceIoControl(aVolume, Const.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@151
|
206 |
System.Threading.Thread.Sleep(KSleepTime);
|
StephaneLenclud@151
|
207 |
tries++;
|
StephaneLenclud@151
|
208 |
}
|
StephaneLenclud@151
|
209 |
|
StephaneLenclud@155
|
210 |
CheckLastError("Lock volume: ");
|
StephaneLenclud@155
|
211 |
|
StephaneLenclud@151
|
212 |
return success;
|
StephaneLenclud@151
|
213 |
}
|
StephaneLenclud@151
|
214 |
|
StephaneLenclud@151
|
215 |
/// <summary>
|
StephaneLenclud@151
|
216 |
///
|
StephaneLenclud@151
|
217 |
/// </summary>
|
StephaneLenclud@151
|
218 |
/// <param name="aVolume"></param>
|
StephaneLenclud@151
|
219 |
/// <returns></returns>
|
StephaneLenclud@151
|
220 |
private bool DismountVolume(SafeFileHandle aVolume)
|
StephaneLenclud@151
|
221 |
{
|
StephaneLenclud@151
|
222 |
//Hope that's doing what I think it does
|
StephaneLenclud@151
|
223 |
IntPtr dwBytesReturned = new IntPtr();
|
StephaneLenclud@151
|
224 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@151
|
225 |
OVERLAPPED overlapped=new OVERLAPPED();
|
StephaneLenclud@151
|
226 |
|
StephaneLenclud@155
|
227 |
bool res = Function.DeviceIoControl(aVolume, Const.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@155
|
228 |
CheckLastError("Dismount volume: ");
|
StephaneLenclud@155
|
229 |
return res;
|
StephaneLenclud@151
|
230 |
}
|
StephaneLenclud@151
|
231 |
|
StephaneLenclud@151
|
232 |
|
StephaneLenclud@151
|
233 |
|
StephaneLenclud@151
|
234 |
/// <summary>
|
StephaneLenclud@151
|
235 |
///
|
StephaneLenclud@151
|
236 |
/// </summary>
|
StephaneLenclud@151
|
237 |
/// <param name="aVolume"></param>
|
StephaneLenclud@151
|
238 |
/// <param name="aPreventRemoval"></param>
|
StephaneLenclud@151
|
239 |
/// <returns></returns>
|
StephaneLenclud@151
|
240 |
private bool PreventRemovalOfVolume(SafeFileHandle aVolume, bool aPreventRemoval)
|
StephaneLenclud@151
|
241 |
{
|
StephaneLenclud@151
|
242 |
//Hope that's doing what I think it does
|
StephaneLenclud@151
|
243 |
IntPtr dwBytesReturned = new IntPtr();
|
StephaneLenclud@151
|
244 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@151
|
245 |
OVERLAPPED overlapped = new OVERLAPPED();
|
StephaneLenclud@151
|
246 |
//
|
StephaneLenclud@151
|
247 |
PREVENT_MEDIA_REMOVAL preventMediaRemoval = new PREVENT_MEDIA_REMOVAL();
|
StephaneLenclud@151
|
248 |
preventMediaRemoval.PreventMediaRemoval = Convert.ToByte(aPreventRemoval);
|
StephaneLenclud@151
|
249 |
IntPtr preventMediaRemovalParam = MarshalToPointer(preventMediaRemoval);
|
StephaneLenclud@151
|
250 |
|
StephaneLenclud@151
|
251 |
bool result = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_MEDIA_REMOVAL, preventMediaRemovalParam, Convert.ToUInt32(Marshal.SizeOf(preventMediaRemoval)), IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@155
|
252 |
CheckLastError("Media removal: ");
|
StephaneLenclud@151
|
253 |
Marshal.FreeHGlobal(preventMediaRemovalParam);
|
StephaneLenclud@151
|
254 |
|
StephaneLenclud@151
|
255 |
return result;
|
StephaneLenclud@151
|
256 |
}
|
StephaneLenclud@151
|
257 |
|
StephaneLenclud@151
|
258 |
/// <summary>
|
StephaneLenclud@154
|
259 |
/// Eject optical drive media opening the tray if any.
|
StephaneLenclud@151
|
260 |
/// </summary>
|
StephaneLenclud@151
|
261 |
/// <param name="aVolume"></param>
|
StephaneLenclud@151
|
262 |
/// <returns></returns>
|
StephaneLenclud@154
|
263 |
private bool MediaEject(SafeFileHandle aVolume)
|
StephaneLenclud@151
|
264 |
{
|
StephaneLenclud@151
|
265 |
//Hope that's doing what I think it does
|
StephaneLenclud@151
|
266 |
IntPtr dwBytesReturned = new IntPtr();
|
StephaneLenclud@151
|
267 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@151
|
268 |
OVERLAPPED overlapped=new OVERLAPPED();
|
StephaneLenclud@151
|
269 |
|
StephaneLenclud@155
|
270 |
bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@155
|
271 |
CheckLastError("Media eject: ");
|
StephaneLenclud@155
|
272 |
return res;
|
StephaneLenclud@151
|
273 |
}
|
StephaneLenclud@151
|
274 |
|
StephaneLenclud@152
|
275 |
/// <summary>
|
StephaneLenclud@154
|
276 |
/// Close an optical drive tray.
|
StephaneLenclud@152
|
277 |
/// </summary>
|
StephaneLenclud@152
|
278 |
/// <param name="aVolume"></param>
|
StephaneLenclud@152
|
279 |
/// <returns></returns>
|
StephaneLenclud@154
|
280 |
private bool MediaLoad(SafeFileHandle aVolume)
|
StephaneLenclud@152
|
281 |
{
|
StephaneLenclud@152
|
282 |
//Hope that's doing what I think it does
|
StephaneLenclud@152
|
283 |
IntPtr dwBytesReturned = new IntPtr();
|
StephaneLenclud@152
|
284 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@152
|
285 |
OVERLAPPED overlapped=new OVERLAPPED();
|
StephaneLenclud@152
|
286 |
|
StephaneLenclud@155
|
287 |
bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_LOAD_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@155
|
288 |
CheckLastError("Media load: ");
|
StephaneLenclud@155
|
289 |
return res;
|
StephaneLenclud@152
|
290 |
}
|
StephaneLenclud@152
|
291 |
|
StephaneLenclud@154
|
292 |
/// <summary>
|
StephaneLenclud@154
|
293 |
///
|
StephaneLenclud@154
|
294 |
/// </summary>
|
StephaneLenclud@154
|
295 |
/// <param name="aVolume"></param>
|
StephaneLenclud@154
|
296 |
/// <returns></returns>
|
StephaneLenclud@154
|
297 |
private bool StorageCheckVerify(SafeFileHandle aVolume)
|
StephaneLenclud@154
|
298 |
{
|
StephaneLenclud@154
|
299 |
//Hope that's doing what I think it does
|
StephaneLenclud@154
|
300 |
IntPtr dwBytesReturned = new IntPtr();
|
StephaneLenclud@154
|
301 |
//Should not be needed but I'm not sure how to pass NULL in there.
|
StephaneLenclud@154
|
302 |
OVERLAPPED overlapped = new OVERLAPPED();
|
StephaneLenclud@154
|
303 |
|
StephaneLenclud@154
|
304 |
bool res = Function.DeviceIoControl(aVolume, Const.IOCTL_STORAGE_CHECK_VERIFY2, IntPtr.Zero, 0, IntPtr.Zero, 0, dwBytesReturned, ref overlapped);
|
StephaneLenclud@154
|
305 |
|
StephaneLenclud@155
|
306 |
CheckLastError("Check verify: ");
|
StephaneLenclud@154
|
307 |
|
StephaneLenclud@154
|
308 |
return res;
|
StephaneLenclud@154
|
309 |
}
|
StephaneLenclud@151
|
310 |
|
StephaneLenclud@151
|
311 |
|
StephaneLenclud@151
|
312 |
|
StephaneLenclud@151
|
313 |
/// <summary>
|
StephaneLenclud@151
|
314 |
/// Perform media ejection.
|
StephaneLenclud@151
|
315 |
/// </summary>
|
StephaneLenclud@150
|
316 |
private void HandleEject()
|
StephaneLenclud@150
|
317 |
{
|
StephaneLenclud@153
|
318 |
string drive = ((MainForm)this).OpticalDriveToEject();
|
StephaneLenclud@153
|
319 |
if (drive.Length!=2)
|
StephaneLenclud@153
|
320 |
{
|
StephaneLenclud@153
|
321 |
//Not a proper drive spec.
|
StephaneLenclud@153
|
322 |
//Probably 'None' selected.
|
StephaneLenclud@153
|
323 |
return;
|
StephaneLenclud@153
|
324 |
}
|
StephaneLenclud@153
|
325 |
|
StephaneLenclud@153
|
326 |
SafeFileHandle handle = OpenVolume(drive);
|
StephaneLenclud@151
|
327 |
if (handle.IsInvalid)
|
StephaneLenclud@151
|
328 |
{
|
StephaneLenclud@155
|
329 |
CheckLastError("ERROR: Failed to open volume: ");
|
StephaneLenclud@151
|
330 |
return;
|
StephaneLenclud@151
|
331 |
}
|
StephaneLenclud@151
|
332 |
|
StephaneLenclud@151
|
333 |
if (LockVolume(handle) && DismountVolume(handle))
|
StephaneLenclud@151
|
334 |
{
|
StephaneLenclud@154
|
335 |
Debug.WriteLine("Volume was dismounted.");
|
StephaneLenclud@151
|
336 |
|
StephaneLenclud@152
|
337 |
if (PreventRemovalOfVolume(handle,false))
|
StephaneLenclud@151
|
338 |
{
|
StephaneLenclud@156
|
339 |
//StorageCheckVerify(handle);
|
StephaneLenclud@154
|
340 |
|
StephaneLenclud@156
|
341 |
DateTime before;
|
StephaneLenclud@156
|
342 |
before = DateTime.Now;
|
StephaneLenclud@156
|
343 |
bool ejectSuccess = MediaEject(handle);
|
StephaneLenclud@156
|
344 |
double ms = (DateTime.Now - before).TotalMilliseconds;
|
StephaneLenclud@156
|
345 |
|
StephaneLenclud@156
|
346 |
//We assume that if it take more than a certain time to for eject to execute it means we actually ejected.
|
StephaneLenclud@156
|
347 |
//If our eject completes too rapidly we assume the tray is already open and we will try to close it.
|
StephaneLenclud@156
|
348 |
if (ejectSuccess && ms > 100)
|
StephaneLenclud@152
|
349 |
{
|
StephaneLenclud@154
|
350 |
Debug.WriteLine("Media was ejected");
|
StephaneLenclud@152
|
351 |
}
|
StephaneLenclud@154
|
352 |
else if (MediaLoad(handle))
|
StephaneLenclud@154
|
353 |
{
|
StephaneLenclud@154
|
354 |
Debug.WriteLine("Media was loaded");
|
StephaneLenclud@154
|
355 |
}
|
StephaneLenclud@151
|
356 |
}
|
StephaneLenclud@151
|
357 |
}
|
StephaneLenclud@154
|
358 |
else
|
StephaneLenclud@154
|
359 |
{
|
StephaneLenclud@154
|
360 |
Debug.WriteLine("Volume lock or dismount failed.");
|
StephaneLenclud@154
|
361 |
}
|
StephaneLenclud@154
|
362 |
|
StephaneLenclud@154
|
363 |
//This is needed to make sure we can open the volume next time around
|
StephaneLenclud@154
|
364 |
handle.Dispose();
|
StephaneLenclud@150
|
365 |
}
|
StephaneLenclud@150
|
366 |
|
StephaneLenclud@150
|
367 |
/// <summary>
|
StephaneLenclud@150
|
368 |
///
|
StephaneLenclud@150
|
369 |
/// </summary>
|
StephaneLenclud@150
|
370 |
private void HandleGreenStart()
|
StephaneLenclud@150
|
371 |
{
|
StephaneLenclud@150
|
372 |
//First check if the process we want to launch already exists
|
StephaneLenclud@150
|
373 |
string procName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.StartFileName);
|
StephaneLenclud@150
|
374 |
Process[] existingProcesses = Process.GetProcessesByName(procName);
|
StephaneLenclud@150
|
375 |
if (existingProcesses == null || existingProcesses.Length == 0)
|
StephaneLenclud@150
|
376 |
{
|
StephaneLenclud@150
|
377 |
// Process do not exists just try to launch it
|
StephaneLenclud@150
|
378 |
ProcessStartInfo start = new ProcessStartInfo();
|
StephaneLenclud@150
|
379 |
// Enter in the command line arguments, everything you would enter after the executable name itself
|
StephaneLenclud@150
|
380 |
//start.Arguments = arguments;
|
StephaneLenclud@150
|
381 |
// Enter the executable to run, including the complete path
|
StephaneLenclud@150
|
382 |
start.FileName = Properties.Settings.Default.StartFileName;
|
StephaneLenclud@150
|
383 |
start.WindowStyle = ProcessWindowStyle.Normal;
|
StephaneLenclud@150
|
384 |
start.CreateNoWindow = true;
|
StephaneLenclud@150
|
385 |
start.UseShellExecute = true;
|
StephaneLenclud@150
|
386 |
// Run the external process & wait for it to finish
|
StephaneLenclud@150
|
387 |
Process proc = Process.Start(start);
|
StephaneLenclud@150
|
388 |
|
StephaneLenclud@150
|
389 |
//SL: We could have used that too
|
StephaneLenclud@150
|
390 |
//Shell32.Shell shell = new Shell32.Shell();
|
StephaneLenclud@150
|
391 |
//shell.ShellExecute(Properties.Settings.Default.StartFileName);
|
StephaneLenclud@150
|
392 |
}
|
StephaneLenclud@150
|
393 |
else
|
StephaneLenclud@150
|
394 |
{
|
StephaneLenclud@150
|
395 |
//This won't work properly until we have a manifest that enables uiAccess.
|
StephaneLenclud@150
|
396 |
//However uiAccess just won't work with ClickOnce so we will have to use a different deployment system.
|
StephaneLenclud@150
|
397 |
SwitchToThisWindow(existingProcesses[0].MainWindowHandle, true);
|
StephaneLenclud@150
|
398 |
}
|
StephaneLenclud@150
|
399 |
}
|
StephaneLenclud@131
|
400 |
/// <summary>
|
StephaneLenclud@131
|
401 |
/// We need to handle WM_INPUT.
|
StephaneLenclud@131
|
402 |
/// </summary>
|
StephaneLenclud@131
|
403 |
/// <param name="message"></param>
|
StephaneLenclud@131
|
404 |
protected override void WndProc(ref Message message)
|
StephaneLenclud@131
|
405 |
{
|
StephaneLenclud@131
|
406 |
switch (message.Msg)
|
StephaneLenclud@131
|
407 |
{
|
StephaneLenclud@131
|
408 |
case Const.WM_INPUT:
|
StephaneLenclud@131
|
409 |
//Returning zero means we processed that message.
|
StephaneLenclud@131
|
410 |
message.Result = new IntPtr(0);
|
StephaneLenclud@131
|
411 |
iHidHandler.ProcessInput(ref message);
|
StephaneLenclud@131
|
412 |
break;
|
StephaneLenclud@131
|
413 |
}
|
StephaneLenclud@131
|
414 |
//Is that needed? Check the docs.
|
StephaneLenclud@131
|
415 |
base.WndProc(ref message);
|
StephaneLenclud@131
|
416 |
}
|
StephaneLenclud@131
|
417 |
}
|
StephaneLenclud@125
|
418 |
}
|