Skip to content

Instantly share code, notes, and snippets.

@nikanos
Last active November 2, 2022 10:10
Show Gist options
  • Save nikanos/775358e0ef5d276377356a3741988c17 to your computer and use it in GitHub Desktop.
Save nikanos/775358e0ef5d276377356a3741988c17 to your computer and use it in GitHub Desktop.
Windows Process List - Win32 code in C to retrieve the list of the processes using the CreateToolhelp32Snapshot() function
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
void PrintHeader()
{
_tprintf(TEXT("%10s %40s\n"), TEXT("Process ID"), TEXT("Process Name"));
}
void PrintProcessEntry(PROCESSENTRY32 pe)
{
_tprintf(TEXT("%10d %40s\n"), pe.th32ProcessID, pe.szExeFile);
}
int main()
{
HANDLE processSnapshotHandle;
PROCESSENTRY32 processEntry;
processSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (processSnapshotHandle != INVALID_HANDLE_VALUE)
{
processEntry.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(processSnapshotHandle, &processEntry))
{
PrintHeader();
PrintProcessEntry(processEntry);
while (Process32Next(processSnapshotHandle, &processEntry))
PrintProcessEntry(processEntry);
}
CloseHandle(processSnapshotHandle);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment