1 // 2 // cputws.cpp 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // Defines _cputws(), which writes a wide string directly to the console. 7 // 8 #include <conio.h> 9 #include <errno.h> 10 #include <corecrt_internal_lowio.h> 11 #include <limits.h> 12 #include <stdio.h> 13 #include <stdlib.h> 14 15 // Writes the given string directly to the console. No newline is appended. 16 // 17 // Returns 0 on success; nonzero on failure. 18 extern "C" int __cdecl _cputws(wchar_t const* string) 19 { 20 _VALIDATE_CLEAR_OSSERR_RETURN((string != nullptr), EINVAL, -1); 21 22 if (__dcrt_lowio_ensure_console_output_initialized() == FALSE) 23 return -1; 24 25 // Write string to console file handle: 26 size_t length = wcslen(string); 27 28 __acrt_lock(__acrt_conio_lock); 29 30 int result = 0; 31 32 __try 33 { 34 while (length > 0) 35 { 36 static size_t const max_write_bytes = 65535; 37 static size_t const max_write_wchars = max_write_bytes / sizeof(wchar_t); 38 39 DWORD const wchars_to_write = length > max_write_wchars 40 ? max_write_wchars 41 : static_cast<DWORD>(length); 42 43 DWORD wchars_written; 44 if (__dcrt_write_console( 45 string, 46 wchars_to_write, 47 &wchars_written) == FALSE) 48 { 49 result = -1; 50 __leave; 51 } 52 53 string += wchars_to_write; 54 length -= wchars_to_write; 55 } 56 } 57 __finally 58 { 59 __acrt_unlock(__acrt_conio_lock); 60 } 61 __endtry 62 63 return result; 64 } 65