GetConsoleProcessList (kernel32)
Last changed: -89.197.96.204

.
Summary
Fills an array of type uint with the process ID's of all processes attached to this console.

C# Signature:

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern uint GetConsoleProcessList(
        uint[] ProcessList,
        uint ProcessCount
        );

VB Signature:

Declare Function GetConsoleProcessList Lib "kernel32.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Tips & Tricks:

To determine if the console will be destroyed when your application exits, check if (processCount < 2). For example:

        public static bool IsFinalProcess()
        {
        // See: https://devblogs.microsoft.com/oldnewthing/20160125-00/?p=92922
        uint[] procIDs = new uint[64];
        uint processCount = GetConsoleProcessList(procIDs, 64);
        return (processCount < 2);
        }

Check IsFinalProcess() as your application exits. If true, use Console.ReadKey(false); to prevent the window disappearing.

Sample Code:

        uint[] procIDs = new uint[64];
        uint processCount = GetConsoleProcessList(procIDs, 64);

Documentation