public IEnumerable <ProcessInfo> GetProcesses() { var snapshot = IntPtr.Zero; try { snapshot = CreateToolhelp32Snapshot((uint)SnapshotFlags.All, 0); var process = new ProcessEntry32 { dwSize = (uint)Marshal.SizeOf(typeof(ProcessEntry32)) }; if (Process32First(snapshot, ref process)) { do { var processInfo = new ProcessInfo { Name = process.szExeFile }; foreach (var module in GetModules(snapshot, process.th32ProcessID)) { var moduleInfo = new ModuleInfo { Size = module.modBaseSize, Name = module.szModule }; processInfo.Modules.Add(moduleInfo); processInfo.TotalSize += moduleInfo.Size; } yield return(processInfo); } while (Process32Next(snapshot, ref process)); } else { throw new ApplicationException($"System error: {Marshal.GetLastWin32Error()}"); } } finally { CloseHandle(snapshot); } }
static extern bool Process32Next([In] IntPtr hSnapshot, ref ProcessEntry32 lppe);