Retrieves a handle for each module in the specified process.
[DllImport("psapi.dll", SetLastError = true)]
public static extern bool EnumProcessModules(IntPtr hProcess,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4)] [In][Out] uint[] lphModule, uint cb, [MarshalAs(UnmanagedType.U4)] out uint lpcbNeeded);
<DllImport("psapi.dll", SetLastError:=True)> _
Public Shared Function EnumProcessModules(ByVal hProcess As IntPtr, <MarshalAs(UnmanagedType.LPArray, ArraySubType := UnmanagedType.U4)> <[In]()> <Out()> ByVal lphModule As UInteger(), ByVal cb As UInteger, <MarshalAs(UnmanagedType.U4)> ByRef lpcbNeeded As UInteger) As Boolean
End Function
None.
System.Diagnostics.Process.Modules
I found that the C# signature above did not work for me. Instead I am using the following signature:
c[DllImport("psapi.dll", CallingConvention=CallingConvention.StdCall, SetLastError = true)]
public static extern int EnumProcessModules(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded);
Please add some!
Used in conjuction with GetModuleFileNameEx:
Process[] pc = Process.GetProcessesByName("communicator");
foreach (Process p in pc)
{
// Setting up the variable for the second argument for EnumProcessModules
IntPtr[] hMods = new IntPtr[1024];
GCHandle gch = GCHandle.Alloc(hMods, GCHandleType.Pinned); // Don't forget to free this later
IntPtr pModules = gch.AddrOfPinnedObject();
// Setting up the rest of the parameters for EnumProcessModules
uint uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
uint cbNeeded = 0;
if (EnumProcessModules(p.Handle, pModules, uiSize, out cbNeeded) == 1)
{
Int32 uiTotalNumberofModules = (Int32)(cbNeeded / (Marshal.SizeOf(typeof(IntPtr))));
for (int i = 0; i < (int)uiTotalNumberofModules; i++)
{
StringBuilder strbld = new StringBuilder(1024);
GetModuleFileNameEx(p.Handle, hMods[i], strbld, (uint)(strbld.Capacity));
Console.WriteLine("File Path: " + strbld.ToString());
Console.WriteLine();
}
Console.WriteLine("Number of Modules: " + uiTotalNumberofModules);
Console.WriteLine();
}
// Must free the GCHandle object
gch.Free();
}