'Windows only method that writes unicode strings correctly to the windows console using the Win32 API'

def write_unicode_text(self, text, ignore_errors=False):

    if self.is_console:
        from ctypes import wintypes, byref, c_wchar_p
        written = wintypes.DWORD(0)
        text = text.replace(u'\x00', u'')
        chunk = len(text)
        while text:
            (t, text) = (text[:chunk], text[chunk:])
            wt = c_wchar_p(t)
            if (not self.write_console(self.file_handle, wt, len(t), byref(written), None)):
                import winerror
                err = ctypes.get_last_error()
                if ((err == winerror.ERROR_NOT_ENOUGH_MEMORY) and (chunk >= 128)):
                    chunk = (chunk // 2)
                    text = (t + text)
                    continue
                if (err == winerror.ERROR_GEN_FAILURE):
                    print(u"Non-ASCII    text    detected.    You    must    set    your    Console's    font    to    Lucida    Console    or    Consolas    or    some    other    TrueType    font    to    see    this    text", file=self.stream, end=u'    --    ')
                    from calibre.utils.filenames import ascii_text
                    print(ascii_text((t + text)), file=self.stream, end=u'')
                    continue
                if (not ignore_errors):
                    raise ctypes.WinError(err)
