Skip to content

Instantly share code, notes, and snippets.

@imshvc
Created August 29, 2024 02:29
Show Gist options
  • Save imshvc/58c3b0d98c87781b1a968de4b15f200f to your computer and use it in GitHub Desktop.
Save imshvc/58c3b0d98c87781b1a968de4b15f200f to your computer and use it in GitHub Desktop.
Windows API: List Processes
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#ifdef __cplusplus
using namespace std;
#endif
void main() {
// We need to enumrate the list of processes.
HANDLE tlHelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
DWORD lastError = GetLastError();
if (lastError != 0) {
printf("GetLastError() returned: %d\n", lastError);
ExitProcess(lastError);
}
while (1) {
PROCESSENTRY32 procEntry = {0};
procEntry.dwSize = sizeof(PROCESSENTRY32);
Process32Next(tlHelp32Snapshot, &procEntry);
if (GetLastError() == ERROR_NO_MORE_FILES) {
break;
}
printf("ID: %7d - NAME: %s\n", procEntry.th32ProcessID, procEntry.szExeFile);
}
CloseHandle(tlHelp32Snapshot);
ExitProcess(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment