-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoryHelper.cpp
More file actions
68 lines (57 loc) · 1.99 KB
/
MemoryHelper.cpp
File metadata and controls
68 lines (57 loc) · 1.99 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
#include "Base.h"
#include "MemoryHelper.h"
#include "PlatformUtils.h"
NS_BEGIN
//#################################################################################################
CMemoryHelper::CMemoryHelper(void)
: m_hNtDll(nullptr),
m_pfnZwQueryInformationProcess(nullptr)
{
m_hNtDll = SystemLoadLibrary(_N("ntdll.dll"));
if(m_hNtDll)
m_pfnZwQueryInformationProcess = (PFNZWQUERYINFORMATIONPROCESS)GetProcAddress(m_hNtDll, "ZwQueryInformationProcess");
}
//#################################################################################################
CMemoryHelper::~CMemoryHelper(void)
{
if(m_hNtDll)
FreeLibrary(m_hNtDll);
}
//#################################################################################################
uint64_t CMemoryHelper::GetProcessPrivateWorkingSet(const HANDLE hProcess) const
{
Assert(hProcess);
// The following structures are in lieu of including <ntddk.h> which is a part of the DDK
struct VM_COUNTERS_EX final
{
SIZE_T PeakVirtualSize;
SIZE_T VirtualSize;
ULONG PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
SIZE_T PrivateUsage;
};
struct VM_COUNTERS_EX2 final
{
VM_COUNTERS_EX CountersEx;
SIZE_T PrivateWorkingSetSize;
ULONGLONG SharedCommitUsage;
};
// End ntddk.h substitute
static constexpr auto PROCESS_VM_COUNTERS = 3;
uint64_t nPrivateWS = 0;
if(m_pfnZwQueryInformationProcess)
{ // GetProcessMemoryInfo() returns the same info as ZwQueryInformationProcess(), but ZwQueryInformationProcess() also returns the private working set size
VM_COUNTERS_EX2 vm = {0};
if(NT_SUCCESS(m_pfnZwQueryInformationProcess(hProcess, (PROCESSINFOCLASS)PROCESS_VM_COUNTERS, &vm, sizeof(vm), nullptr)))
nPrivateWS = vm.PrivateWorkingSetSize;
}
return nPrivateWS;
}
NS_END