Skip to content

Instantly share code, notes, and snippets.

@vallahor
Last active January 12, 2024 19:04
Show Gist options
  • Save vallahor/ffcef418a9be18e8e78fc577f73f1984 to your computer and use it in GitHub Desktop.
Save vallahor/ffcef418a9be18e8e78fc577f73f1984 to your computer and use it in GitHub Desktop.
Enum Processes Windows
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#include <wtsapi32.h>
#pragma comment(lib, "wtsapi32")
void EnumProcToolhelp() {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return;
PROCESSENTRY32 pe;
pe.dwSize = sizeof(pe);
Process32First(snapshot, &pe);
do {
printf("PID: %6lu PPID: %6lu Threads: %3lu Process Name: %s\n",
pe.th32ProcessID, pe.th32ParentProcessID, pe.cntThreads, pe.szExeFile);
} while (Process32Next(snapshot, &pe));
CloseHandle(snapshot);
}
void EnumProcWTS() {
DWORD level = 1;
DWORD count;
WTS_PROCESS_INFO_EX* info;
if (!WTSEnumerateProcessesEx(nullptr, &level, WTS_ANY_SESSION, (LPSTR*) & info, &count))
return;
for (DWORD i = 0; i < count; i++) {
auto& p = info[i];
printf("PID: %6lu Session: %lu Threads: %3lu Handles: %6lu Process Name: %s\n",
p.ProcessId, p.SessionId, p.NumberOfThreads, p.HandleCount, p.pProcessName);
}
WTSFreeMemory(info);
}
int main() {
EnumProcToolhelp();
EnumProcWTS();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment