-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayprocs.cpp
More file actions
204 lines (159 loc) · 5.84 KB
/
Copy pathdisplayprocs.cpp
File metadata and controls
204 lines (159 loc) · 5.84 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
#include "../header/displayprocs.h"
#include "../header/getprocs.h"
#include "../header/sortprocs.h"
#include "../header/keyactions.h"
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <sstream>
#include <string>
#include <unordered_map>
#include <functional>
using namespace std;
void DisplayProcs(KeyList key, bool asc) {
HANDLE snap = GetProcessSnap();
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);
std::vector<ProcInfo> procs = GetProcVector(snap, pe32);
int len = procs.size();
SortProcs(procs, key, asc);
const wchar_t* baseHeaders[5] = {
L"PROCESS NAME",
L"ID",
L"THREADS",
L"PARENT ID",
L"PRIORITY"
};
const KeyList headerKeys[5] = {
NAME,
ID,
THREADS,
PARENTID,
PRIORITY
};
std::wstring headers[5];
for (int i = 0; i < 5; i++) {
headers[i] = baseHeaders[i];
if (headerKeys[i] == key) {
headers[i] = L"> " + headers[i] + (asc ? L" <↑" : L" <↓");
}
}
int namew = headers[0].size();
int idw = headers[1].size();
int threadsw = headers[2].size();
int parentw = headers[3].size();
int priow = headers[4].size();
for (int i = 0; i < len; i++) {
if (procs[i].szExeFile.size() > namew) namew = procs[i].szExeFile.size();
int cur = 0;
cur = std::to_wstring(procs[i].th32ProcessID).size();
if (cur > idw) idw = cur;
cur = std::to_wstring(procs[i].cntThreads).size();
if (cur > threadsw) threadsw = cur;
cur = std::to_wstring(procs[i].th32ParentProcessID).size();
if (cur > parentw) parentw = cur;
cur = std::to_wstring(procs[i].pcPriClassBase).size();
if (cur > priow) priow = cur;
}
const int pad = 2;
namew += pad; idw += pad; threadsw += pad; parentw += pad; priow += pad;
std::wostringstream out;
out << L"Total Process Count: " << len << L"\n";
out << std::left
<< std::setw(namew) << headers[0]
<< std::setw(idw) << headers[1]
<< std::setw(threadsw) << headers[2]
<< std::setw(parentw) << headers[3]
<< std::setw(priow) << headers[4]
<< L"\n";
for (int i = 0; i < len; i++) {
out << std::left
<< std::setw(namew) << procs[i].szExeFile
<< std::right
<< std::setw(idw) << procs[i].th32ProcessID
<< std::setw(threadsw) << procs[i].cntThreads
<< std::setw(parentw) << procs[i].th32ParentProcessID
<< std::setw(priow) << procs[i].pcPriClassBase
<< L"\n";
}
std::wcout << out.str();
CloseHandle(snap);
}
static void RefreshDisplay(KeyList key, bool asc) {
std::wostringstream capture;
std::wstreambuf* oldBuf = std::wcout.rdbuf(capture.rdbuf());
DisplayProcs(key, asc);
std::wcout.rdbuf(oldBuf);
std::wstring frame = capture.str();
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) return;
const COORD savedCursor = csbi.dwCursorPosition;
const SMALL_RECT savedWindow = csbi.srWindow;
DWORD cellCount = (DWORD)csbi.dwSize.X * (DWORD)csbi.dwSize.Y;
DWORD written = 0;
COORD origin = { 0, 0 };
FillConsoleOutputCharacterW(hOut, L' ', cellCount, origin, &written);
FillConsoleOutputAttribute(hOut, csbi.wAttributes, cellCount, origin, &written);
SHORT row = savedWindow.Top;
SHORT col = 0;
const SHORT bufW = csbi.dwSize.X;
const SHORT bufH = csbi.dwSize.Y;
size_t i = 0;
while (i < frame.size() && row < bufH) {
size_t lineEnd = frame.find(L'\n', i);
if (lineEnd == std::wstring::npos) lineEnd = frame.size();
std::wstring line = frame.substr(i, lineEnd - i);
if ((SHORT)line.size() > bufW) line.resize(bufW);
DWORD charsWritten = 0;
COORD pos = { col, row };
WriteConsoleOutputCharacterW(hOut, line.c_str(), (DWORD)line.size(), pos, &charsWritten);
if ((SHORT)line.size() < bufW) {
DWORD restWritten = 0;
COORD restPos = { (SHORT)line.size(), row };
FillConsoleOutputCharacterW(hOut, L' ', (DWORD)(bufW - (SHORT)line.size()), restPos, &restWritten);
FillConsoleOutputAttribute(hOut, csbi.wAttributes, (DWORD)(bufW - (SHORT)line.size()), restPos, &restWritten);
}
row++;
i = (lineEnd < frame.size()) ? (lineEnd + 1) : lineEnd;
}
SetConsoleWindowInfo(hOut, TRUE, &savedWindow);
SetConsoleCursorPosition(hOut, savedCursor);
}
void CycleDisplay() {
InitKeyActions();
UIState st;
static KeyList columns[] = {NAME, ID, THREADS, PARENTID, PRIORITY, DWSIZE};
const int colCount = (sizeof(columns) / sizeof(columns[0]));
RefreshDisplay(columns[st.selectedIndex], st.asc);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
if (hIn == INVALID_HANDLE_VALUE) {
GetLastError();
return;
}
while (st.running) {
DWORD numEvents = 0;
if (!GetNumberOfConsoleInputEvents(hIn, &numEvents)) {
GetLastError();
break;
}
while (numEvents > 0) {
INPUT_RECORD rec;
DWORD readCount = 0;
if (!ReadConsoleInputW(hIn, &rec, 1, &readCount)) {
st.running = false;
break;
}
if (readCount == 1 && rec.EventType == KEY_EVENT) {
const KEY_EVENT_RECORD& kev = rec.Event.KeyEvent;
HandleKeyEvent(kev, st, colCount);
RefreshDisplay(columns[st.selectedIndex], st.asc);
}
if (!GetNumberOfConsoleInputEvents(hIn, &numEvents)) {
break;
}
}
Sleep(1000);
}
}