65.9K
CodeProject is changing. Read more.
Home

Process enumeration class

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.50/5 (6 votes)

Jun 6, 2007

CPOL

2 min read

viewsIcon

26700

downloadIcon

1094

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 Name Description
1 CEnumProcessList

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

2 CEnumProcessList(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.

3 BOOL 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.

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

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

5 BOOL HasFailed(); Returns if the status indicates failure.
6 POSITION GetHeadPosition();
7 CModuleDetails* GetNext(POSITION &Pos); Gets the next position in the list.
8 DWORD LastError(); Returns the last error occurred while processing on process or modules.
9 static 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.