ReadConsoleOutputCharacter (kernel32)
Last changed: -24.87.64.223

.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput,
   [Out] StringBuilder lpCharacter, uint nLength, COORD dwReadCoord,
   out uint lpNumberOfCharsRead);

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

[StructLayout(LayoutKind.Sequential)]

private struct COORD

{

    public short X;
    public short Y;

}

// Before reading from the console, allocate the console using AllocConsole(), or attach to an existing console using AttachConsole().

// Then call GetStdHandle() to get an IntPtr for the stdout handle.

private string ReadFromConsole(IntPtr stdout)

{

    uint nLength = 80;
    StringBuilder lpCharacter = new StringBuilder((int)nLength);

    // read from the first character of the first line (0, 0)
    COORD dwReadCoord;
    dwReadCoord.X = 0;
    dwReadCoord.Y = 0;

    uint lpNumberOfCharsRead = 0;

    if ( !ReadConsoleOutputCharacter(stdout, lpCharacter, nLength, dwReadCoord, out lpNumberOfCharsRead) )
    throw new Win32Exception();

    return lpCharacter.ToString();

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation