Skip to content

Commit 852750d

Browse files
committed
start off the ProcessKiller plugin
This plugin is a direct port of Wox.Plugin.ProcessKiller from https://github.com/theClueless/Wox.Plugins
1 parent 8f74316 commit 852750d

File tree

5 files changed

+280
-0
lines changed

5 files changed

+280
-0
lines changed

Flow.Launcher.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flow.Launcher.Plugin.Calcul
7070
EndProject
7171
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flow.Launcher.Plugin.Explorer", "Plugins\Flow.Launcher.Plugin.Explorer\Flow.Launcher.Plugin.Explorer.csproj", "{F9C4C081-4CC3-4146-95F1-E102B4E10A5F}"
7272
EndProject
73+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flow.Launcher.Plugin.ProcessKiller", "Plugins\Flow.Launcher.Plugin.ProcessKiller\Flow.Launcher.Plugin.ProcessKiller.csproj", "{588088F4-3262-4F9F-9663-A05DE12534C3}"
74+
EndProject
7375
Global
7476
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7577
Debug|Any CPU = Debug|Any CPU
@@ -297,6 +299,18 @@ Global
297299
{F9C4C081-4CC3-4146-95F1-E102B4E10A5F}.Release|x64.Build.0 = Release|Any CPU
298300
{F9C4C081-4CC3-4146-95F1-E102B4E10A5F}.Release|x86.ActiveCfg = Release|Any CPU
299301
{F9C4C081-4CC3-4146-95F1-E102B4E10A5F}.Release|x86.Build.0 = Release|Any CPU
302+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
303+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
304+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|x64.ActiveCfg = Debug|Any CPU
305+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|x64.Build.0 = Debug|Any CPU
306+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|x86.ActiveCfg = Debug|Any CPU
307+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Debug|x86.Build.0 = Debug|Any CPU
308+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
309+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|Any CPU.Build.0 = Release|Any CPU
310+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|x64.ActiveCfg = Release|Any CPU
311+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|x64.Build.0 = Release|Any CPU
312+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|x86.ActiveCfg = Release|Any CPU
313+
{588088F4-3262-4F9F-9663-A05DE12534C3}.Release|x86.Build.0 = Release|Any CPU
300314
EndGlobalSection
301315
GlobalSection(SolutionProperties) = preSolution
302316
HideSolutionNode = FALSE
@@ -315,6 +329,7 @@ Global
315329
{9B130CC5-14FB-41FF-B310-0A95B6894C37} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
316330
{59BD9891-3837-438A-958D-ADC7F91F6F7E} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
317331
{F9C4C081-4CC3-4146-95F1-E102B4E10A5F} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
332+
{588088F4-3262-4F9F-9663-A05DE12534C3} = {3A73F5A7-0335-40D8-BF7C-F20BE5D0BA87}
318333
EndGlobalSection
319334
GlobalSection(ExtensibilityGlobals) = postSolution
320335
SolutionGuid = {F26ACB50-3F6C-4907-B0C9-1ADACC1D0DED}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<AssemblyName>Flow.Launcher.Plugin.ProcessKiller</AssemblyName>
6+
<PackageId>Flow.Launcher.Plugin.ProcessKiller</PackageId>
7+
<Authors>Flow-Launcher</Authors>
8+
<PackageProjectUrl>https://github.com/Flow-Launcher/Flow.Launcher.Plugin.ProcessKiller</PackageProjectUrl>
9+
<RepositoryUrl>https://github.com/Flow-Launcher/Flow.Launcher.Plugin.ProcessKiller</RepositoryUrl>
10+
<PackageTags>flow-launcher flow-plugin</PackageTags>
11+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
12+
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>..\..\Output\Debug\Plugins\Flow.Launcher.Plugin.ProcessKiller\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
<Prefer32Bit>false</Prefer32Bit>
24+
</PropertyGroup>
25+
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>..\..\Output\Release\Plugins\Flow.Launcher.Plugin.ProcessKiller\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
<Prefer32Bit>false</Prefer32Bit>
34+
</PropertyGroup>
35+
36+
<ItemGroup>
37+
<Content Include="Images\app.png">
38+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
39+
</Content>
40+
<Content Include="plugin.json">
41+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
42+
</Content>
43+
</ItemGroup>
44+
45+
<ItemGroup>
46+
<ProjectReference Include="..\..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
47+
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
48+
</ItemGroup>
49+
50+
</Project>
1.31 KB
Loading
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Diagnostics;
6+
using System.Dynamic;
7+
using System.Runtime.InteropServices;
8+
using Flow.Launcher.Infrastructure;
9+
using Flow.Launcher.Infrastructure.Logger;
10+
11+
namespace Flow.Launcher.Plugin.ProcessKiller
12+
{
13+
public class Main : IPlugin
14+
{
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"};
30+
31+
public List<Result> Query(Query query)
32+
{
33+
var termToSearch = query.Terms.Length == 1
34+
? null
35+
: query.FirstSearch.ToLower();
36+
var processlist = GetProcesslist(termToSearch);
37+
38+
return
39+
!processlist.Any()
40+
? null
41+
: CreateResultsFromProcesses(processlist, termToSearch);
42+
}
43+
44+
private List<Result> CreateResultsFromProcesses(List<ProcessResult> processlist, string termToSearch)
45+
{
46+
var results = new List<Result>();
47+
48+
foreach (var pr in processlist)
49+
{
50+
var p = pr.Process;
51+
var path = GetPath(p);
52+
results.Add(new Result()
53+
{
54+
IcoPath = path,
55+
Title = p.ProcessName + " - " + p.Id,
56+
SubTitle = path,
57+
Score = pr.Score,
58+
Action = (c) =>
59+
{
60+
KillProcess(p);
61+
return true;
62+
}
63+
});
64+
}
65+
66+
if (processlist.Count > 1 && !string.IsNullOrEmpty(termToSearch))
67+
{
68+
results.Insert(0, new Result()
69+
{
70+
IcoPath = "Images\\app.png",
71+
Title = "kill all \"" + termToSearch + "\" process",
72+
SubTitle = "",
73+
Score = 200,
74+
Action = (c) =>
75+
{
76+
foreach (var p in processlist)
77+
{
78+
KillProcess(p.Process);
79+
}
80+
81+
return true;
82+
}
83+
});
84+
}
85+
86+
return results;
87+
88+
void KillProcess(Process p)
89+
{
90+
try
91+
{
92+
if (!p.HasExited)
93+
{
94+
p.Kill();
95+
}
96+
}
97+
catch (Exception e)
98+
{
99+
Log.Exception($"Fail to kill process {p.ProcessName}", e);
100+
}
101+
}
102+
}
103+
private List<ProcessResult> GetProcesslist(string termToSearch)
104+
{
105+
var processlist = new List<ProcessResult>();
106+
var processes = Process.GetProcesses();
107+
if (string.IsNullOrWhiteSpace(termToSearch))
108+
{
109+
// show all process
110+
foreach (var p in processes)
111+
{
112+
if (FilterSystemProcesses(p)) continue;
113+
114+
processlist.Add(new ProcessResult(p,0));
115+
}
116+
}
117+
else
118+
{
119+
foreach (var p in processes)
120+
{
121+
if (FilterSystemProcesses(p)) continue;
122+
var score = StringMatcher.FuzzySearch(termToSearch, p.ProcessName + p.Id).Score;
123+
if (score > 0)
124+
{
125+
processlist.Add(new ProcessResult(p, score));
126+
}
127+
}
128+
}
129+
130+
return processlist;
131+
132+
bool FilterSystemProcesses(Process p)
133+
{
134+
var name = p.ProcessName.ToLower();
135+
if (_systemProcessList.Contains(name))
136+
return true;
137+
return false;
138+
}
139+
}
140+
141+
internal class ProcessResult
142+
{
143+
public ProcessResult(Process process, int score)
144+
{
145+
Process = process;
146+
Score = score;
147+
}
148+
149+
public Process Process { get; }
150+
151+
public int Score { get; }
152+
}
153+
154+
private string GetPath(Process p)
155+
{
156+
try
157+
{
158+
var path = GetProcessFilename(p);
159+
return path.ToLower();
160+
}
161+
catch
162+
{
163+
return "";
164+
}
165+
}
166+
167+
public void Init(PluginInitContext context)
168+
{
169+
}
170+
171+
[Flags]
172+
private enum ProcessAccessFlags : uint
173+
{
174+
QueryLimitedInformation = 0x00001000
175+
}
176+
177+
[DllImport("kernel32.dll", SetLastError = true)]
178+
private static extern bool QueryFullProcessImageName(
179+
[In] IntPtr hProcess,
180+
[In] int dwFlags,
181+
[Out] StringBuilder lpExeName,
182+
ref int lpdwSize);
183+
184+
[DllImport("kernel32.dll", SetLastError = true)]
185+
private static extern IntPtr OpenProcess(
186+
ProcessAccessFlags processAccess,
187+
bool bInheritHandle,
188+
int processId);
189+
190+
private string GetProcessFilename(Process p)
191+
{
192+
int capacity = 2000;
193+
StringBuilder builder = new StringBuilder(capacity);
194+
IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id);
195+
if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity))
196+
{
197+
return String.Empty;
198+
}
199+
200+
return builder.ToString();
201+
}
202+
}
203+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ID":"b64d0a79-329a-48b0-b53f-d658318a1bf6",
3+
"ActionKeyword":"kill",
4+
"Name":"Process Killer",
5+
"Description":"kill running processes from Flow",
6+
"Author":"Flow-Launcher",
7+
"Version":"1.0.0",
8+
"Language":"csharp",
9+
"Website":"https://github.com/Flow-Launcher/Flow.Launcher.Plugin.ProcessKiller",
10+
"IcoPath":"Images\\app.png",
11+
"ExecuteFileName":"Flow.Launcher.Plugin.ProcessKiller.dll"
12+
}

0 commit comments

Comments
 (0)