Gold mine of Visual C++ tricks!
How to Iterate all running process in your system.
![]()
In your system there might be several process running at the same time. So how to iterate through the running process in your system and get their infos?
![]()
You can use ToolHelp library for iterating and getting process information. First you’ve to take a snapshot of running process at that moment by calling CreateToolhelp32Snapshot(). Then you should iterate each process info by calling Process32First() and Process32Next(). See the code snippet below.
#include <tlhelp32.h>
...
// Get the process list snapshot.
HANDLE hProcessSnapShot = CreateToolhelp32Snapshot(
TH32CS_SNAPALL,
0 );
// Initialize the process entry structure.
PROCESSENTRY32 ProcessEntry = { 0 };
ProcessEntry.dwSize = sizeof( ProcessEntry );
// Get the first process info.
BOOL Return = FALSE;
Return = Process32First( hProcessSnapShot,
&ProcessEntry );
// Getting process info failed.
if( !Return )
{
return;
}
do
{
// print the process details.
cout << _T("Process EXE File:") << ProcessEntry.szExeFile
<< endl;
cout << _T("Process ID:") << ProcessEntry.th32ProcessID
<< endl;
cout << _T("Process References:") << ProcessEntry.cntUsage
<< endl;
cout << _T("Process Thread Count:") << ProcessEntry.cntThreads
<< endl;
// check the PROCESSENTRY32 for other members.
}
while( Process32Next( hProcessSnapShot, &ProcessEntry ));
// Close the handle
CloseHandle( hProcessSnapShot );
![]()
Together with process info, you can also get information regarding – Modules, Heaps, threads using these ToolHelp library functions. have a look at MSDN.
![]()
Targeted Audience – Intermediate.
| Print article | This entry was posted by Jijo Raj on May 2, 2008 at 6:28 pm, and is filed under Codeproject, Uncategorized, Windows APIs. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |