Click here to Skip to main content
15,390,470 members
Articles / Desktop Programming / MFC
Posted 5 Sep 2002


59 bookmarked

Enumerating processes : A practical approach

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
6 Sep 20023 min read
Enumerating processes using ToolHelp32 library and also shows how to change priority and terminate a process.


First of all, welcome to my tutorial "Enumerating Processes: A Practical Approach". Here, I will try to define what a process is and then we will use C++ and the ToolHelp32 API to find out currently running processes in our system.

What is a Process

A process is actually an instance of a running application. A system can have more than one instance of same application all running independently. A process can initiate a subprocess, which is called child process and the initiator process is sometimes referred to as a parent process. It is not like object-oriented inheritance where we can make instances of a child class without making instances of the parent class. Here parent process will be created first and then child processes and a child process can share some resources with parent process. Child processes cannot live without the parent process. This means if the parent process dies then child processes are also terminated.


I am using Visual C++ 6.0 Professional Edition and project type is Win32 Console Application (for simplicity). We will use functions from ToolHelp32 group of API. I am using Win2000 and hope it will work on Win9x as well. For WinNT we use PSAPI (Process Status API) functions and here we will not discuss them.

Now first we will include all necessary header files.

#include <windows.h>
#include <tlhelp32.h>
#include <iostream>	
#include <string>

using namespace std;

int main( )
    cout<<endl<<"Running Processes"<<endl;

Now we will take snapshot of currently running processes in our system using CreateToolhelp32Snapshot() function which returns handle to snapshot and it contains information about running processes. Its prototype is:

