Exemplo n.º 1
0
        /// <summary>
        /// Gets the list of processes that are attached to the console.
        /// </summary>
        /// <returns>Returns an array of integer process ids.</returns>
        public static int[] GetProcessList()
        {
            int[] processes = new int[10];
            do
            {
                int rslt = WinCon.GetConsoleProcessList(processes, processes.Length);
                if (rslt == 0)
                {
                    throw new IOException("Unable to get process list", Marshal.GetLastWin32Error());
                }
                if (rslt <= processes.Length)
                {
                    // if the array is exactly the right size, return it
                    if (rslt == processes.Length)
                    {
                        return(processes);
                    }

                    // otherwise create a new array of the required length
                    int[] newProcesses = new int[rslt];
                    Array.Copy(processes, newProcesses, rslt);
                    return(newProcesses);
                }
                else
                {
                    // The initial array was too small.
                    // Allocate more space and try again.
                    processes = new int[rslt];
                }
            } while (true);
        }