WriteConsoleOutput (kernel32)
Last changed: John Leitch (john@autosectools.com)-24.236.197.35

.
Summary

C# Signature:

    /* Writes character and color attribute data to a specified rectangular block of character cells in a console screen buffer.
        The data to be written is taken from a correspondingly sized rectangular block at a specified location in the source buffe */
    [DllImport("kernel32.dll", EntryPoint = "WriteConsoleOutputW", CharSet = CharSet.Unicode, SetLastError = true)]
    internal static extern BOOL WriteConsoleOutput(
        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), In] CHAR_INFO[,] lpBuffer,
        COORD dwBufferSize,
        COORD dwBufferCoord,
        ref SMALL_RECT lpWriteRegion);

User-Defined Types:

None.

Notes:

None.

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
                };
                break;

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

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

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

        short
            xI = 0,
            yI = 0;

        short
            xII = 10,
            yII = 4;

        short
            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))
        {
            cb.ReadLine();
        }
        }
    }
    }

Alternative Managed API:

Do you know one? Please contribute it!

Documentation