Exemplo n.º 1
0
 private static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
Exemplo n.º 2
0
        public static IEnumerable<PROCESSENTRY32> SnapProcesses()
        {
            IntPtr snapHandle = INVALID_HANDLE_VALUE;
            snapHandle = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0);
            if (snapHandle == IntPtr.Zero || snapHandle == INVALID_HANDLE_VALUE)  // Couldn't snap, use a different method
            {
                yield break;
            }

            var autoDisposer = new AutoDispose<IntPtr>(snapHandle, (handle) => CloseHandle(handle));

            PROCESSENTRY32 procEntry = new PROCESSENTRY32();
            procEntry.dwSize = (uint)Marshal.SizeOf(procEntry);
            if (Process32First(autoDisposer.Value, ref procEntry))  // Go through each of the processes
            {
                do
                {
                    yield return procEntry;
                } while (Process32Next(autoDisposer.Value, ref procEntry)); // Next process
            }
        }