winapi: use FormatMessageA instead of FormatMessageW

Use FormatMessageA() to get the error message as multibyte-character
string (local codepage) directly, instead of using FormatMessageW()
and then convert the string from Unicode (UTF-16) to multi-byte (local
codepage) manually.

Prior to this change we used FormatMessageW + conversion because some
Windows CE did not have FormatMessageA. Since curl no longer supports
Windows CE, FormatMessageA can be used.

Closes https://github.com/curl/curl/pull/20261
This commit is contained in:
dEajL3kA 2026-01-16 11:20:48 +01:00 committed by Jay Satiro
parent 066ed13ca4
commit 4890074e68

View file

@ -39,30 +39,27 @@
const char *curlx_get_winapi_error(DWORD err, char *buf, size_t buflen)
{
char *p;
wchar_t wbuf[256];
DWORD wlen;
if(!buflen)
return NULL;
*buf = '\0';
*wbuf = L'\0';
/* We return the local codepage version of the error string because if it is
output to the user's terminal it will likely be with functions which
expect the local codepage (eg fprintf, failf, infof). */
wlen = FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
LANG_NEUTRAL, wbuf, CURL_ARRAYSIZE(wbuf), NULL);
if(wlen && !wcstombs_s(NULL, buf, buflen, wbuf, wlen)) {
/* Truncate multiple lines */
p = strchr(buf, '\n');
if(p) {
if(p > buf && *(p - 1) == '\r')
*(p - 1) = '\0';
else
*p = '\0';
}
if(!FormatMessageA((FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
LANG_NEUTRAL, buf, (DWORD)buflen, NULL)) {
*buf = '\0';
return NULL;
}
/* Truncate multiple lines */
p = strchr(buf, '\n');
if(p) {
if(p > buf && *(p - 1) == '\r')
*(p - 1) = '\0';
else
*p = '\0';
}
return *buf ? buf : NULL;