public bool TryMoveNext(Toolhelp32.Snapshot snap, out Toolhelp32.IEntry entry) { var x = new WinProcessEntry { dwSize = Marshal.SizeOf(typeof(WinProcessEntry)) }; var b = Process32Next(snap, ref x); entry = x; return(b); }
public static extern bool Process32Next(Toolhelp32.Snapshot snap, ref WinProcessEntry entry);
public static ProcessInfo[] OldGetProcessInfos() { IntPtr ptr = (IntPtr)(-1); GCHandle handle = new GCHandle(); ArrayList list = new ArrayList(); Hashtable hashtable = new Hashtable(); try { ptr = CreateToolhelp32Snapshot(6, 0); if (ptr == ((IntPtr)(-1))) { return(null); } int num = Marshal.SizeOf(typeof(WinProcessEntry)); int val = num + 260; int[] numArray = new int[val / 4]; handle = GCHandle.Alloc(numArray, GCHandleType.Pinned); IntPtr ptr2 = handle.AddrOfPinnedObject(); Marshal.WriteInt32(ptr2, val); HandleRef ref2 = new HandleRef(null, ptr); if (Process32First(ref2, ptr2)) { do { WinProcessEntry entry = new WinProcessEntry(); Marshal.PtrToStructure(ptr2, entry); ProcessInfo info = new ProcessInfo(); string path = Marshal.PtrToStringAnsi((IntPtr)(((long)ptr2) + num)); info.processName = Path.ChangeExtension(Path.GetFileName(path), null); info.handleCount = entry.cntUsage; info.processId = entry.th32ProcessID; info.basePriority = entry.pcPriClassBase; info.mainModuleId = entry.th32ModuleID; hashtable.Add(info.processId, info); Marshal.WriteInt32(ptr2, val); }while (Process32Next(ref2, ptr2)); } WinThreadEntry structure = new WinThreadEntry(); structure.dwSize = Marshal.SizeOf(structure); if (Thread32First(ref2, structure)) { do { ThreadInfo info2 = new ThreadInfo(); info2.threadId = structure.th32ThreadID; info2.processId = structure.th32OwnerProcessID; info2.basePriority = structure.tpBasePri; info2.currentPriority = structure.tpBasePri + structure.tpDeltaPri; list.Add(info2); }while (Thread32Next(ref2, structure)); } for (int i = 0; i < list.Count; i++) { ThreadInfo info3 = (ThreadInfo)list[i]; ProcessInfo info4 = (ProcessInfo)hashtable[info3.processId]; if (info4 != null) { info4.threadInfoList.Add(info3); } } } finally { if (handle.IsAllocated) { handle.Free(); } if (ptr != ((IntPtr)(-1))) { CloseHandle(new HandleRef(null, ptr)); } } ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count]; hashtable.Values.CopyTo(array, 0); return(array); }