-
Notifications
You must be signed in to change notification settings - Fork 19
/
CloseProcessInstruction.cs
104 lines (92 loc) · 3.39 KB
/
CloseProcessInstruction.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
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Win32;
namespace Ketarin
{
/// <summary>
/// Represents a setup instruction that closes a process by first trying
/// a soft exit and then a hard exit (kill) if necessary.
/// </summary>
[Serializable()]
public class CloseProcessInstruction : SetupInstruction
{
/// <summary>
/// Specifies the name of the process to close.
/// </summary>
public string ProcessName
{
get;
set;
}
public override string Name
{
get
{
return "Close process";
}
}
public override void Execute()
{
foreach (Process process in Process.GetProcessesByName(ProcessName))
{
if (process.CloseMainWindow())
{
// Wait 2 seconds until the process has closed
DateTime startWait = DateTime.Now;
while (DateTime.Now - startWait < TimeSpan.FromSeconds(2))
{
if (process.HasExited) break;
Thread.Sleep(100);
}
}
if (!process.HasExited)
{
// Safe termination
if (!SafeTerminateProcess(process.Handle, 0))
{
process.Kill();
}
}
}
}
/// <summary>
/// Safely terminate a process by creating a remote thread in the process that calls ExitProcess.
/// </summary>
/// <remarks>http://www.drdobbs.com/184416547;?pgno=3</remarks>
private bool SafeTerminateProcess(IntPtr hProcess, uint uExitCode)
{
uint dwCode;
IntPtr hProcessDup;
IntPtr hRT = IntPtr.Zero;
IntPtr hKernel = Kernel32.GetModuleHandle("Kernel32");
bool success = false;
bool bDup = Kernel32.DuplicateHandle(Kernel32.GetCurrentProcess(), hProcess, Kernel32.GetCurrentProcess(), out hProcessDup, Kernel32.PROCESS_ALL_ACCESS, false, 0);
// Detect the special case where the process is
// already dead...
if (Kernel32.GetExitCodeProcess(bDup ? hProcessDup : hProcess, out dwCode) && dwCode == Kernel32.STILL_ACTIVE)
{
IntPtr pfnExitProc = Kernel32.GetProcAddress(hKernel, "ExitProcess");
uint dwTID;
hRT = Kernel32.CreateRemoteThread(bDup ? hProcessDup : hProcess, IntPtr.Zero, 0, pfnExitProc, new IntPtr(uExitCode), 0, out dwTID);
}
if (hRT != IntPtr.Zero)
{
// Must wait process to terminate to
// guarantee that it has exited...
Kernel32.WaitForSingleObject(bDup ? hProcessDup : hProcess, Kernel32.INFINITE);
Kernel32.CloseHandle(hRT);
success = true;
}
if (bDup)
{
Kernel32.CloseHandle(hProcessDup);
}
return success;
}
public override string ToString()
{
return string.Format("Close {0}", this.ProcessName);
}
}
}