In Process Explorer I can view all the dlls (and dll details) loaded by a process selected. How can do this programmatically?
I can get a specific process details like this. But unsure where to go from here?
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
There exists the Process.Modules
property which you can enumerate all Modules (exe and .dll's) loaded by the process.
foreach (var module in proc.Modules)
{
Console.WriteLine(string.Format("Module: {0}", module.FileName));
}
Per the ProcessModule
class which gives you the properties of a specific module.
The Process.Modules
solution is NOT sufficient when running a 64-bit process and trying to collect all modules from a 32-bit process. By default, 64-bit will only work on 64-bit processes, and 32-bit will only work on 32-bit processes.
For a solution that works for "AnyCPU", "x86", and "x64", simply call the CollectModules
function with the target process, see below. Note: Collecting 64-bit modules from a 32-bit process is not possible.
public List<Module> CollectModules(Process process)
{
List<Module> collectedModules = new List<Module>();
IntPtr[] modulePointers = new IntPtr[0];
int bytesNeeded = 0;
// Determine number of modules
if (!Native.EnumProcessModulesEx(process.Handle, modulePointers, 0, out bytesNeeded, (uint)Native.ModuleFilter.ListModulesAll))
{
return collectedModules;
}
int totalNumberofModules = bytesNeeded / IntPtr.Size;
modulePointers = new IntPtr[totalNumberofModules];
// Collect modules from the process
if (Native.EnumProcessModulesEx(process.Handle, modulePointers, bytesNeeded, out bytesNeeded, (uint)Native.ModuleFilter.ListModulesAll))
{
for (int index = 0; index < totalNumberofModules; index++)
{
StringBuilder moduleFilePath = new StringBuilder(1024);
Native.GetModuleFileNameEx(process.Handle, modulePointers[index], moduleFilePath, (uint)(moduleFilePath.Capacity));
string moduleName = Path.GetFileName(moduleFilePath.ToString());
Native.ModuleInformation moduleInformation = new Native.ModuleInformation();
Native.GetModuleInformation(process.Handle, modulePointers[index], out moduleInformation, (uint)(IntPtr.Size * (modulePointers.Length)));
// Convert to a normalized module and add it to our list
Module module = new Module(moduleName, moduleInformation.lpBaseOfDll, moduleInformation.SizeOfImage);
collectedModules.Add(module);
}
}
return collectedModules;
}
}
public class Native
{
[StructLayout(LayoutKind.Sequential)]
public struct ModuleInformation
{
public IntPtr lpBaseOfDll;
public uint SizeOfImage;
public IntPtr EntryPoint;
}
internal enum ModuleFilter
{
ListModulesDefault = 0x0,
ListModules32Bit = 0x01,
ListModules64Bit = 0x02,
ListModulesAll = 0x03,
}
[DllImport("psapi.dll")]
public static extern bool EnumProcessModulesEx(IntPtr hProcess, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] [In][Out] IntPtr[] lphModule, int cb, [MarshalAs(UnmanagedType.U4)] out int lpcbNeeded, uint dwFilterFlag);
[DllImport("psapi.dll")]
public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] uint nSize);
[DllImport("psapi.dll", SetLastError = true)]
public static extern bool GetModuleInformation(IntPtr hProcess, IntPtr hModule, out ModuleInformation lpmodinfo, uint cb);
}
public class Module
{
public Module(string moduleName, IntPtr baseAddress, uint size)
{
this.ModuleName = moduleName;
this.BaseAddress = baseAddress;
this.Size = size;
}
public string ModuleName { get; set; }
public IntPtr BaseAddress { get; set; }
public uint Size { get; set; }
}
This depends on what you want precisely.
Getting the list of .NET assemblies loaded into a specific app domain is easy (AppDomain.GetAssemblies
).
But listing the App Domains in a process isn't so easy but can be done.
However if you want a list of dll's – native and .NET – as Tony the Lion answers, is just Process.Modules
.