forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessBrowserForm.cs
More file actions
188 lines (156 loc) · 4.69 KB
/
Copy pathProcessBrowserForm.cs
File metadata and controls
188 lines (156 loc) · 4.69 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ReClassNET.Forms;
using ReClassNET.Memory;
using ReClassNET.UI;
using ReClassNET.Util;
namespace ReClassNET
{
public partial class ProcessBrowserForm : IconForm
{
private const string NoPreviousProcess = "No previous process";
private static readonly string[] CommonProcesses = new string[]
{
"[system process]", "system", "svchost.exe", "services.exe", "wininit.exe",
"smss.exe", "csrss.exe", "lsass.exe", "winlogon.exe", "wininit.exe", "dwm.exe"
};
private readonly NativeHelper nativeHelper;
/// <summary>Gets the selected process.</summary>
public ProcessInfo SelectedProcess
{
get
{
var row = (processDataGridView.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault()?.DataBoundItem as DataRowView)?.Row;
if (row != null)
{
return new ProcessInfo(nativeHelper, row.Field<int>("id"), row.Field<string>("name"), row.Field<string>("path"));
}
return null;
}
}
/// <summary>Gets if symbols should get loaded.</summary>
public bool LoadSymbols => loadSymbolsCheckBox.Checked;
public ProcessBrowserForm(NativeHelper nativeHelper, string previousProcess)
{
Contract.Requires(nativeHelper != null);
this.nativeHelper = nativeHelper;
InitializeComponent();
processDataGridView.AutoGenerateColumns = false;
previousProcessLinkLabel.Text = string.IsNullOrEmpty(previousProcess) ? NoPreviousProcess : previousProcess;
RefreshProcessList();
foreach (var row in processDataGridView.Rows.Cast<DataGridViewRow>())
{
if ((row.Cells[1].Value as string) == previousProcess)
{
processDataGridView.CurrentCell = row.Cells[1];
break;
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GlobalWindowManager.AddWindow(this);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
base.OnFormClosed(e);
GlobalWindowManager.RemoveWindow(this);
}
#region Event Handler
private void filterCheckBox_CheckedChanged(object sender, EventArgs e)
{
RefreshProcessList();
}
private void filterTextBox_TextChanged(object sender, EventArgs e)
{
ApplyFilter();
}
private void refreshButton_Click(object sender, EventArgs e)
{
RefreshProcessList();
}
private void previousProcessLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
filterTextBox.Text = previousProcessLinkLabel.Text == NoPreviousProcess ? string.Empty : previousProcessLinkLabel.Text;
}
private void processDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
AcceptButton.PerformClick();
}
#endregion
/// <summary>Queries all processes and displays them.</summary>
private void RefreshProcessList()
{
var dt = new DataTable();
dt.Columns.Add("icon", typeof(Icon));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("path", typeof(string));
dt.Columns.Add("create", typeof(DateTime));
nativeHelper.EnumerateProcesses((pid, path) =>
{
var moduleName = Path.GetFileName(path);
if (!filterCheckBox.Checked || !CommonProcesses.Contains(moduleName.ToLower()))
{
var row = dt.NewRow();
row["icon"] = ShellIcon.GetSmallIcon(path);
row["name"] = moduleName;
row["id"] = pid;
row["path"] = path;
row["create"] = GetProcessCreateTime((int)pid);
dt.Rows.Add(row);
}
});
dt.DefaultView.Sort = "create DESC";
processDataGridView.DataSource = dt;
ApplyFilter();
}
/// <summary>Query the time the process was created.</summary>
/// <param name="pid">The process id.</param>
/// <returns>The time the process was created or <see cref="DateTime.MinValue"/> if an error occurs.</returns>
private DateTime GetProcessCreateTime(int pid)
{
IntPtr handle = IntPtr.Zero;
try
{
handle = nativeHelper.OpenRemoteProcess((int)pid, NativeMethods.PROCESS_QUERY_LIMITED_INFORMATION);
if (!handle.IsNull())
{
long dummy;
long create;
if (NativeMethods.GetProcessTimes(handle, out create, out dummy, out dummy, out dummy))
{
return DateTime.FromFileTime(create);
}
}
}
catch
{
}
finally
{
if (!handle.IsNull())
{
nativeHelper.CloseRemoteProcess(handle);
}
}
return DateTime.MinValue;
}
private void ApplyFilter()
{
var filter = filterTextBox.Text;
if (!string.IsNullOrEmpty(filter))
{
filter = $"name like '%{filter}%' or path like '%{filter}%'";
}
(processDataGridView.DataSource as DataTable).DefaultView.RowFilter = filter;
}
}
}