From a6245a56d82d50e2f3f502ca81ba332bef9ba41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sun, 25 Jul 2021 14:34:48 +0200 Subject: [PATCH] builtin: fix Windows panic message (#10952) --- vlib/builtin/builtin_windows.c.v | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/vlib/builtin/builtin_windows.c.v b/vlib/builtin/builtin_windows.c.v index 741009c326..97592cff0c 100644 --- a/vlib/builtin/builtin_windows.c.v +++ b/vlib/builtin/builtin_windows.c.v @@ -54,8 +54,6 @@ fn C.SymFromAddr(h_process voidptr, address u64, p_displacement voidptr, p_symbo fn C.SymGetLineFromAddr64(h_process voidptr, address u64, p_displacement voidptr, p_line &Line64) int -fn C.FormatMessage(dwFlags u32, lpSource voidptr, dwMessageId u32, dwLanguageId u32, lpBuffer &voidptr, nSize u32, Arguments voidptr) u32 - // Ref - https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-symsetoptions const ( symopt_undname = 0x00000002 @@ -282,17 +280,19 @@ fn break_if_debugger_attached() { // return an error message generated from WinAPI's `LastError` pub fn winapi_lasterr_str() string { - mut msgbuf := &byte(0) err_msg_id := C.GetLastError() - res := C.FormatMessage(C.FORMAT_MESSAGE_ALLOCATE_BUFFER | C.FORMAT_MESSAGE_FROM_SYSTEM, - C.NULL, err_msg_id, 0, &msgbuf, 0, C.NULL) + if err_msg_id == 8 { + // handle this case special since `FormatMessage()` might not work anymore + return 'insufficient memory' + } + mut msgbuf := &u16(0) + res := C.FormatMessage(C.FORMAT_MESSAGE_ALLOCATE_BUFFER | C.FORMAT_MESSAGE_FROM_SYSTEM | C.FORMAT_MESSAGE_IGNORE_INSERTS, + C.NULL, err_msg_id, C.MAKELANGID(C.LANG_NEUTRAL, C.SUBLANG_DEFAULT), &msgbuf, + 0, C.NULL) err_msg := if res == 0 { - 'unknown error $err_msg_id' + 'Win-API error $err_msg_id' } else { - string{ - str: msgbuf - len: int(res) - } + unsafe { string_from_wide(msgbuf) } } return err_msg }