コード例 #1
0
        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);
        }
コード例 #2
0
        public static ProcessInfo[] NtGetProcessInfos(IntPtr dataPtr)
        {
            IntPtr    ptr;
            Hashtable hashtable = new Hashtable(100);
            uint      num       = 0;

Label_000B:
            ptr = (IntPtr)(((uint)dataPtr) + num);
            SystemProcessInformation structure = new SystemProcessInformation();

            Marshal.PtrToStructure(ptr, structure);

            ProcessInfo info = new ProcessInfo();

            info.processId         = structure.UniqueProcessId.ToInt32();
            info.handleCount       = (int)structure.HandleCount;
            info.sessionId         = (int)structure.SessionId;
            info.poolPagedBytes    = (long)structure.QuotaPagedPoolUsage;
            info.poolNonpagedBytes = (long)structure.QuotaNonPagedPoolUsage;
            info.virtualBytes      = (long)structure.VirtualSize;
            info.virtualBytesPeak  = (long)structure.PeakVirtualSize;
            info.workingSetPeak    = (long)structure.PeakWorkingSetSize;
            info.workingSet        = (long)structure.WorkingSetSize;
            info.pageFileBytesPeak = (long)structure.PeakPagefileUsage;
            info.pageFileBytes     = (long)structure.PagefileUsage;
            info.privateBytes      = (long)structure.PrivatePageCount;
            info.basePriority      = structure.BasePriority;


            hashtable[info.processId] = info;
            try
            {
                ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(structure));
            }
            catch
            {
            }
            for (int i = 0; i < structure.NumberOfThreads; i++)
            {
                SystemThreadInformation information2 = new SystemThreadInformation();
                Marshal.PtrToStructure(ptr, information2);
                ThreadInfo info2 = new ThreadInfo
                {
                    processId        = (int)information2.UniqueProcess,
                    threadId         = (int)information2.UniqueThread,
                    basePriority     = information2.BasePriority,
                    currentPriority  = information2.Priority,
                    startAddress     = information2.StartAddress,
                    threadState      = (ThreadState)information2.ThreadState,
                    threadWaitReason = GetThreadWaitReason((int)information2.WaitReason)
                };
                info.threadInfoList.Add(info2);
                try
                {
                    ptr = (IntPtr)(((long)ptr) + Marshal.SizeOf(information2));
                }
                catch
                {
                }
            }
            if (structure.NextEntryOffset != 0)
            {
                num += (uint)structure.NextEntryOffset;
                goto Label_000B;
            }

            ProcessInfo[] array = new ProcessInfo[hashtable.Values.Count];
            hashtable.Values.CopyTo(array, 0);
            return(array);
        }