Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C++
Article

Process enumeration class

Rate me:
Please Sign up or sign in to vote.
3.50/5 (6 votes)
6 Jun 2007CPOL2 min read 26.3K   1.1K   18   3
This class can be used for the enumeration of processes and modules.

Screenshot - image.jpg

Introduction

This article is regarding how to enumerate the processes running currently as well as enumerate the modules loaded in a particular process. There are two classes provided for these enumerations.

The Problem

During the development of certain software, we need many information about the processes and modules. This class can help to do this (e.g., if we need the version information of a process or module, this file class help you).

Basic Usage

This class exposes the complete interface for enumerating the processes, and the modules for a process.

There are two classes, CEnumProcessList and CModuleInfo. CEnumProcessList enumerates the processes and modules, and you can query for the module information with the CModuleInfo class.

The functions exposed by the CEnumProcessList class as public methods include:

No.Function NameDescription
1CEnumProcessList

This is the default constructor which enumerates all the running processes in the system.

2CEnumProcessList(DWORD dwProcessID);

This constructor takes the Process ID as a parameter, and makes a list of modules that are loaded that are related to the process.

3BOOL GetModuleVersion(CString ModuleName,CString &VersionString);

Returns the version of the the process/module that has been passed to the function. You have to query the version according to the list.

4BOOL SearchProcessOrModule(CString ModuleName,CModuleDetails* pModuleData);

You can search for a process or a module with this function.

5BOOL HasFailed();Returns if the status indicates failure.
6POSITION GetHeadPosition();
7CModuleDetails* GetNext(POSITION &Pos);Gets the next position in the list.
8DWORD LastError();Returns the last error occurred while processing on process or modules.
9static CString FormatError(DWORD dwError);Returns the formatted error.

How the Class Works

As the name suggests, when we create an object of CEnumProcessList with the default constructor, the class enumerates the current running processes in the system using the EnumProcesses function, and makes a list of them as CModuleInfo objects. The class uses the CModuleInfo class to store process information.

Now, we can query the class with the function like GetHeadPosition to get the elements from the list. To get the list of all modules loaded by a process, we have to use another constructor of the class with the parameter, process ID. This function will generate the list of all the modules loaded by the process .

Using this Class in Your Application

You need to add the ProcListP.h and ProcListP.CPP files in your project. Now, it depends where you want to use the class. The class can be used as given below:

//To get the list of all processes
CEnumProcessList * m_listp = new CEnumProcessList;
//This will genrate the list of all processes

for(POSITION modulePos = m_listp->GetHeadPosition();modulePos!=NULL;) 
{ 
    CModuleDetails* pModuleData = new CModuleDetails;
    pModuleData = m_listp->GetNext(modulePos); 
    //You can use it to extarct all the informatio about the process
}

//To genrate the list of all modules of a process

CEnumProcessList * m_listp =  newCEnumProcessList(pModuleInfo->pProcessID);
for(POSITION modulePos1 = m_listp->GetHeadPosition();modulePos1!=NULL;)
{
  CModuleDetails* pModuleData = new CModuleDetails;
  pModuleData = m_listp->GetNext(modulePos1);
 //You can use it to extarct all the informatio about the modules related to the process

}

Error Codes

You can query for errors with the functions provided with the class.

Future Improvements and/or Enhancements

  • MFC dependency
  • OS dependency
  • Custom error code can be defined for the class.
  • Other ways can also be implemented instead of only using psapi.lib.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMemory leak on all new CModuleDetails and new CEnumProcessList in CProcessListDlg class Pin
MaxMax1419-Feb-22 6:54
MaxMax1419-Feb-22 6:54 
AnswerRe: Memory leak on all new CModuleDetails and new CEnumProcessList in CProcessListDlg class Pin
jung-kreidler11-May-22 4:31
jung-kreidler11-May-22 4:31 
Problem is that the list m_listp is created inside the function call, which causes more problems...
Just move m_listp from local function to the header file and do a delete(m_listp) in OnDestroy(); of the dialog (OnDestroy must be added).
Next one is this:
CModuleDetails* pModuleData = new CModuleDetails;
pModuleData = m_listp->GetNext(modulePos);
This is wrong, as CModuleDetails is already created inside the list. Just assign the pModuleData to the pointer returned.

CModuleDetails* pModuleData;
pModuleData = m_listp->GetNext(modulePos);


Regards,
jung-kreidler
QuestionError on m_listp->GetHeadPosition() when selection of subtree Pin
MaxMax1411-Feb-22 2:04
MaxMax1411-Feb-22 2:04 

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.