Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EnumProcesses() vs CreateToolhelp32Snapshot()

I was wondering if there are any differences - mostly performance wise - between the two Win32 API functions EnumProcesses() and CreateToolhelp32Snapshot() for enumerating all active processes and loaded modules. Or if one is better than the other to use and why.

like image 248
jay.lee Avatar asked Oct 26 '10 06:10

jay.lee


People also ask

What is Enumprocesses?

Retrieves the process identifier for each process object in the system.

What is CreateToolhelp32Snapshot?

Description & Usage. CreateToolhelp32Snapshot creates a snapshot of what is running on the computer the moment the function is called. Depending on the flags specified, this snapshot can include running processes and/or threads, among other things.


Video Answer


1 Answers

Here are results from few functions:

  • EnumProcesses: 16 msec, 207 processes
  • CreateToolhelp32Snapshot: 141 msec (16 msec), 207 processes
  • WTSEnumerateProcesses: 16 msec, 207 processes
  • WTSEnumerateProcessesEx(WTS_CURRENT_SESSION): 16 msec, 98 processes
  • WTSEnumerateProcessesEx(WTS_ANY_SESSION): 16 msec, 207 processes

Machine is running Windows 8 with UAC enabled, user is not elevated (e.g. have no access to system processes). Main process is 32-bit, machine is 64-bit, so plenty of 64-bit processes around. There are: session 0 for system, session 1 for current console user, session 2 for another fast-switch user. 207 processes in total (these are both 32- and 64-bit, including pseudo "system" process) - 207 is also confirmed by Process Explorer. Among these 207 processes: 23 processes are for session 2, 98 processes - for session 1, and remaining - for session 0.

Results are for cycle of 10 single function call. They are 100% reproducible on each run.

For CreateToolhelp32Snapshot the main result is call of CreateToolhelp32Snapshot itself, and second result (in brackets) is cycle with First/Next.

I think people confuse "enumerate all processes" (get PIDs) and "get name of process/exe". The first one ("enumerate") has no issues with x32/x64 cross-bitness whatsoever. But the latter one ("get name") does have issues - not every method will work across x32/x64.

like image 144
Alex Avatar answered Sep 28 '22 03:09

Alex