Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to language id 0 with FormatMessageA if the error messages are not available in system locale #3260

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions stl/src/syserror_import_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ _NODISCARD size_t __CLRCALL_PURE_OR_STDCALL __std_system_error_allocate_message(
if (_Ret == 0) {
_Lang_id = 0;
}
const unsigned long _Chars =
unsigned long _Chars =
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, _Message_id, _Lang_id, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);

if (_Chars == 0 && _Lang_id != 0) {
const DWORD _Last_error = GetLastError();
if (_Last_error == ERROR_MUI_FILE_NOT_FOUND || _Last_error == ERROR_RESOURCE_LANG_NOT_FOUND) {
LocalFree(*_Ptr_str);
_Chars = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
_Message_id, 0, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);
Comment on lines +61 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_Chars = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
_Message_id, 0, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);
const DWORD _US_lang_id = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
_Chars = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
_Message_id, _US_lang_id, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);

Could you test this? Does it work for you?
It works for me...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails if the US english locale is not installed (which it isn't on localized windows installs)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test it? What windows versions?
I believe this should work because:

  1. I tested it on a localized windows install(Russian)
  2. Look at https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea
 If you pass in zero, FormatMessage looks for a message for LANGIDs in the following order:

1. Language neutral
2. Thread LANGID, based on the thread's locale value
3. User default LANGID, based on the user's default locale value
4. System default LANGID, based on the system default locale value
5. US English

US English is the final fallback. This is why I think it should work.
Of course I may be wrong and more real tests are needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My program used to hardcode US english as well, but I had to change that because I would get failures on some systems (I tried a brand-new Japanese install of the OS and saw the issue, IIRC): TranslucentTB/TranslucentTB@2d1cd95

Copy link
Contributor

@frederick-vs-ja frederick-vs-ja Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a test to avoid regression?

}
}
return _CSTD __std_get_string_size_without_trailing_whitespace(*_Ptr_str, _Chars);
}

Expand Down