Skip to content

Commit e2bef82

Browse files
committed
ProcessKiller: split source to multiple files
1 parent 1228114 commit e2bef82

File tree

3 files changed

+150
-123
lines changed

3 files changed

+150
-123
lines changed

Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs

Lines changed: 8 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller
1212
{
1313
public class Main : IPlugin, IPluginI18n, IContextMenu
1414
{
15-
private readonly HashSet<string> _systemProcessList = new HashSet<string>(){
16-
"conhost",
17-
"svchost",
18-
"idle",
19-
"system",
20-
"rundll32",
21-
"csrss",
22-
"lsass",
23-
"lsm",
24-
"smss",
25-
"wininit",
26-
"winlogon",
27-
"services",
28-
"spoolsv",
29-
"explorer"};
15+
private ProcessHelper processHelper = new ProcessHelper();
3016

3117
private static PluginInitContext _context;
3218

@@ -41,7 +27,7 @@ public List<Result> Query(Query query)
4127
? null
4228
: string.Join(Plugin.Query.TermSeperater, query.Terms.Skip(1)).ToLower();
4329

44-
var processlist = GetProcesslist(termToSearch);
30+
var processlist = processHelper.GetMatchingProcesses(termToSearch);
4531

4632
return !processlist.Any()
4733
? null
@@ -64,8 +50,7 @@ public List<Result> LoadContextMenus(Result result)
6450
var processPath = result.SubTitle;
6551

6652
// get all non-system processes whose file path matches that of the given result (processPath)
67-
var similarProcesses = Process.GetProcesses()
68-
.Where(p => !IsSystemProcess(p) && GetProcessFilename(p) == processPath);
53+
var similarProcesses = processHelper.GetSimilarProcesses(processPath);
6954

7055
menuOptions.Add(new Result
7156
{
@@ -75,7 +60,7 @@ public List<Result> LoadContextMenus(Result result)
7560
{
7661
foreach (var p in similarProcesses)
7762
{
78-
KillProcess(p);
63+
processHelper.TryKill(p);
7964
}
8065

8166
return true;
@@ -93,7 +78,7 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
9378
foreach (var pr in processlist)
9479
{
9580
var p = pr.Process;
96-
var path = GetPath(p);
81+
var path = processHelper.TryGetProcessFilename(p);
9782
results.Add(new Result()
9883
{
9984
IcoPath = path,
@@ -103,7 +88,7 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
10388
Score = pr.Score,
10489
Action = (c) =>
10590
{
106-
KillProcess(p);
91+
processHelper.TryKill(p);
10792
return true;
10893
}
10994
});
@@ -116,15 +101,15 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
116101
{
117102
results.Insert(0, new Result()
118103
{
119-
IcoPath = "Images\\app.png",
104+
IcoPath = "Images/app.png",
120105
Title = string.Format(_context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_all"), termToSearch),
121106
SubTitle = "",
122107
Score = 200,
123108
Action = (c) =>
124109
{
125110
foreach (var p in processlist)
126111
{
127-
KillProcess(p.Process);
112+
processHelper.TryKill(p.Process);
128113
}
129114

130115
return true;
@@ -134,105 +119,5 @@ private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist,
134119

135120
return results;
136121
}
137-
138-
private void KillProcess(Process p)
139-
{
140-
try
141-
{
142-
if (!p.HasExited)
143-
{
144-
p.Kill();
145-
}
146-
}
147-
catch (Exception e)
148-
{
149-
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
150-
}
151-
}
152-
153-
private List<ProcessResult> GetProcesslist(string termToSearch)
154-
{
155-
var processlist = new List<ProcessResult>();
156-
157-
foreach (var p in Process.GetProcesses())
158-
{
159-
if (IsSystemProcess(p)) continue;
160-
161-
if (string.IsNullOrWhiteSpace(termToSearch))
162-
{
163-
// show all non-system processes
164-
processlist.Add(new ProcessResult(p,0));
165-
}
166-
else
167-
{
168-
var score = StringMatcher.FuzzySearch(termToSearch, p.ProcessName + p.Id).Score;
169-
if (score > 0)
170-
{
171-
processlist.Add(new ProcessResult(p, score));
172-
}
173-
}
174-
}
175-
176-
return processlist;
177-
}
178-
179-
private bool IsSystemProcess(Process p) => _systemProcessList.Contains(p.ProcessName.ToLower());
180-
181-
internal class ProcessResult
182-
{
183-
public ProcessResult(Process process, int score)
184-
{
185-
Process = process;
186-
Score = score;
187-
}
188-
189-
public Process Process { get; }
190-
191-
public int Score { get; }
192-
}
193-
194-
private string GetPath(Process p)
195-
{
196-
try
197-
{
198-
return GetProcessFilename(p);
199-
}
200-
catch
201-
{
202-
return "";
203-
}
204-
}
205-
206-
[Flags]
207-
private enum ProcessAccessFlags : uint
208-
{
209-
QueryLimitedInformation = 0x00001000
210-
}
211-
212-
[DllImport("kernel32.dll", SetLastError = true)]
213-
private static extern bool QueryFullProcessImageName(
214-
[In] IntPtr hProcess,
215-
[In] int dwFlags,
216-
[Out] StringBuilder lpExeName,
217-
ref int lpdwSize);
218-
219-
[DllImport("kernel32.dll", SetLastError = true)]
220-
private static extern IntPtr OpenProcess(
221-
ProcessAccessFlags processAccess,
222-
bool bInheritHandle,
223-
int processId);
224-
225-
private string GetProcessFilename(Process p)
226-
{
227-
int capacity = 2000;
228-
StringBuilder builder = new StringBuilder(capacity);
229-
IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
230-
if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
231-
{
232-
return String.Empty;
233-
}
234-
235-
return builder.ToString();
236-
}
237122
}
238123
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Flow.Launcher.Infrastructure;
2+
using Flow.Launcher.Infrastructure.Logger;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.Runtime.InteropServices;
8+
using System.Text;
9+
10+
namespace Flow.Launcher.Plugin.ProcessKiller
11+
{
12+
internal class ProcessHelper
13+
{
14+
private readonly HashSet<string> _systemProcessList = new HashSet<string>()
15+
{
16+
"conhost",
17+
"svchost",
18+
"idle",
19+
"system",
20+
"rundll32",
21+
"csrss",
22+
"lsass",
23+
"lsm",
24+
"smss",
25+
"wininit",
26+
"winlogon",
27+
"services",
28+
"spoolsv",
29+
"explorer"
30+
};
31+
32+
private bool IsSystemProcess(Process p) => _systemProcessList.Contains(p.ProcessName.ToLower());
33+
34+
/// <summary>
35+
/// Returns a ProcessResult for evey running non-system process whose name matches the given searchTerm
36+
/// </summary>
37+
public List<ProcessResult> GetMatchingProcesses(string searchTerm)
38+
{
39+
var processlist = new List<ProcessResult>();
40+
41+
foreach (var p in Process.GetProcesses())
42+
{
43+
if (IsSystemProcess(p)) continue;
44+
45+
if (string.IsNullOrWhiteSpace(searchTerm))
46+
{
47+
// show all non-system processes
48+
processlist.Add(new ProcessResult(p, 0));
49+
}
50+
else
51+
{
52+
var score = StringMatcher.FuzzySearch(searchTerm, p.ProcessName + p.Id).Score;
53+
if (score > 0)
54+
{
55+
processlist.Add(new ProcessResult(p, score));
56+
}
57+
}
58+
}
59+
60+
return processlist;
61+
}
62+
63+
/// <summary>
64+
/// Returns all non-system processes whose file path matches the given processPath
65+
/// </summary>
66+
public IEnumerable<Process> GetSimilarProcesses(string processPath)
67+
{
68+
return Process.GetProcesses().Where(p => !IsSystemProcess(p) && TryGetProcessFilename(p) == processPath);
69+
}
70+
71+
public void TryKill(Process p)
72+
{
73+
try
74+
{
75+
if (!p.HasExited)
76+
{
77+
p.Kill();
78+
}
79+
}
80+
catch (Exception e)
81+
{
82+
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e);
83+
}
84+
}
85+
86+
public string TryGetProcessFilename(Process p)
87+
{
88+
try
89+
{
90+
int capacity = 2000;
91+
StringBuilder builder = new StringBuilder(capacity);
92+
IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
93+
if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
94+
{
95+
return String.Empty;
96+
}
97+
98+
return builder.ToString();
99+
}
100+
catch
101+
{
102+
return "";
103+
}
104+
}
105+
106+
[Flags]
107+
private enum ProcessAccessFlags : uint
108+
{
109+
QueryLimitedInformation = 0x00001000
110+
}
111+
112+
[DllImport("kernel32.dll", SetLastError = true)]
113+
private static extern bool QueryFullProcessImageName(
114+
[In] IntPtr hProcess,
115+
[In] int dwFlags,
116+
[Out] StringBuilder lpExeName,
117+
ref int lpdwSize);
118+
119+
[DllImport("kernel32.dll", SetLastError = true)]
120+
private static extern IntPtr OpenProcess(
121+
ProcessAccessFlags processAccess,
122+
bool bInheritHandle,
123+
int processId);
124+
}
125+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Diagnostics;
2+
3+
namespace Flow.Launcher.Plugin.ProcessKiller
4+
{
5+
internal class ProcessResult
6+
{
7+
public ProcessResult(Process process, int score)
8+
{
9+
Process = process;
10+
Score = score;
11+
}
12+
13+
public Process Process { get; }
14+
15+
public int Score { get; }
16+
}
17+
}

0 commit comments

Comments
 (0)