HANDLE WINAPI CreateToolhelp32Snapshot(
    DWORD dwFlags,
    DWORD th32ProcessID

In dwFlags we will use TH32CS_SNAPPROCESS and 0 for th32ProcessID. For other options see MSDN.

HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

Now we have information about all running processes in hSnapShot. We will extract data of each process from hSnapShot and put it into PROCESSENTRY32 structure, which represents a process, and it is a part of ToolHelp32 API. This extraction is done by using Process32First() and Process32Next() functions. Here we will only use Process32Next() function and it’s prototype is:

BOOL WINAPI Process32Next(
    HANDLE hSnapshot,

We must set size of PROCESSENTRY32 structure in dwSize member.

int index=0;

Here we are passing snapshot handle and PROCESSENTRY32 structure to Process32Next() function. After execution, PROCESSENTRY32 structure will contain information about a process. We are iterating through a loop until we got FALSE and this means there is now no process left to visit in snapshot and our pointer is at the end of snapshot.

cout<<endl<<"Parent Process ID: "<<processInfo->th32ParentProcessID;
cout<<endl<<"Process ID: "<<processInfo->th32ProcessID;
cout<<endl<<"Name: "<<processInfo->szExeFile;
cout<<endl<<"Current Threads: "<<processInfo->cntThreads;
cout<<endl<<"Current Usage: "<<processInfo->cntUsage;
cout<<endl<<"Flags: "<<processInfo->dwFlags;
cout<<endl<<"Size: "<<processInfo->dwSize;
cout<<endl<<"Primary Class Base: "<<processInfo->pcPriClassBase;
cout<<endl<<"Default Heap ID: "<<processInfo->th32DefaultHeapID;
cout<<endl<<"Module ID: "<<processInfo->th32ModuleID;

Don’t forget to close handle.


Now we will have all information about running processes including process ID (very important), file name, parent process ID etc. We can get handle of any running process by using OpenProcess() function.

HANDLE OpenProcess(
    DWORD dwDesiredAccess,  // access flag
    BOOL bInheritHandle,    // handle inheritance option
    DWORD dwProcessId       // process identifier

For description use MSDN.

int processID;
cout<<"Enter ProcessID to get handle of the process: ";

Here we are trying to get all possible access.

HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,processID);
    cout<<"Unable to get handle of process: "<<processID;
    cout<<"Error is: "<<GetLastError();
    return 1;

Now we have handle of a process and we can do all types of magical things. Here we are getting priority value of process using GetPriorityClass() function and then setting priority to high using SetPriorityClass() function.

cout<<endl<<"Priority Class: "<<GetPriorityClass(hProcess);

Now we are terminating process using TerminateProcess() function.

cout<<endl<<"Enter Process ID to terminate that process: ";
    cout<<"Unable to get handle of process: "<<processID;
    cout<<"Error is: "<<GetLastError();

When we create object on heap using new operator it is necessary to explicitly delete them using delete.

    delete processInfo;
    return 0;


In this tutorial I have tried to explain how to get information about running processes using C++. If you have any comments or question please email me.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Irfan Dawood
Web Developer
Pakistan Pakistan
Irfan Dawood is the junior year student at Department of Computer Science, University of Karachi. His interest includes programming in C++ and Java.

Comments and Discussions

QuestionHow Process32Next finishes enumeration? Pin
KonstKas13-Aug-14 3:05
MemberKonstKas13-Aug-14 3:05 
GeneralRunning Applications Pin
Member 20579142-Jul-08 20:48
MemberMember 20579142-Jul-08 20:48 
QuestionEnumerating only Application Pin
shilpa jain12-Jul-06 21:05
Membershilpa jain12-Jul-06 21:05 
AnswerRe: Enumerating only Application Pin
ThatsAlok4-Jun-08 19:01
MemberThatsAlok4-Jun-08 19:01 
QuestionTerminate Process tree Pin
dipali_be200323-Jun-06 1:41
Memberdipali_be200323-Jun-06 1:41 
GeneralOne question Pin
JaVinci5-Jun-05 21:28
MemberJaVinci5-Jun-05 21:28 
GeneralRe: One question [modified] Pin
kitty524-Aug-06 6:10
Memberkitty524-Aug-06 6:10 
Generalterminate processes, i need help! Pin
igor@446-May-05 2:37
Memberigor@446-May-05 2:37 
GeneralRe: terminate processes, i need help! Pin
gamitech30-Aug-05 10:26
Membergamitech30-Aug-05 10:26 
QuestionHow to set base priority of an application Pin
ledallam6-Sep-04 0:11
Memberledallam6-Sep-04 0:11 
GeneralProblem with name length Pin
Member 5157368-Oct-03 3:50
MemberMember 5157368-Oct-03 3:50 
Generalprocess termination Pin
salah_gis18-Dec-02 4:04
Membersalah_gis18-Dec-02 4:04 
Generalincorrect information Pin
Anonymous7-Nov-02 15:36
MemberAnonymous7-Nov-02 15:36 
Questionhow to check memory usage Pin
Anonymous14-Oct-02 1:06
MemberAnonymous14-Oct-02 1:06 
GeneralVery nicely done! Pin
Nish Nishant17-Sep-02 14:40
sitebuilderNish Nishant17-Sep-02 14:40 
QuestionHow to Append Results to String? Pin
Ed Suominen9-Sep-02 14:08
MemberEd Suominen9-Sep-02 14:08 
AnswerRe: How to Append Results to String? Pin
Garth J Lancaster9-Sep-02 14:41
professionalGarth J Lancaster9-Sep-02 14:41 
AnswerRe: How to Append Results to String? Pin
Mike Junkin24-Sep-02 10:09
MemberMike Junkin24-Sep-02 10:09 
GeneralInteresting... Pin
Shog96-Sep-02 14:47
sitebuilderShog96-Sep-02 14:47 
GeneralRe: Interesting... Pin
Irfan Dawood6-Sep-02 23:14
MemberIrfan Dawood6-Sep-02 23:14 
GeneralRe: Interesting... Pin
C-J Berg7-Sep-02 6:34
MemberC-J Berg7-Sep-02 6:34 
GeneralRe: Interesting... Pin
Irfan Dawood7-Sep-02 9:53
MemberIrfan Dawood7-Sep-02 9:53 
GeneralRe: Interesting... Pin
Anonymous3-Oct-02 20:17
MemberAnonymous3-Oct-02 20:17 
GeneralRe: Interesting... Pin
Silent Mobius7-Jun-04 0:26
MemberSilent Mobius7-Jun-04 0:26 
GeneralRe: Interesting... Pin
David 'dex' Schwartz8-Sep-08 17:14
MemberDavid 'dex' Schwartz8-Sep-08 17:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.