Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
readconsoleoutput (kernel32)

C# Signature:

    /* Reads character and color attribute data from a rectangular block of character cells in a console screen buffer,
        and the function writes the data to a rectangular block at a specified location in the destination buffer. */
    [DllImport("kernel32.dll", EntryPoint = "ReadConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern BOOL ReadConsoleOutput(
        SafeConsoleHandle hConsoleOutput,
        /* This pointer is treated as the origin of a two-dimensional array of CHAR_INFO structures
        whose size is specified by the dwBufferSize parameter.*/
        [MarshalAs(UnmanagedType.LPArray), Out] CHAR_INFO[,] lpBuffer,
        COORD dwBufferSize,
        COORD dwBufferCoord,
        ref SMALL_RECT lpReadRegion);

User-Defined Types:

    //these may be defined elsewhere on this site, but it seems handy to have them
    // all in one place

    //CHAR_INFO struct, which was a union in the old days
    // so we want to use LayoutKind.Explicit to mimic it as closely
    // as we can
    private struct CHAR_INFO
        internal char UnicodeChar;
        internal char AsciiChar;
        [FieldOffset(2)] //2 bytes seems to work properly
        internal UInt16 Attributes;

    //COORD struct
    private struct COORD
        public short X;
        public short Y;

    //SMALL_RECT struct
    private struct SMALL_RECT
        public short Left;
        public short Top;
        public short Right;
        public short Bottom;



Tips & Tricks:

Please add some!

Sample Code:

    class Program
    static void Main()
        using (var cb = new Console())
        var ainfoArray = new ConsoleApi.CHAR_INFO[4, 10];
        for (int e = 0; e < 4; e++)
            for (int i = 0; i < 10; i++)
            switch (e)
                case 0:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                    UnicodeChar = 'a',
                    Attributes = ConsoleApi.Attribute.Gray

                case 1:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                    UnicodeChar = 'b',
                    Attributes = ConsoleApi.Attribute.Red

                case 2:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                    UnicodeChar = 'c',
                    Attributes = ConsoleApi.Attribute.SkyBlue

                case 3:
                ainfoArray[e, i] = new ConsoleApi.CHAR_INFO
                    UnicodeChar = 'd',
                    Attributes = ConsoleApi.Attribute.Yellow

            xI = 0,
            yI = 0;

            xII = 10,
            yII = 4;

            xx = (SHORT) (xI + xII - 1),
            yy = (SHORT) (yI + yII - 1);

        var from = new ConsoleApi.COORD() { X = xI, Y = yI };
        var end = new ConsoleApi.COORD() { X = xII, Y = yII };
        var position = new ConsoleApi.SMALL_RECT() { Left = xI, Right = xx, Top = yI, Bottom = yy };
        if (ConsoleApi.WriteConsoleOutput(
            cb.StdOutput, ainfoArray, end, from, ref position))

Alternative Managed API:

Do you know one? Please contribute it!

(Sources at MSDN forums say there isn't one.)


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version