Примерно так
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern SafeProcessHandle OpenProcess(int access, bool inherit, int processId);
using (var processHandler = SafeProcessHandle.OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId))
{
if (!processHandler.IsInvalid)
{
int needed = 0;
IntPtr[] hMods = new IntPtr[1024];
var strBuilder = new StringBuilder(1024);
var handle2 = new GCHandle();
try
{
if (!GetModuleFileNameEx(processHandler, IntPtr.Zero, strBuilder, (uint)(strBuilder.Capacity)))
return;
fnFileFound(strBuilder.ToString());
handle2 = GCHandle.Alloc(hMods, GCHandleType.Pinned);
var uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
var flag = EnumProcessModulesEx(processHandler, handle2.AddrOfPinnedObject(), uiSize, ref needed, LIST_MODULES_ALL);
if (flag != 0)
{
var uiTotalNumberofModules = needed / (Marshal.SizeOf(typeof(IntPtr)));
for (var i = 0; i < uiTotalNumberofModules; i++)
{
GetModuleFileNameEx(processHandler, hMods[i], strBuilder, (uint)(strBuilder.Capacity));
fnFileFound(strBuilder.ToString());
if (!Active)
break;
}
}
}
finally
{
if (handle2.IsAllocated)
handle2.Free();
}
}
}