Get All Modules Used By Process In C#

Introduction

A process may use one or more modules. A module is either a .exe or a .dll file. By using the Windows Task Manager, you can see a list of all the processes that are currently running on the computer on the "Process" tab. However, have you ever wondered where the .exe or .dll files displayed on the Process tab are located? Sometimes you may suspect that an application is using .dll files from the wrong location. How can you determine that? The "Process" class provides the Modules method that you can use to retrieve a list of all the modules used by a process. By using this method, you can also retrieve information about a specific module, such as the name, the file name, and the memory used by the module. This information can help evaluate and optimize the performance of the application.

The following code sample shows the use of the "Modules" method. In this code sample, you create a process called process. You set its "StartInfor" property so that it executes Notepad. You then display the modules that are associated with Notepad and output information about each module to the command console. Then you display the same information for Notepad.

Example

class ProcessModules
{
    public void GetModules()
    {
        Process myProcess = new Process();
        // Get the process start information of notepad
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
        // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
        myProcess.StartInfo = myProcessStartInfo;
        // Create a notepad
        myProcess.Start();
        System.Threading.Thread.Sleep(1000);
        ProcessModule myProcessModule;
        // Get all the modules associated with 'myProcess'.
        ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
        Console.WriteLine("Properties of the modules associated with 'notepad' are:");
        // Display the properties of each of the modules.
        for (int i = 0; i < myProcessModuleCollection.Count; i++)
        {
            myProcessModule = myProcessModuleCollection[i];
            Console.WriteLine("The moduleName is " + myProcessModule.ModuleName);
            Console.WriteLine("The " + myProcessModule.ModuleName + "'s File Name is: " + myProcessModule.FileName);
            Console.WriteLine("The " + myProcessModule.ModuleName + "'s base address is: " + myProcessModule.BaseAddress);
            Console.WriteLine("For " + myProcessModule.ModuleName + " Entry point address is: " + myProcessModule.EntryPointAddress);
        }
        // Get the Main module associated with 'myProcess'.
        myProcessModule = myProcess.MainModule;
        Console.WriteLine("The Main Module associated");
        Console.WriteLine("The process's main module name is " + myProcessModule.ModuleName);
        Console.WriteLine("The process's main module name File Name is: " + myProcessModule.FileName);
        Console.WriteLine("The process's main module name base address is: " + myProcessModule.BaseAddress);
        Console.WriteLine("The process's main module name Entry point address is: " + myProcessModule.EntryPointAddress);
        myProcess.CloseMainWindow();
    }
}


Similar Articles