-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathprocessmonitor.cpp
More file actions
536 lines (492 loc) · 15.1 KB
/
processmonitor.cpp
File metadata and controls
536 lines (492 loc) · 15.1 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "config.h"
#include "processmonitor.h"
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QFileIconProvider>
#include <QHeaderView>
#include <QList>
#include <QStyle>
#include <QtConcurrent/QtConcurrent>
#include <QtWinExtras/QtWin>
#include <psapi.h>
ProcessMonitor::ProcessMonitor(
QSettings* settings,
QTreeWidget* treeWidget,
QLabel* treeStatusLabel,
QLabel* injector32StatusLabel,
QLabel* injector64StatusLabel,
QObject* parent)
: m_treeWidget(treeWidget)
, m_treeStatusLabel(treeStatusLabel)
, m_injector32StatusLabel(injector32StatusLabel)
, m_injector64StatusLabel(injector64StatusLabel)
, m_settings(settings)
{
winutils::enableAllPrivilege();
m_treeWidget->header()->setMinimumHeight(40);
m_treeWidget->setColumnWidth(0, 300);
m_treeWidget->setColumnWidth(5, 50);
m_treeWidget->setUniformRowHeights(true);
m_treeWidget->sortByColumn(0, Qt::AscendingOrder);
connect(m_treeWidget, &QTreeWidget::itemChanged, this,
&ProcessMonitor::onItemChanged);
this->startBridge32();
this->startBridge64();
init();
refresh();
}
ProcessMonitor::~ProcessMonitor()
{
this->terminalBridge();
delete m_bridge32;
delete m_bridge64;
// 注释: 程序退出时, unhook已经注入的进程
/*
QList<ProcessInfo> processList = winutils::getProcessList();
for (const auto& info: processList)
{
if (m_speedupItems.contains(info.name))
{
std::wstring dllPath =
QDir::toNativeSeparators(m_dllPath).toStdWString();
winutils::unhookDll(info.pid, dllPath);
}
}
*/
}
void ProcessMonitor::setFilter(QString processName)
{
m_filter = processName.toLower();
}
void ProcessMonitor::refresh()
{
healthcheckBridge();
QList<ProcessInfo> processList = winutils::getProcessList();
if (m_filter == "")
{
update(processList);
m_treeStatusLabel->setText(QString("搜索到%1个进程, 已过滤展示%2个")
.arg(processList.size())
.arg(processList.size()));
}
else
{
QList<ProcessInfo> filtered;
for (ProcessInfo info : processList)
{
if (info.name.toLower().contains(m_filter))
filtered.append(info);
}
update(filtered);
m_treeStatusLabel->setText(QString("搜索到%1个进程, 已过滤展示%2个")
.arg(processList.size())
.arg(filtered.size()));
}
}
void ProcessMonitor::start()
{
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &ProcessMonitor::refresh);
m_timer->start(1000);
}
void ProcessMonitor::onItemChanged(QTreeWidgetItem* item, int column)
{
if (column == 5 && (item->flags() & Qt::ItemIsUserCheckable))
{
bool state = m_treeWidget->blockSignals(true);
Qt::CheckState checkState = item->checkState(column);
QString processName = item->text(0);
DWORD pid = item->text(1).toLong();
bool is64Bit = item->text(3) == "x64" ? true : false;
if (checkState == Qt::Checked)
{
m_targetNames.insert(processName);
this->injectDll(pid, is64Bit);
item->setText(5, "加速中");
for (int col = 0; col < item->columnCount(); ++col)
{
item->setBackground(col, QBrush(QColor("#f3e5f5")));
item->setForeground(col, QBrush(QColor("#7b1fa2")));
}
qDebug() << processName << "勾选";
dump();
}
else
{
m_targetNames.remove(processName);
this->unhookDll(pid, is64Bit);
item->setText(5, "");
for (int col = 0; col < item->columnCount(); ++col)
{
item->setBackground(col, QBrush());
item->setForeground(col, QBrush());
}
qDebug() << processName << "取消勾选";
dump();
}
m_treeWidget->blockSignals(state);
}
}
void ProcessMonitor::init()
{
QStringList targetNames = m_settings->value(CONFIG_TARGETNAMES_KEY).toStringList();
m_targetNames = QSet<QString>(targetNames.begin(), targetNames.end());
}
void ProcessMonitor::dump()
{
m_settings->setValue(CONFIG_TARGETNAMES_KEY,
QStringList(m_targetNames.values()));
}
void ProcessMonitor::update(const QList<ProcessInfo>& processList)
{
// 保存当前展开状态
QMap<DWORD, bool> expandStates;
for (auto it = m_processItems.constBegin(); it != m_processItems.constEnd();
++it)
{
expandStates[it.key()] = it.value()->isExpanded();
}
// 跟踪现有进程,用于确定哪些已终止
QSet<DWORD> currentPids;
for (const ProcessInfo& info : processList)
{
currentPids.insert(info.pid);
}
// 移除已终止的进程
QMutableMapIterator<DWORD, QTreeWidgetItem*> i(m_processItems);
while (i.hasNext())
{
i.next();
if (!currentPids.contains(i.key()))
{
QTreeWidgetItem* item = i.value();
QTreeWidgetItem* parent = item->parent();
if (parent)
{
parent->removeChild(item);
}
else
{
auto index = m_treeWidget->indexOfTopLevelItem(item);
m_treeWidget->takeTopLevelItem(index);
}
while (item->childCount() > 0)
{
QTreeWidgetItem* child = item->takeChild(0);
if (parent)
{
parent->addChild(child);
}
else
{
m_treeWidget->addTopLevelItem(child);
}
}
// 删除节点
delete item;
// 从映射中移除
i.remove();
}
}
// 添加或更新进程信息
for (const ProcessInfo& info : processList)
{
if (m_processItems.contains(info.pid))
{
// 更新已存在的进程信息
QTreeWidgetItem* item = m_processItems[info.pid];
item->setText(1, QString::number(info.pid));
item->setText(2,
QString("%1 MB").arg(info.memoryUsage / 1024 / 1024));
QString arch = info.is64Bit ? "x64" : "x86";
item->setText(3, arch);
QString priority;
switch (info.priorityClass)
{
case HIGH_PRIORITY_CLASS:
priority = "高";
break;
case NORMAL_PRIORITY_CLASS:
priority = "中";
break;
case IDLE_PRIORITY_CLASS:
priority = "低";
break;
case REALTIME_PRIORITY_CLASS:
priority = "实时";
break;
default:
priority = "未知";
break;
}
item->setText(4, priority);
if (m_targetNames.contains(info.name))
{
if (item->checkState(5) == Qt::Unchecked)
{
item->setCheckState(5, Qt::Checked);
}
}
else
{
if (item->checkState(5) == Qt::Checked)
{
item->setCheckState(5, Qt::Unchecked);
}
}
}
else
{
// 添加新进程
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, info.name);
item->setText(1, QString::number(info.pid));
item->setText(2,
QString("%1 MB").arg(info.memoryUsage / 1024 / 1024));
QString arch = info.is64Bit ? "x64" : "x86";
item->setText(3, arch);
// 加载进程图标
item->setIcon(0, getProcessIconCached(info.pid));
QString priority;
switch (info.priorityClass)
{
case HIGH_PRIORITY_CLASS:
priority = "高";
break;
case NORMAL_PRIORITY_CLASS:
priority = "中";
break;
case IDLE_PRIORITY_CLASS:
priority = "低";
break;
case REALTIME_PRIORITY_CLASS:
priority = "实时";
break;
default:
priority = "未知";
break;
}
item->setText(4, priority);
item->setCheckState(5, Qt::Unchecked);
m_treeWidget->addTopLevelItem(item);
m_processItems[info.pid] = item;
}
}
// 恢复展开状态
for (auto it = m_processItems.constBegin(); it != m_processItems.constEnd();
++it)
{
if (expandStates.contains(it.key()))
{
it.value()->setExpanded(expandStates[it.key()]);
}
}
}
void ProcessMonitor::injectDll(DWORD processId, bool is64Bit)
{
QString cmd = QString("inject %1\n").arg(processId);
if (!is64Bit)
{
m_bridge32->write(cmd.toUtf8(), cmd.size());
m_bridge32->waitForBytesWritten();
}
else
{
m_bridge64->write(cmd.toUtf8(), cmd.size());
m_bridge64->waitForBytesWritten();
}
}
void ProcessMonitor::unhookDll(DWORD processId, bool is64Bit)
{
QString cmd = QString("unhook %1\n").arg(processId);
if (!is64Bit)
{
m_bridge32->write(cmd.toUtf8(), cmd.size());
m_bridge32->waitForBytesWritten();
}
else
{
m_bridge64->write(cmd.toUtf8(), cmd.size());
m_bridge64->waitForBytesWritten();
}
}
void ProcessMonitor::startBridge32()
{
m_bridge32 = new QProcess();
QStringList params32;
m_bridge32->start(BRIDGE32_EXE, params32);
if (!m_bridge32->waitForStarted())
{
qDebug() << "32位桥接子进程启动失败";
}
else
{
qDebug() << "32位桥接子进程已启动";
}
m_bridge32->setProcessChannelMode(QProcess::MergedChannels);
connect(m_bridge32, &QProcess::readyReadStandardOutput,
[&]()
{
QByteArray data = m_bridge32->readAllStandardOutput();
qDebug() << "收到输出:" << QString(data).trimmed();
});
}
void ProcessMonitor::startBridge64()
{
m_bridge64 = new QProcess();
QStringList params64;
m_bridge64->start(BRIDGE64_EXE, params64);
if (!m_bridge64->waitForStarted())
{
qDebug() << "64位桥接子进程启动失败";
}
else
{
qDebug() << "64位桥接子进程已启动";
}
m_bridge64->setProcessChannelMode(QProcess::MergedChannels);
connect(m_bridge64, &QProcess::readyReadStandardOutput,
[&]()
{
QByteArray data = m_bridge64->readAllStandardOutput();
qDebug() << "收到输出:" << QString(data).trimmed();
});
}
void ProcessMonitor::healthcheckBridge()
{
if (this->m_bridge32->state() == QProcess::Running)
{
m_injector32StatusLabel->setStyleSheet("color: green");
m_injector32StatusLabel->setText("正常");
}
else
{
m_injector32StatusLabel->setStyleSheet("color: red");
m_injector32StatusLabel->setText("异常退出");
}
if (this->m_bridge64->state() == QProcess::Running)
{
m_injector64StatusLabel->setStyleSheet("color: green");
m_injector64StatusLabel->setText("正常");
}
else
{
m_injector64StatusLabel->setStyleSheet("color:red");
m_injector64StatusLabel->setText("异常退出");
}
}
void ProcessMonitor::terminalBridge()
{
QString cmd = QString("exit\n");
m_bridge32->write(cmd.toUtf8(), cmd.size());
m_bridge32->waitForBytesWritten();
m_bridge64->write(cmd.toUtf8(), cmd.size());
m_bridge64->waitForBytesWritten();
}
void ProcessMonitor::changeSpeed(double factor)
{
QString cmd = QString("change %1\n").arg(factor);
m_bridge32->write(cmd.toUtf8(), cmd.size());
m_bridge32->waitForBytesWritten();
m_bridge64->write(cmd.toUtf8(), cmd.size());
m_bridge64->waitForBytesWritten();
}
QIcon ProcessMonitor::getProcessIconCached(DWORD processId)
{
QString processPath = winutils::getProcessPath(processId);
if (m_iconCache.contains(processPath))
{
return m_iconCache[processPath];
}
QIcon icon = getProcessIcon(processPath);
m_iconCache.insert(processPath, icon);
return icon;
}
QIcon ProcessMonitor::getDefaultIcon(const QString& processName)
{
// 根据进程名称或类型提供更有针对性的默认图标
if (processName != "")
{
// 使用SHGetFileInfo获取.exe文件的图标
SHFILEINFO sfi = {};
QIcon icon;
if (SHGetFileInfo(
reinterpret_cast<LPCWSTR>(processName.utf16()),
FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON))
{
// 将HICON转换为QIcon
icon = QtWin::fromHICON(sfi.hIcon);
// 释放图标资源
DestroyIcon(sfi.hIcon);
}
return icon;
}
else
{
// 使用SHGetFileInfo获取.exe文件的图标
SHFILEINFO sfi = {};
QIcon icon;
if (SHGetFileInfo(
reinterpret_cast<LPCWSTR>(L".exe"), FILE_ATTRIBUTE_NORMAL, &sfi,
sizeof(SHFILEINFO),
SHGFI_USEFILEATTRIBUTES | SHGFI_ICON | SHGFI_SMALLICON))
{
// 将HICON转换为QIcon
icon = QtWin::fromHICON(sfi.hIcon);
// 释放图标资源
DestroyIcon(sfi.hIcon);
}
return icon;
}
}
QIcon ProcessMonitor::getProcessIcon(QString processPath)
{
int lastSlashPos = std::max(processPath.lastIndexOf('/'), processPath.lastIndexOf('\\'));
QString processName;
if (lastSlashPos == -1)
{
processName = processPath;
}
processName = processPath.mid(lastSlashPos + 1);
if (processPath.isEmpty())
{
qDebug() << processPath << "无法获取进程完整路径";
return getDefaultIcon(processName);
}
SHFILEINFO sfi = {};
if (SHGetFileInfo(reinterpret_cast<LPCWSTR>(processPath.utf16()),
FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES))
{
if (sfi.hIcon)
{
QIcon icon = QtWin::fromHICON(sfi.hIcon);
DestroyIcon(sfi.hIcon);
if (!icon.isNull())
{
qDebug() << processPath << "通过SHGetFileInfo获取图标成功";
return icon;
}
}
}
HICON hIcon = ExtractIconW(
nullptr, reinterpret_cast<LPCWSTR>(processPath.utf16()), 0);
if (hIcon)
{
QIcon icon = QtWin::fromHICON(hIcon);
DestroyIcon(hIcon);
if (!icon.isNull())
return icon;
}
QFileInfo fileInfo(processPath);
if (fileInfo.exists())
{
// 使用Qt的QFileIconProvider获取文件图标
QFileIconProvider iconProvider;
return iconProvider.icon(fileInfo);
}
qDebug() << processPath << "无法获取进程图标使用默认图标";
return getDefaultIcon(processName);
}