forked from wyday/wyupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmFilesInUse.cs
380 lines (306 loc) · 14.1 KB
/
frmFilesInUse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace wyUpdate
{
public partial class frmFilesInUse : Form
{
[StructLayout(LayoutKind.Sequential)]
struct RM_UNIQUE_PROCESS
{
public int dwProcessId;
public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
}
const int RmRebootReasonNone = 0;
const int CCH_RM_MAX_APP_NAME = 255;
const int CCH_RM_MAX_SVC_NAME = 63;
enum RM_APP_TYPE
{
RmUnknownApp = 0,
RmMainWindow = 1,
RmOtherWindow = 2,
RmService = 3,
RmExplorer = 4,
RmConsole = 5,
RmCritical = 1000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct RM_PROCESS_INFO
{
public RM_UNIQUE_PROCESS Process;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)]
public string strAppName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
public string strServiceShortName;
public RM_APP_TYPE ApplicationType;
public uint AppStatus;
public uint TSSessionId;
[MarshalAs(UnmanagedType.Bool)]
public bool bRestartable;
}
[DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
[DllImport("rstrtmgr.dll")]
static extern int RmEndSession(uint pSessionHandle);
[DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
static extern int RmRegisterResources(uint pSessionHandle,
UInt32 nFiles,
string[] rgsFilenames,
UInt32 nApplications,
[In] RM_UNIQUE_PROCESS[] rgApplications,
UInt32 nServices,
string[] rgsServiceNames);
[DllImport("rstrtmgr.dll")]
static extern int RmGetList(uint dwSessionHandle,
out uint pnProcInfoNeeded,
ref uint pnProcInfo,
[In, Out] RM_PROCESS_INFO[] rgAffectedApps,
ref uint lpdwRebootReasons);
const int SidePadding = 12;
readonly ClientLanguage clientLang;
public bool CancelUpdate;
readonly bool showingProcesses;
readonly BackgroundWorker bw;
Timer chkProc;
List<Process> runningProcesses;
public string FilenameInUse;
public frmFilesInUse(ClientLanguage cLang, string filename)
{
InitializeComponent();
clientLang = cLang;
// translate the buttons & text
Text = clientLang.FilesInUseDialog.Title;
btnCancel.Text = clientLang.CancelUpdate;
FilenameInUse = filename;
txtFile.Text = filename;
if (VistaTools.AtLeastVista())
{
// get the list of processes using the file
try
{
runningProcesses = GetProcessesUsingFiles(new[] {filename});
}
catch { }
if (runningProcesses != null && runningProcesses.Count > 0)
{
UpdateList();
// translate the items
lblProc.Text = clientLang.FilesInUseDialog.SubTitle;
btnCloseProc.Text = clientLang.ClosePrc;
btnCloseAll.Text = clientLang.CloseAllPrc;
// show the list box of the running processes
lblProc.Visible = true;
listProc.Visible = true;
btnCloseAll.Visible = true;
btnCloseProc.Visible = true;
showingProcesses = true;
chkProc = new Timer {Enabled = true, Interval = 2000};
chkProc.Tick += chkProc_Tick;
bw = new BackgroundWorker {WorkerSupportsCancellation = true};
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
//begin checking the for the filenames
bw.RunWorkerAsync();
}
}
SetStyle(ControlStyles.AllPaintingInWmPaint
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.UserPaint, true);
// position all the components
UpdateSizes();
}
// http://blogs.msdn.com/b/oldnewthing/archive/2012/02/17/10268840.aspx (How do I find out which process has a file open?)
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373682(v=vs.85).aspx (Using Restart Manager with a Secondary Installer )
// http://msdn.microsoft.com/en-us/magazine/cc163450.aspx ( Restart Manager -- C# example)
public static List<Process> GetProcessesUsingFiles(IList<string> filePaths)
{
uint sessionHandle;
List<Process> processes = null;
// Create a restart manager session
int rv = RmStartSession(out sessionHandle, 0, Guid.NewGuid().ToString("N"));
if (rv != 0)
throw new Win32Exception();
try
{
// Let the restart manager know what files we’re interested in
string[] pathStrings = new string[filePaths.Count];
filePaths.CopyTo(pathStrings, 0);
rv = RmRegisterResources(sessionHandle, (uint)pathStrings.Length, pathStrings, 0, null, 0, null);
if (rv != 0)
throw new Win32Exception();
// Ask the restart manager what other applications
// are using those files
const int ERROR_MORE_DATA = 234;
uint pnProcInfoNeeded = 0,
pnProcInfo = 0,
lpdwRebootReasons = RmRebootReasonNone;
//Note: there's a race condition here -- the first call to RmGetList() returns
// the total number of process. However, when we call RmGetList() again to get
// the actual processes this number may have increased.
rv = RmGetList(sessionHandle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
if (rv == ERROR_MORE_DATA)
{
// Create an array to store the process results
RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
pnProcInfo = pnProcInfoNeeded;
// Get the list
rv = RmGetList(sessionHandle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
if (rv == 0)
{
processes = new List<Process>((int)pnProcInfo);
// Enumerate all of the results and add them to the
// list to be returned
for (int i = 0; i < pnProcInfo; i++)
{
try
{
processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
}
// catch the error -- in case the process is no longer running
catch (ArgumentException) { }
}
}
else
throw new Win32Exception();
}
else if (rv != 0)
throw new Win32Exception();
}
finally
{
// Close the resource manager
RmEndSession(sessionHandle);
}
return processes;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
List<Process> rProcs = GetProcessesUsingFiles(new[] { FilenameInUse });
e.Result = rProcs != null && rProcs.Count > 0 ? rProcs : null;
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Result == null)
return;
List<Process> rProcs = (List<Process>)e.Result;
//check if list of process is sames as the semi-global one (runningProcesses)
//if diff, update listbox.
if (!frmProcesses.SameProcs(rProcs, runningProcesses))
{
//update the running processes array
runningProcesses = rProcs;
// the worker might be completed as a result of the form window being closed.
// so, check if the form is disposed before updating the list.
if (!IsDisposed)
UpdateList();
}
}
void btnCancel_Click(object sender, EventArgs e)
{
chkProc.Enabled = false;
CancelUpdate = DialogResult.Yes ==
MessageBox.Show(clientLang.CancelDialog.Content, clientLang.CancelDialog.Title,
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2);
// prevent closing this window if the user isn't canceling
if (!CancelUpdate)
{
DialogResult = DialogResult.None;
chkProc.Enabled = true;
}
}
Rectangle m_DescripRect;
void UpdateSizes()
{
if (txtFile == null || clientLang == null)
return;
m_DescripRect = new Rectangle(new Point(SidePadding, SidePadding),
TextRenderer.MeasureText(clientLang.FilesInUseDialog.Content,
Font,
new Size(ClientRectangle.Width - SidePadding * 2, 1),
TextFormatFlags.WordBreak | TextFormatFlags.NoPrefix | TextFormatFlags.NoPadding));
txtFile.Location = new Point(SidePadding, m_DescripRect.Bottom + 5);
txtFile.Width = ClientRectangle.Width - SidePadding * 2;
int txtHeight = ClientRectangle.Height - txtFile.Top - (ClientRectangle.Height - btnCancel.Top) - 5;
if (showingProcesses)
{
txtFile.Height = (txtHeight / 2) - 5 - lblProc.Height;
lblProc.Location = new Point(SidePadding, txtFile.Bottom + 12);
listProc.Height = txtFile.Height;
listProc.Location = new Point(SidePadding, lblProc.Bottom + 5);
listProc.Width = ClientRectangle.Width - SidePadding * 2;
}
else
txtFile.Height = txtHeight;
}
protected override void OnLayout(LayoutEventArgs levent)
{
base.OnLayout(levent);
UpdateSizes();
}
protected override void OnPaint(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, clientLang.FilesInUseDialog.Content, Font, m_DescripRect, ForeColor,
TextFormatFlags.WordBreak | TextFormatFlags.NoPrefix | TextFormatFlags.NoPadding);
base.OnPaint(e);
}
void btnCloseProc_Click(object sender, EventArgs e)
{
try
{
// if there's no window handle, just kill the process
if (runningProcesses[listProc.SelectedIndex].MainWindowHandle == IntPtr.Zero)
runningProcesses[listProc.SelectedIndex].Kill();
else
runningProcesses[listProc.SelectedIndex].CloseMainWindow();
string procDets = (string)listProc.Items[listProc.SelectedIndex];
if (!procDets.StartsWith("[closing]"))
procDets = "[closing] " + procDets;
listProc.Items[listProc.SelectedIndex] = procDets;
}
catch { }
if (!bw.IsBusy)
bw.RunWorkerAsync();
}
void btnCloseAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < runningProcesses.Count; i++)
{
try
{
// if there's no window handle, just kill the process
if (runningProcesses[i].MainWindowHandle == IntPtr.Zero)
runningProcesses[i].Kill();
else
runningProcesses[i].CloseMainWindow();
string procDets = (string)listProc.Items[i];
if (!procDets.StartsWith("[closing]"))
procDets = "[closing] " + procDets;
listProc.Items[i] = procDets;
}
catch { }
}
if (!bw.IsBusy)
bw.RunWorkerAsync();
}
void chkProc_Tick(object sender, EventArgs e)
{
if (!bw.IsBusy)
bw.RunWorkerAsync();
}
void UpdateList()
{
listProc.Items.Clear();
foreach (Process proc in runningProcesses)
{
listProc.Items.Add(proc.MainWindowTitle + " (" + proc.ProcessName + ".exe)");
}
listProc.SelectedIndex = 0;
}
}
}