def init_kernel32():
    kernel32 = ctypes.LibraryLoader(ctypes.WinDLL).kernel32  # Load our own instance. Unique memory address.

    # Setup GetStdHandle.
    kernel32.GetStdHandle.argtypes = [ctypes.c_ulong]
    kernel32.GetStdHandle.restype = ctypes.c_void_p

    # Setup GetConsoleScreenBufferInfo.
    kernel32.GetConsoleScreenBufferInfo.argtypes = [
        ctypes.c_void_p,
        ctypes.POINTER(ConsoleScreenBufferInfo),
    ]
    kernel32.GetConsoleScreenBufferInfo.restype = ctypes.c_long

    # Get handles.
    stderr = kernel32.GetStdHandle(STD_ERROR_HANDLE)
    stdout = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

    return kernel32, stderr, stdout
Load a unique instance of WinDLL into memory, set arg/return types, and get stdout/err handles.
dataset/ETHPy150Open Robpol86/colorclass/colorclass/windows.py/init_kernel32