static void print_string(const WCHAR *string) { DWORD count, ret, len, lena; char *buf; if(wshInteractive) { static const WCHAR windows_script_hostW[] = {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0}; MessageBoxW(NULL, string, windows_script_hostW, MB_OK); return; } len = strlenW(string); ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL); if(ret) { static const WCHAR crnlW[] = {'\r','\n'}; WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), crnlW, sizeof(crnlW)/sizeof(*crnlW), &count, NULL); return; } lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL); buf = heap_alloc(len); if(!buf) return; WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE); heap_free(buf); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE); }
static int output_write(const WCHAR* str, int len) { DWORD ret, count; ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL); if (!ret) { DWORD lenA; char* strA; /* On Windows WriteConsoleW() fails if the output is redirected. So fall * back to WriteFile(), assuming the console encoding is still the right * one in that case. */ lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL); strA = HeapAlloc(GetProcessHeap(), 0, lenA); if (!strA) return 0; WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, strA); } return count; }
static int reg_printfW(const WCHAR *msg, ...) { va_list va_args; int wlen; DWORD count, ret; WCHAR msg_buffer[8192]; va_start(va_args, msg); vsprintfW(msg_buffer, msg, va_args); va_end(va_args); wlen = lstrlenW(msg_buffer); ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL); if (!ret) { DWORD len; char *msgA; len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, NULL, 0, NULL, NULL); msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char)); if (!msgA) return 0; WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, msgA); } return count; }
static int hostname_vprintfW(const WCHAR *msg, va_list va_args) { int wlen; DWORD count, ret; WCHAR msg_buffer[8192]; wlen = vsprintfW(msg_buffer, msg, va_args); ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL); if (!ret) { DWORD len; char *msgA; len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, NULL, 0, NULL, NULL); msgA = HeapAlloc(GetProcessHeap(), 0, len); if (!msgA) return 0; WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, msgA); } return count; }
/** Output given message to stdout without formatting. */ static void output(const WCHAR *message) { DWORD count; DWORD res; int wlen = strlenW(message); if (!wlen) return; res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL); /* If writing to console fails, assume its file i/o so convert to OEM codepage and output */ if (!res) { DWORD len; char *mesA; /* Convert to OEM, then output */ len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL ); mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char)); if (!mesA) return; WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL ); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, mesA); } }
static int taskkill_vprintfW(const WCHAR *msg, __ms_va_list va_args) { int wlen; DWORD count, ret; WCHAR msg_buffer[8192]; wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer, sizeof(msg_buffer)/sizeof(*msg_buffer), &va_args); ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL); if (!ret) { DWORD len; char *msgA; /* On Windows WriteConsoleW() fails if the output is redirected. So fall * back to WriteFile(), assuming the console encoding is still the right * one in that case. */ len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, NULL, 0, NULL, NULL); msgA = HeapAlloc(GetProcessHeap(), 0, len); if (!msgA) return 0; WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, msgA); } return count; }
static void hb_gt_cgi_Init( PHB_GT pGT, HB_FHANDLE hFilenoStdin, HB_FHANDLE hFilenoStdout, HB_FHANDLE hFilenoStderr ) { PHB_GTCGI pGTCGI; HB_TRACE( HB_TR_DEBUG, ( "hb_gt_cgi_Init(%p,%p,%p,%p)", pGT, ( void * ) ( HB_PTRDIFF ) hFilenoStdin, ( void * ) ( HB_PTRDIFF ) hFilenoStdout, ( void * ) ( HB_PTRDIFF ) hFilenoStderr ) ); pGTCGI = ( PHB_GTCGI ) hb_xgrab( sizeof( HB_GTCGI ) ); memset( pGTCGI, 0, sizeof( HB_GTCGI ) ); HB_GTLOCAL( pGT ) = pGTCGI; pGTCGI->hStdout = hFilenoStdout; #if defined( HB_OS_WIN ) && ! defined( HB_OS_WIN_CE ) if( IsValidCodePage( CP_UTF8 ) ) { pGTCGI->uiOldCP = GetConsoleOutputCP(); SetConsoleOutputCP( CP_UTF8 ); HB_GTSELF_SETDISPCP( pGT, "UTF8", NULL, HB_FALSE ); } #endif pGTCGI->szCrLf = hb_strdup( hb_conNewLine() ); pGTCGI->nCrLf = strlen( pGTCGI->szCrLf ); hb_fsSetDevMode( pGTCGI->hStdout, FD_BINARY ); HB_GTSUPER_INIT( pGT, hFilenoStdin, hFilenoStdout, hFilenoStderr ); HB_GTSELF_SETFLAG( pGT, HB_GTI_STDOUTCON, HB_TRUE ); }
// Console, ANSI VOID my_putsCA( HANDLE h, LPCTSTR s ) { DWORD n1, n2; DWORD len = 0; LPSTR p; #ifdef UNICODE UINT cp = GetConsoleOutputCP(); if( ( len = WideCharToMultiByte( cp, 0, s, -1, NULL, 0, NULL, NULL ) ) == 0 ) return; if( ( p = (LPSTR)LocalAlloc( LMEM_FIXED, len ) ) == NULL ) return; len = WideCharToMultiByte( cp, 0, s, -1, p, len, NULL, NULL ); #else size_t n; p = (LPTSTR)s; if( StringCbLength( p, 4096, &n ) != S_OK ) len = 0; else len = n; #endif n1 = len ? len -1 : 0; while( n1 ){ if( !WriteFile( h, p, n1, &n2, NULL ) ) break; n1 -= n2; } #ifdef UNICODE LocalFree( p ); #endif }
/** * Prepare console on program initialization: change console font codepage * according to program options and hide cursor. */ void setup_console(void) { HANDLE hOut; CONSOLE_CURSOR_INFO cci; int cp = (opt.flags&OPT_UTF8 ? CP_UTF8 : opt.flags&OPT_ANSI ? GetACP() : GetOEMCP()); rhash_data.saved_console_codepage = -1; /* note: we are using numbers 1 = _fileno(stdout), 2 = _fileno(stderr) */ /* cause _fileno() is undefined, when compiling as strict ansi C. */ if(cp > 0 && IsValidCodePage(cp) && (isatty(1) || isatty(2)) ) { rhash_data.saved_console_codepage = GetConsoleOutputCP(); SetConsoleOutputCP(cp); setlocale(LC_CTYPE, opt.flags&OPT_UTF8 ? "C" : opt.flags&OPT_ANSI ? ".ACP" : ".OCP"); rsh_exit = rhash_exit; } if((opt.flags & OPT_PERCENTS) != 0) { hOut = GetStdHandle(STD_ERROR_HANDLE); if(hOut != INVALID_HANDLE_VALUE) { /* store current cursor size and visibility flag */ GetConsoleCursorInfo(hOut, &cci); rhash_data.saved_cursor_size = (cci.bVisible ? cci.dwSize : 0); /* now hide cursor */ cci.bVisible = 0; SetConsoleCursorInfo(hOut, &cci); /* hide cursor */ } } }
/* * The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing. * Attempting to restore on exit fails to restore if the program is terminated by the user. * Solution - set the output format each printf. */ void console_vprintf(const char *fmt, va_list ap) { UINT cp = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); vprintf(fmt, ap); SetConsoleOutputCP(cp); }
void kull_m_output_init() { previousStdOut = _setmode(_fileno(stdout), _O_U8TEXT); previousStdErr = _setmode(_fileno(stderr), _O_U8TEXT); previousConsoleOutput = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); }
PyObject * _Py_device_encoding(int fd) { #if defined(MS_WINDOWS) UINT cp; #endif if (!_PyVerify_fd(fd) || !isatty(fd)) { Py_RETURN_NONE; } #if defined(MS_WINDOWS) if (fd == 0) cp = GetConsoleCP(); else if (fd == 1 || fd == 2) cp = GetConsoleOutputCP(); else cp = 0; /* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application has no console */ if (cp != 0) return PyUnicode_FromFormat("cp%u", (unsigned int)cp); #elif defined(CODESET) { char *codeset = nl_langinfo(CODESET); if (codeset != NULL && codeset[0] != 0) return PyUnicode_FromString(codeset); } #endif Py_RETURN_NONE; }
/* Windows console (cmd.exe) input to utf8 */ static char* oem_to_utf8(char *instring) { char * out; wchar_t *winput, *wp; size_t len; const char *p; int cv; unsigned int consolecp; consolecp = GetConsoleOutputCP(); /* Convert input string to wide chars. */ len = strlen(instring) + 1; cv = MultiByteToWideChar(consolecp, 0, instring, len, NULL, 0); winput = (wchar_t*) malloc(cv * sizeof(wchar_t)); cv = MultiByteToWideChar(consolecp, 0, instring, len, winput, cv); /* Convert wide chars to utf8. */ cv = WideCharToMultiByte(CP_UTF8, 0, winput, len, NULL, 0, NULL, NULL); out = (char*) malloc(cv); cv = WideCharToMultiByte(CP_UTF8, 0, winput, len, out, cv, NULL, NULL); free(winput); return out; }
BOOL WINAPI OnReadConsoleInputA(HANDLE hConsoleInput, PINPUT_RECORD lpBuffer, DWORD nLength, LPDWORD lpNumberOfEventsRead) { //typedef BOOL (WINAPI* OnReadConsoleInputA_t)(HANDLE hConsoleInput, PINPUT_RECORD lpBuffer, DWORD nLength, LPDWORD lpNumberOfEventsRead); SUPPRESSORIGINALSHOWCALL; ORIGINAL_KRNL(ReadConsoleInputA); //if (gpFarInfo && bMainThread) // TouchReadPeekConsoleInputs(0); BOOL lbRc = FALSE; #if defined(_DEBUG) #if 1 UINT nCp = GetConsoleCP(); UINT nOutCp = GetConsoleOutputCP(); UINT nOemCp = GetOEMCP(); UINT nAnsiCp = GetACP(); #endif #endif // To minimize startup duration and possible problems // hook server will start on first 'user interaction' CheckHookServer(); if (ph && ph->PreCallBack) { SETARGS4(&lbRc,hConsoleInput,lpBuffer,nLength,lpNumberOfEventsRead); // Если функция возвращает FALSE - реальное чтение не будет вызвано if (!ph->PreCallBack(&args)) return lbRc; } CESERVER_CONSOLE_APP_MAPPING* pAppMap = NULL; PreReadConsoleInput(hConsoleInput, rcif_Ansi|rcif_LLInput, &pAppMap); //#ifdef USE_INPUT_SEMAPHORE //DWORD nSemaphore = ghConInSemaphore ? WaitForSingleObject(ghConInSemaphore, INSEMTIMEOUT_READ) : 1; //_ASSERTE(nSemaphore<=1); //#endif lbRc = F(ReadConsoleInputA)(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead); PostReadConsoleInput(hConsoleInput, rcif_Ansi|rcif_LLInput, pAppMap); //#ifdef USE_INPUT_SEMAPHORE //if ((nSemaphore == WAIT_OBJECT_0) && ghConInSemaphore) ReleaseSemaphore(ghConInSemaphore, 1, NULL); //#endif if (ph && ph->PostCallBack) { SETARGS4(&lbRc,hConsoleInput,lpBuffer,nLength,lpNumberOfEventsRead); ph->PostCallBack(&args); } if (lbRc && lpNumberOfEventsRead && *lpNumberOfEventsRead && lpBuffer) { OnPeekReadConsoleInput('R', 'A', hConsoleInput, lpBuffer, *lpNumberOfEventsRead); } return lbRc; }
void WindowManager::setup() { //Set console font CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx; lpConsoleCurrentFontEx.cbSize = sizeof(CONSOLE_FONT_INFOEX); lpConsoleCurrentFontEx.dwFontSize.X = 12; lpConsoleCurrentFontEx.dwFontSize.Y = 12; lpConsoleCurrentFontEx.FontWeight = 700; lpConsoleCurrentFontEx.nFont = 1; lpConsoleCurrentFontEx.FontFamily = FF_DONTCARE; lstrcpyW(lpConsoleCurrentFontEx.FaceName, L"Lucida Console"); SetCurrentConsoleFontEx ( GetStdHandle(STD_OUTPUT_HANDLE), false, &lpConsoleCurrentFontEx ); //get handles and create screen buffers mStdHandle = GetStdHandle(STD_OUTPUT_HANDLE); mConsoleA = Console::createConsoleOutput(); mConsoleB = Console::createConsoleOutput(); mSwitch = true; CONSOLE_SCREEN_BUFFER_INFO info; if (GetConsoleScreenBufferInfo(mStdHandle, &info) != 0) { mCurrentSize.X = (info.srWindow.Right - info.srWindow.Left) + 1; mCurrentSize.Y = (info.srWindow.Bottom - info.srWindow.Top) + 1; } else { mCurrentSize.X = 80; mCurrentSize.Y = 25; } //get current codepage and set new one, unicode mOldCodePage = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); }
int qse_main (int argc, qse_achar_t* argv[]) { int x; #if defined(_WIN32) char locale[100]; UINT codepage = GetConsoleOutputCP(); if (codepage == CP_UTF8) { /*SetConsoleOUtputCP (CP_UTF8);*/ qse_setdflcmgrbyid (QSE_CMGR_UTF8); } else { sprintf (locale, ".%u", (unsigned int)codepage); setlocale (LC_ALL, locale); /*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ } #else setlocale (LC_ALL, ""); /*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ #endif qse_openstdsios (); x = qse_runmain (argc, argv, test_main); qse_closestdsios (); return x; }
PW32CP const struct php_win32_cp *php_win32_cp_do_setup(const char *enc) {/*{{{*/ if (!enc) { enc = php_win32_cp_get_enc(); } cur_cp = php_win32_cp_get_by_enc(enc); if (!orig_cp) { orig_cp = php_win32_cp_get_by_id(GetACP()); } if (!strcmp(sapi_module.name, "cli")) { if (!orig_in_cp) { orig_in_cp = php_win32_cp_get_by_id(GetConsoleCP()); if (!orig_in_cp) { orig_in_cp = orig_cp; } } if (!orig_out_cp) { orig_out_cp = php_win32_cp_get_by_id(GetConsoleOutputCP()); if (!orig_out_cp) { orig_out_cp = orig_cp; } } php_win32_cp_cli_io_setup(); } return cur_cp; }/*}}}*/
static void init_ti(void) { CONSOLE_SCREEN_BUFFER_INFO info; int i; UINT acp; if (ti_flag) return; ti_flag = 1; for (i = 0; i<2; ++i) { if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info)) break; if (i) return; FreeConsole(); AllocConsole(); } ti.normattr = info.wAttributes; ti.winright = info.dwSize.X; ti.winbottom = info.dwSize.Y; ti.screenwidth = info.dwSize.X; ti.screenheight = info.dwSize.Y; ti.curx = info.dwCursorPosition.X + 1; ti.cury = info.dwCursorPosition.Y + 1; ti.attribute = info.wAttributes; acp = GetACP(); if (GetConsoleOutputCP() != acp) { SetConsoleOutputCP(acp); SetConsoleCP(acp); } SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_ECHO_INPUT); }
int ShowConsoleStatus() { DWORD dwKbdDelay; DWORD dwKbdSpeed; CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo; HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); wprintf(L"\nStatus for device CON:\n"); wprintf(L"-----------------------\n"); if (GetConsoleScreenBufferInfo(hConsoleOutput, &ConsoleScreenBufferInfo)) { wprintf(L" Lines: %d\n", ConsoleScreenBufferInfo.dwSize.Y); wprintf(L" Columns: %d\n", ConsoleScreenBufferInfo.dwSize.X); } if (SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, &dwKbdDelay, 0)) { wprintf(L" Keyboard delay: %ld\n", dwKbdDelay); } if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &dwKbdSpeed, 0)) { wprintf(L" Keyboard rate: %ld\n", dwKbdSpeed); } wprintf(L" Code page: %d\n", GetConsoleOutputCP()); return 0; }
void _wprintf(LPCWSTR asBuffer) { if (!asBuffer) return; int nAllLen = lstrlenW(asBuffer); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); DWORD dwWritten = 0; if (!IsOutputRedirected()) { WriteConsoleW(hOut, asBuffer, nAllLen, &dwWritten, 0); } else { UINT cp = GetConsoleOutputCP(); int cchMax = WideCharToMultiByte(cp, 0, asBuffer, -1, NULL, 0, NULL, NULL) + 1; char* pszOem = (cchMax > 1) ? (char*)malloc(cchMax) : NULL; if (pszOem) { int nWrite = WideCharToMultiByte(cp, 0, asBuffer, -1, pszOem, cchMax, NULL, NULL); if (nWrite > 1) { // Don't write terminating '\0' to redirected output WriteFile(hOut, pszOem, nWrite-1, &dwWritten, 0); } free(pszOem); } } }
static void __cdecl output_write(UINT id, ...) { WCHAR fmt[1024]; __ms_va_list va_args; WCHAR *str; DWORD len, nOut, ret; if (Silent) return; if (!LoadStringW(GetModuleHandleW(NULL), id, fmt, sizeof(fmt)/sizeof(fmt[0]))) { WINE_FIXME("LoadString failed with %d\n", GetLastError()); return; } __ms_va_start(va_args, id); SetLastError(NO_ERROR); len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ALLOCATE_BUFFER, fmt, 0, 0, (LPWSTR)&str, 0, &va_args); __ms_va_end(va_args); if (len == 0 && GetLastError() != NO_ERROR) { WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt)); return; } ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &nOut, NULL); /* WriteConsole fails if its output is redirected to a file. * If this occurs, we should use an OEM codepage and call WriteFile. */ if (!ret) { DWORD lenA; char *strA; lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, NULL, 0, NULL, NULL); strA = HeapAlloc(GetProcessHeap(), 0, lenA); if (strA) { WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &nOut, FALSE); HeapFree(GetProcessHeap(), 0, strA); } } LocalFree(str); }
static void _init_console_info(void) { DWORD scrnmode, len; HKEY reghnd; int i; console_info.Hwnd = _find_console_handle(); console_info.Length = sizeof(console_info); GetConsoleMode(pdc_con_in, &scrnmode); console_info.QuickEdit = !!(scrnmode & 0x0040); console_info.InsertMode = !!(scrnmode & 0x0020); console_info.FullScreen = FALSE; console_info.AutoPosition = 0x10000; console_info.ScreenColors = SP->orig_back << 4 | SP->orig_fore; console_info.PopupColors = 0xf5; console_info.HistoryNoDup = FALSE; console_info.HistoryBufferSize = 50; console_info.NumberOfHistoryBuffers = 4; console_info.CodePage = GetConsoleOutputCP(); RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Console"), 0, KEY_QUERY_VALUE, ®hnd); len = sizeof(DWORD); /* Default color table */ for (i = 0; i < 16; i++) { char tname[13]; sprintf(tname, "ColorTable%02d", i); RegQueryValueExA(reghnd, tname, NULL, NULL, (LPBYTE)(&(console_info.ColorTable[i])), &len); } /* Font info */ RegQueryValueEx(reghnd, TEXT("FontSize"), NULL, NULL, (LPBYTE)(&console_info.FontSize), &len); RegQueryValueEx(reghnd, TEXT("FontFamily"), NULL, NULL, (LPBYTE)(&console_info.FontFamily), &len); RegQueryValueEx(reghnd, TEXT("FontWeight"), NULL, NULL, (LPBYTE)(&console_info.FontWeight), &len); len = sizeof(WCHAR) * 32; RegQueryValueExW(reghnd, L"FaceName", NULL, NULL, (LPBYTE)(console_info.FaceName), &len); RegCloseKey(reghnd); }
void kull_m_output_init() { #ifndef _POWERKATZ #ifndef _WINDLL previousStdOut = _setmode(_fileno(stdout), _O_U8TEXT); previousStdErr = _setmode(_fileno(stderr), _O_U8TEXT); #endif previousConsoleOutput = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); #endif }
/** Initialize the console. */ ConsoleInitializer::ConsoleInitializer() : m_inputCP(GetConsoleCP()), m_outputCP(GetConsoleOutputCP()) { // http://msdn.microsoft.com/en-us/library/ms686036%28VS.85%29.aspx //bool b1 = SetConsoleCP(65001); // set to UTF-8 //bool b2 = SetConsoleOutputCP(65001); // set to UTF-8 //bool b1 = SetConsoleCP(1200); // set to UTF-16LE //bool b2 = SetConsoleOutputCP(1200); // set to UTF-16LE //unsigned inputCP = GetConsoleCP(); //unsigned outputCP = GetConsoleOutputCP(); }
wint_t __cdecl _putwch_nolock ( wchar_t ch ) { int size, num_written; static int use_w = 2; char mbc[MB_LEN_MAX +1]; if ( use_w) { if (_confh == -2) __initconout(); /* write character to console file handle */ if (_confh == -1) return WEOF; else if ( !WriteConsoleW( (HANDLE)_confh, (LPVOID)&ch, 1, &num_written, NULL ) ) { if ( use_w == 2 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) use_w = 0; else return WEOF; } else use_w = 1; } if ( use_w == 0) { size = WideCharToMultiByte( GetConsoleOutputCP(), 0, (LPWSTR)&ch, 1, mbc, MB_LEN_MAX, NULL, NULL ); if ( (_confh == -1) || !WriteConsole( (HANDLE)_confh, (LPVOID)mbc, size, &num_written, NULL ) ) /* return error indicator */ return WEOF; } return ch; }
void setup_utf8() { #ifdef UNICODE /* Ensure we write in UTF-8 mode, so that non-ASCII characters don't get mangled. If we were compiled in ANSI mode it won't work. */ cp = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); _setmode(_fileno(stdout), _O_U8TEXT); _setmode(_fileno(stderr), _O_U8TEXT); #endif }
int fputsU(const char *buf, FILE *f) { /* fputs a UTF-8 string */ int n; if (!codePage) codePage = GetConsoleOutputCP(); if (codePage != CP_UTF8) n = ConvertString(buf, strlen(buf)+1, CP_UTF8, codePage, NULL); if (n > 0) { /* If no error, and something to output */ int iErr = fputs(buf, f); if ((iErr >= 0) && DEBUG_IS_ON()) fflush(f); /* Slower, but ensures we get everything before crashes! */ if (iErr < 0) n = iErr; } return n; /* Return the error (n<0) or success (n>=0) */ }
void PrintInit() { #ifdef __Windows__ G_OLD_CP = GetConsoleOutputCP(); SetConsoleOutputCP(CP_UTF8); #endif PrintDestination(stdout); PrintColor(RED+GREEN+BLUE); G_IN_COLOR = false; G_PRINT_LOG = NULL; }
static void output_writeconsole(const WCHAR *str, DWORD wlen) { DWORD count, ret; ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, wlen, &count, NULL); if (!ret) { DWORD len; char *msgA; /* On Windows WriteConsoleW() fails if the output is redirected. So fall * back to WriteFile(), assuming the console encoding is still the right * one in that case. */ len = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, NULL, 0, NULL, NULL); msgA = HeapAlloc(GetProcessHeap(), 0, len * sizeof(char)); if (!msgA) return; WideCharToMultiByte(GetConsoleOutputCP(), 0, str, wlen, msgA, len, NULL, NULL); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE); HeapFree(GetProcessHeap(), 0, msgA); } }
void FileTests::TestUtf8WriteFileInvalid() { Log::Comment(L"Backup original console codepage."); UINT const uiOriginalCP = GetConsoleOutputCP(); auto restoreOriginalCP = wil::scope_exit([&] { Log::Comment(L"Restore original console codepage."); SetConsoleOutputCP(uiOriginalCP); }); HANDLE const hOut = GetStdOutputHandle(); VERIFY_IS_NOT_NULL(hOut, L"Verify we have the standard output handle."); VERIFY_WIN32_BOOL_SUCCEEDED(SetConsoleOutputCP(CP_UTF8), L"Set output codepage to UTF8"); DWORD dwWritten; DWORD dwExpectedWritten; char* str; DWORD cbStr; // \x80 is an invalid UTF-8 continuation // \x40 is the @ symbol which is valid. str = "\x80\x40"; cbStr = (DWORD)strlen(str); dwWritten = 0; dwExpectedWritten = cbStr; VERIFY_WIN32_BOOL_SUCCEEDED(WriteFile(hOut, str, cbStr, &dwWritten, nullptr)); VERIFY_ARE_EQUAL(dwExpectedWritten, dwWritten); // \x80 is an invalid UTF-8 continuation // \x40 is the @ symbol which is valid. str = "\x80\x40\x40"; cbStr = (DWORD)strlen(str); dwWritten = 0; dwExpectedWritten = cbStr; VERIFY_WIN32_BOOL_SUCCEEDED(WriteFile(hOut, str, cbStr, &dwWritten, nullptr)); VERIFY_ARE_EQUAL(dwExpectedWritten, dwWritten); // \x80 is an invalid UTF-8 continuation // \x40 is the @ symbol which is valid. str = "\x80\x80\x80\x40"; cbStr = (DWORD)strlen(str); dwWritten = 0; dwExpectedWritten = cbStr; VERIFY_WIN32_BOOL_SUCCEEDED(WriteFile(hOut, str, cbStr, &dwWritten, nullptr)); VERIFY_ARE_EQUAL(dwExpectedWritten, dwWritten); }