[ATTRIB] Simplify the ErrorMessage() function.

And send the errors to the error stream.
This commit is contained in:
Hermès Bélusca-Maïto 2023-05-18 12:12:39 +02:00
parent 0f50a22d3b
commit f870bbe1d4
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0

View file

@ -49,41 +49,32 @@ CON_SCREEN StdOutScreen = INIT_CON_SCREEN(StdOut);
static static
VOID VOID
ErrorMessage( ErrorMessage(
DWORD dwErrorCode, _In_ DWORD dwErrorCode,
LPWSTR szFormat, _In_opt_ PCWSTR pszMsg,
...) ...)
{ {
WCHAR szMsg[RC_STRING_MAX_SIZE]; INT Len;
WCHAR szMessage[1024];
LPWSTR szError;
va_list arg_ptr; va_list arg_ptr;
if (dwErrorCode == ERROR_SUCCESS) if (dwErrorCode == ERROR_SUCCESS)
return; return;
if (szFormat) va_start(arg_ptr, pszMsg);
{ Len = ConMsgPrintfV(StdErr,
va_start(arg_ptr, szFormat); FORMAT_MESSAGE_FROM_SYSTEM,
vswprintf(szMessage, szFormat, arg_ptr); NULL,
va_end(arg_ptr); dwErrorCode,
} LANG_USER_DEFAULT,
&arg_ptr);
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, va_end(arg_ptr);
NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&szError, 0, NULL))
{
ConPrintf(StdOut, L"%s %s\n", szError, szMessage);
if (szError)
LocalFree(szError);
return;
}
/* Fall back just in case the error is not defined */ /* Fall back just in case the error is not defined */
LoadStringW(GetModuleHandle(NULL), STRING_CONSOLE_ERROR, szMsg, ARRAYSIZE(szMsg)); if (Len <= 0)
if (szFormat) ConResPrintf(StdErr, STRING_CONSOLE_ERROR, dwErrorCode);
ConPrintf(StdOut, L"%s -- %s\n", szMsg, szMessage);
else /* Display the extra optional message if necessary */
ConPrintf(StdOut, L"%s\n", szMsg); if (pszMsg)
ConPrintf(StdErr, L" %s\n", pszMsg);
} }
/* Returns TRUE if anything is printed, FALSE otherwise */ /* Returns TRUE if anything is printed, FALSE otherwise */