forked from winsiderss/systeminformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemlists.c
More file actions
322 lines (292 loc) · 14.2 KB
/
Copy pathmemlists.c
File metadata and controls
322 lines (292 loc) · 14.2 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
/*
* Process Hacker -
* memory list information
*
* Copyright (C) 2010-2011 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <phapp.h>
#include <emenu.h>
#include <settings.h>
#include <actions.h>
#include <phsvccl.h>
#include <procprv.h>
#include <windowsx.h>
#define MSG_UPDATE (WM_APP + 1)
INT_PTR CALLBACK PhpMemoryListsDlgProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
HWND PhMemoryListsWindowHandle = NULL;
static VOID (NTAPI *UnregisterDialogFunction)(HWND);
static PH_CALLBACK_REGISTRATION ProcessesUpdatedRegistration;
VOID PhShowMemoryListsDialog(
_In_ HWND ParentWindowHandle,
_In_opt_ VOID (NTAPI *RegisterDialog)(HWND),
_In_opt_ VOID (NTAPI *UnregisterDialog)(HWND)
)
{
if (!PhMemoryListsWindowHandle)
{
PhMemoryListsWindowHandle = CreateDialog(
PhInstanceHandle,
MAKEINTRESOURCE(IDD_MEMLISTS),
ParentWindowHandle,
PhpMemoryListsDlgProc
);
RegisterDialog(PhMemoryListsWindowHandle);
UnregisterDialogFunction = UnregisterDialog;
}
if (!IsWindowVisible(PhMemoryListsWindowHandle))
ShowWindow(PhMemoryListsWindowHandle, SW_SHOW);
else if (IsIconic(PhMemoryListsWindowHandle))
ShowWindow(PhMemoryListsWindowHandle, SW_RESTORE);
else
SetForegroundWindow(PhMemoryListsWindowHandle);
}
static VOID NTAPI ProcessesUpdatedCallback(
_In_opt_ PVOID Parameter,
_In_opt_ PVOID Context
)
{
PostMessage(PhMemoryListsWindowHandle, MSG_UPDATE, 0, 0);
}
static VOID PhpUpdateMemoryListInfo(
_In_ HWND hwndDlg
)
{
SYSTEM_MEMORY_LIST_INFORMATION memoryListInfo;
if (NT_SUCCESS(NtQuerySystemInformation(
SystemMemoryListInformation,
&memoryListInfo,
sizeof(SYSTEM_MEMORY_LIST_INFORMATION),
NULL
)))
{
ULONG_PTR standbyPageCount;
ULONG_PTR repurposedPageCount;
ULONG i;
standbyPageCount = 0;
repurposedPageCount = 0;
for (i = 0; i < 8; i++)
{
standbyPageCount += memoryListInfo.PageCountByPriority[i];
repurposedPageCount += memoryListInfo.RepurposedPagesByPriority[i];
}
SetDlgItemText(hwndDlg, IDC_ZLISTZEROED_V, PhaFormatSize((ULONG64)memoryListInfo.ZeroPageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTFREE_V, PhaFormatSize((ULONG64)memoryListInfo.FreePageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIED_V, PhaFormatSize((ULONG64)memoryListInfo.ModifiedPageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIEDNOWRITE_V, PhaFormatSize((ULONG64)memoryListInfo.ModifiedNoWritePageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTBAD_V, PhaFormatSize((ULONG64)memoryListInfo.BadPageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY_V, PhaFormatSize((ULONG64)standbyPageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY0_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[0] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY1_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[1] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY2_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[2] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY3_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[3] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY4_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[4] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY5_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[5] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY6_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[6] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY7_V, PhaFormatSize((ULONG64)memoryListInfo.PageCountByPriority[7] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED_V, PhaFormatSize((ULONG64)repurposedPageCount * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED0_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[0] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED1_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[1] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED2_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[2] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED3_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[3] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED4_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[4] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED5_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[5] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED6_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[6] * PAGE_SIZE, -1)->Buffer);
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED7_V, PhaFormatSize((ULONG64)memoryListInfo.RepurposedPagesByPriority[7] * PAGE_SIZE, -1)->Buffer);
if (WindowsVersion >= WINDOWS_8)
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIEDPAGEFILE_V, PhaFormatSize((ULONG64)memoryListInfo.ModifiedPageCountPageFile * PAGE_SIZE, -1)->Buffer);
else
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIEDPAGEFILE_V, L"N/A");
}
else
{
SetDlgItemText(hwndDlg, IDC_ZLISTZEROED_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTFREE_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIED_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIEDNOWRITE_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTMODIFIEDPAGEFILE_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTBAD_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY0_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY1_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY2_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY3_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY4_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY5_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY6_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTSTANDBY7_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED0_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED1_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED2_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED3_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED4_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED5_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED6_V, L"Unknown");
SetDlgItemText(hwndDlg, IDC_ZLISTREPURPOSED7_V, L"Unknown");
}
}
INT_PTR CALLBACK PhpMemoryListsDlgProc(
_In_ HWND hwndDlg,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
switch (uMsg)
{
case WM_INITDIALOG:
{
PhRegisterCallback(&PhProcessesUpdatedEvent, ProcessesUpdatedCallback, NULL, &ProcessesUpdatedRegistration);
PhpUpdateMemoryListInfo(hwndDlg);
PhLoadWindowPlacementFromSetting(L"MemoryListsWindowPosition", NULL, hwndDlg);
PhRegisterDialog(hwndDlg);
}
break;
case WM_DESTROY:
{
PhUnregisterDialog(hwndDlg);
PhSaveWindowPlacementToSetting(L"MemoryListsWindowPosition", NULL, hwndDlg);
PhUnregisterCallback(&PhProcessesUpdatedEvent, &ProcessesUpdatedRegistration);
UnregisterDialogFunction(hwndDlg);
PhMemoryListsWindowHandle = NULL;
}
break;
case WM_COMMAND:
{
switch (GET_WM_COMMAND_ID(wParam, lParam))
{
case IDCANCEL:
case IDOK:
DestroyWindow(hwndDlg);
break;
case IDC_EMPTY:
{
PPH_EMENU menu;
RECT buttonRect;
POINT point;
PPH_EMENU_ITEM selectedItem;
SYSTEM_MEMORY_LIST_COMMAND command = -1;
menu = PhCreateEMenu();
PhLoadResourceEMenuItem(menu, PhInstanceHandle, MAKEINTRESOURCE(IDR_EMPTYMEMLISTS), 0);
GetClientRect(GetDlgItem(hwndDlg, IDC_EMPTY), &buttonRect);
point.x = 0;
point.y = buttonRect.bottom;
ClientToScreen(GetDlgItem(hwndDlg, IDC_EMPTY), &point);
selectedItem = PhShowEMenu(menu, hwndDlg, PH_EMENU_SHOW_LEFTRIGHT,
PH_ALIGN_LEFT | PH_ALIGN_TOP, point.x, point.y);
if (selectedItem)
{
switch (selectedItem->Id)
{
case ID_EMPTY_EMPTYWORKINGSETS:
command = MemoryEmptyWorkingSets;
break;
case ID_EMPTY_EMPTYMODIFIEDPAGELIST:
command = MemoryFlushModifiedList;
break;
case ID_EMPTY_EMPTYSTANDBYLIST:
command = MemoryPurgeStandbyList;
break;
case ID_EMPTY_EMPTYPRIORITY0STANDBYLIST:
command = MemoryPurgeLowPriorityStandbyList;
break;
case ID_EMPTY_COMBINEMEMORYLISTS:
{
NTSTATUS status;
HANDLE tokenHandle;
MEMORY_COMBINE_INFORMATION_EX combineInfo = { 0 };
if (NT_SUCCESS(NtOpenProcessToken(NtCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &tokenHandle)))
{
PhSetTokenPrivilege(tokenHandle, SE_PROF_SINGLE_PROCESS_NAME, NULL, SE_PRIVILEGE_ENABLED);
NtClose(tokenHandle);
}
status = NtSetSystemInformation(
SystemCombinePhysicalMemoryInformation,
&combineInfo,
sizeof(MEMORY_COMBINE_INFORMATION_EX)
);
if (NT_SUCCESS(status))
{
PhShowInformation2(
hwndDlg,
L"Memory pages combined",
L"%s (%llu pages)",
PhaFormatSize(combineInfo.PagesCombined * PAGE_SIZE, -1)->Buffer,
combineInfo.PagesCombined
);
}
else
{
PhShowStatus(hwndDlg, L"Unable to combine memory pages", status, 0);
}
}
break;
}
}
if (command != -1)
{
NTSTATUS status;
SetCursor(LoadCursor(NULL, IDC_WAIT));
status = NtSetSystemInformation(
SystemMemoryListInformation,
&command,
sizeof(SYSTEM_MEMORY_LIST_COMMAND)
);
SetCursor(LoadCursor(NULL, IDC_ARROW));
if (status == STATUS_PRIVILEGE_NOT_HELD)
{
if (!PhGetOwnTokenAttributes().Elevated)
{
if (PhUiConnectToPhSvc(hwndDlg, FALSE))
{
SetCursor(LoadCursor(NULL, IDC_WAIT));
status = PhSvcCallIssueMemoryListCommand(command);
SetCursor(LoadCursor(NULL, IDC_ARROW));
PhUiDisconnectFromPhSvc();
}
else
{
// User cancelled eleavtion.
status = STATUS_SUCCESS;
}
}
}
if (!NT_SUCCESS(status))
{
PhShowStatus(hwndDlg, L"Unable to execute the memory list command", status, 0);
}
}
PhDestroyEMenu(menu);
}
break;
}
}
break;
case MSG_UPDATE:
{
PhpUpdateMemoryListInfo(hwndDlg);
}
break;
}
return FALSE;
}