diff options
| author | chooyy <[email protected]> | 2026-06-18 15:24:43 +0800 |
|---|---|---|
| committer | chooyy <[email protected]> | 2026-06-18 15:24:43 +0800 |
| commit | ca9e48d741ad4c1e6a004137fdda530bce22407a (patch) | |
| tree | fa4130b0f81b369b5a3560a5350a30c3a7ceedd5 | |
| parent | 2245e0b6ed0f954aae1dafe225c2b436ae98cfcd (diff) | |
use FormatMessageW for Windows system error messages
| -rw-r--r-- | core/src/xmake/os/strerror.c | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/core/src/xmake/os/strerror.c b/core/src/xmake/os/strerror.c index c1ae41039..d54671fef 100644 --- a/core/src/xmake/os/strerror.c +++ b/core/src/xmake/os/strerror.c @@ -64,8 +64,16 @@ tb_int_t xm_os_strerror(lua_State *lua) { lua_pushstring(lua, strerr); } else { #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - tb_char_t strerr[128] = { 0 }; - tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)GetLastError()); + DWORD error_code = GetLastError(); + tb_char_t strerr[128 * 2] = { 0 }; + tb_wchar_t wstrerr[128] = { 0 }; + if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + wstrerr, tb_arrayn(wstrerr), NULL)) { + WideCharToMultiByte(CP_UTF8, 0, wstrerr, (tb_int_t)tb_wcslen(wstrerr), strerr, sizeof(strerr), tb_null, tb_null); + } else { + tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)error_code); + } lua_pushstring(lua, strerr); #else lua_pushstring(lua, strerror(errno)); |
