- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
bool nt_utils::enum_processes(std::vector<uint32_t>& pids)
{
pids.clear();
PDWORD process_ids = NULL;
DWORD cb = ALLOC_SIZE;
DWORD bytes_returned = 0;
BOOL complete = FALSE;
__try {
while (!complete) {
process_ids = static_cast<PDWORD>(
::HeapAlloc(
::GetProcessHeap(),
HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY,
cb
)
);
if (::EnumProcesses(process_ids, cb, &bytes_returned)) {
if (cb == bytes_returned) {
::HeapFree(::GetProcessHeap(), 0, process_ids);
process_ids = NULL;
cb *= 2;
} else {
complete = TRUE;
}
} else {
::HeapFree(::GetProcessHeap(), 0, process_ids);
process_ids = NULL;
complete = TRUE;
}
}
if (process_ids) {
DWORD size = bytes_returned / sizeof(DWORD);
pids.reserve(size);
for (DWORD i = 0; i < size; ++i) {
assert(process_ids[i]);
pids[i] = process_ids[i];
}
::HeapFree(::GetProcessHeap(), 0, process_ids);
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
if (process_ids) {
::HeapFree(::GetProcessHeap(), 0, process_ids);
}
return false;
}
return !!process_ids;
}