Skip to content

How to get the buffer size #2

@zadjii

Description

@zadjii

I'm mostly just leaving this snippet here for future me

def get_buffer_size():

    import ctypes
    class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
        _fields_ = [
            ('dwSize', ctypes.wintypes._COORD),
            ('dwCursorPosition', ctypes.wintypes._COORD),
            ('wAttributes', ctypes.c_ushort),
            ('srWindow', ctypes.wintypes._SMALL_RECT),
            ('dwMaximumWindowSize', ctypes.wintypes._COORD)
        ]

    ctypes.windll.kernel32.GetStdHandle.restype = ctypes.wintypes.HANDLE
    hstd = ctypes.windll.kernel32.GetStdHandle(-11) # STD_OUTPUT_HANDLE = -11
    csbi = CONSOLE_SCREEN_BUFFER_INFO()
    # print(csbi.__dict__)
    # print(csbi.srWindow)
    # print(csbi.srWindow.Top)
    # print(csbi.srWindow.Right)
    ret = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(
        hstd,
        ctypes.byref(csbi)
    )
    if ret == False:
        return Error('Failed to retrieve console buffer size')
    width = csbi.srWindow.Right - csbi.srWindow.Left
    height = csbi.srWindow.Bottom - csbi.srWindow.Top
    return Success( (width, height) )
    # print(csbi.__dict__)
    # print(csbi.srWindow)
    # print(csbi.srWindow.Top)
    # print(csbi.srWindow.Right)
    return ResultAndData(ret, csbi)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions