Rollup merge of #114983 - crlf0710:formatmsg, r=ChrisDenton
Usage zero as language id for `FormatMessageW()` This switches the language selection from using system language (note that this might be different than application language, typically stored as thread ui language) to use `FormatMessageW` default search strategy, which is `neutral` first, then `thread ui lang`, then `user language`, then `system language`, then `English`. (See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagew) This allows the Rust program to take more control of `std::io::Error`'s message field, by setting up thread ui language themselves before hand (which many programs already do).
This commit is contained in:
commit
d761a5fe11
@ -25,10 +25,6 @@ pub fn errno() -> i32 {
|
|||||||
|
|
||||||
/// Gets a detailed string description for the given error number.
|
/// Gets a detailed string description for the given error number.
|
||||||
pub fn error_string(mut errnum: i32) -> String {
|
pub fn error_string(mut errnum: i32) -> String {
|
||||||
// This value is calculated from the macro
|
|
||||||
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
|
|
||||||
let langId = 0x0800 as c::DWORD;
|
|
||||||
|
|
||||||
let mut buf = [0 as c::WCHAR; 2048];
|
let mut buf = [0 as c::WCHAR; 2048];
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -56,13 +52,13 @@ pub fn error_string(mut errnum: i32) -> String {
|
|||||||
flags | c::FORMAT_MESSAGE_FROM_SYSTEM | c::FORMAT_MESSAGE_IGNORE_INSERTS,
|
flags | c::FORMAT_MESSAGE_FROM_SYSTEM | c::FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
module,
|
module,
|
||||||
errnum as c::DWORD,
|
errnum as c::DWORD,
|
||||||
langId,
|
0,
|
||||||
buf.as_mut_ptr(),
|
buf.as_mut_ptr(),
|
||||||
buf.len() as c::DWORD,
|
buf.len() as c::DWORD,
|
||||||
ptr::null(),
|
ptr::null(),
|
||||||
) as usize;
|
) as usize;
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
// Sometimes FormatMessageW can fail e.g., system doesn't like langId,
|
// Sometimes FormatMessageW can fail e.g., system doesn't like 0 as langId,
|
||||||
let fm_err = errno();
|
let fm_err = errno();
|
||||||
return format!("OS Error {errnum} (FormatMessageW() returned error {fm_err})");
|
return format!("OS Error {errnum} (FormatMessageW() returned error {fm_err})");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user