From 30ea5da70276b1b48e7a38ee6097bf0c9c9adfad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Tue, 18 Feb 2025 23:05:43 +0100 Subject: [PATCH] win32-console-wrapper: simplify error printing No need for pointless prefix, print error directly. Don't use CRT function. This will write only to console, but it's not meant to be written to file anyway. --- osdep/win32-console-wrapper.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/osdep/win32-console-wrapper.c b/osdep/win32-console-wrapper.c index a5791631d9..75d8e58a19 100644 --- a/osdep/win32-console-wrapper.c +++ b/osdep/win32-console-wrapper.c @@ -24,18 +24,19 @@ int wmain(int argc, wchar_t **argv, wchar_t **envp); -static void cr_perror(const wchar_t *prefix) +static void cr_perror(void) { - wchar_t *error; + LPWSTR error = NULL; + DWORD len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, GetLastError(), + LANG_USER_DEFAULT, + (LPWSTR)&error, 0, NULL); - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&error, 0, NULL); - - fwprintf(stderr, L"%ls: %ls", prefix, error); + HANDLE out = GetStdHandle(STD_ERROR_HANDLE); + if (out != INVALID_HANDLE_VALUE && GetConsoleMode(out, &(DWORD){0})) + WriteConsoleW(out, error, len, NULL, NULL); LocalFree(error); } @@ -66,10 +67,8 @@ static int cr_runproc(wchar_t *name, wchar_t *cmdline) }; si.dwFlags = (si.hStdInput || si.hStdOutput || si.hStdError) ? STARTF_USESTDHANDLES : 0; PROCESS_INFORMATION pi = {0}; - if (!CreateProcessW(name, cmdline, NULL, NULL, TRUE, 0, - NULL, NULL, &si, &pi)) { - - cr_perror(L"CreateProcess"); + if (!CreateProcessW(name, cmdline, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) { + cr_perror(); } else { WaitForSingleObject(pi.hProcess, INFINITE); GetExitCodeProcess(pi.hProcess, &retval);