Example #1
0
        public static int[] GetProcessList(IntPtr handle)
        {
            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);
        }