From cdf2335f5a8e19d88893b28aac5351ac662bbbc9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 23 Jan 2022 18:26:29 +0900 Subject: [PATCH] refactoring: better error handlings on Windows --- .../opengl/gl/procaddr_windows.go | 9 ++++++++- internal/uidriver/glfw/hideconsole_windows.go | 18 ++++++++++-------- internal/uidriver/glfw/ui_windows.go | 16 ++++++++-------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/internal/graphicsdriver/opengl/gl/procaddr_windows.go b/internal/graphicsdriver/opengl/gl/procaddr_windows.go index dea4dc2da..06440ddba 100644 --- a/internal/graphicsdriver/opengl/gl/procaddr_windows.go +++ b/internal/graphicsdriver/opengl/gl/procaddr_windows.go @@ -3,6 +3,7 @@ package gl import ( + "fmt" "unsafe" "golang.org/x/sys/windows" @@ -18,9 +19,15 @@ func getProcAddress(namea string) uintptr { if err != nil { panic(err) } - if r, _, _ := wglGetProcAddress.Call(uintptr(unsafe.Pointer(cname))); r != 0 { + + r, _, err := wglGetProcAddress.Call(uintptr(unsafe.Pointer(cname))) + if err != nil && err != windows.ERROR_SUCCESS && err != windows.ERROR_PROC_NOT_FOUND { + panic(fmt.Sprintf("gl: wglGetProcAddress failed: %s", err.Error())) + } + if r != 0 { return r } + p := opengl32.NewProc(namea) if err := p.Find(); err != nil { // The proc is not found. diff --git a/internal/uidriver/glfw/hideconsole_windows.go b/internal/uidriver/glfw/hideconsole_windows.go index c82cd70aa..c50196da0 100644 --- a/internal/uidriver/glfw/hideconsole_windows.go +++ b/internal/uidriver/glfw/hideconsole_windows.go @@ -37,8 +37,8 @@ var ( func getCurrentProcessId() (uint32, error) { r, _, e := getCurrentProcessIdProc.Call() - if e != nil && e.(windows.Errno) != 0 { - return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %w", e) } return uint32(r), nil } @@ -46,24 +46,26 @@ func getCurrentProcessId() (uint32, error) { func getWindowThreadProcessId(hwnd uintptr) (uint32, error) { pid := uint32(0) r, _, e := getWindowThreadProcessIdProc.Call(hwnd, uintptr(unsafe.Pointer(&pid))) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: GetWindowThreadProcessId failed: %w", e) + } if r == 0 { - return 0, fmt.Errorf("ui: GetWindowThreadProcessId failed: %d", e) + return 0, fmt.Errorf("ui: GetWindowThreadProcessId returned 0") } return pid, nil } func getConsoleWindow() (uintptr, error) { r, _, e := getConsoleWindowProc.Call() - if e != nil && e.(windows.Errno) != 0 { - return 0, fmt.Errorf("ui: GetConsoleWindow failed: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: GetConsoleWindow failed: %w", e) } return r, nil } func freeConsole() error { - _, _, e := freeConsoleWindowProc.Call() - if e != nil && e.(windows.Errno) != 0 { - return fmt.Errorf("ui: FreeConsole failed: %d", e) + if _, _, e := freeConsoleWindowProc.Call(); e != nil && e != windows.ERROR_SUCCESS { + return fmt.Errorf("ui: FreeConsole failed: %w", e) } return nil } diff --git a/internal/uidriver/glfw/ui_windows.go b/internal/uidriver/glfw/ui_windows.go index 522beec7b..d313f6f67 100644 --- a/internal/uidriver/glfw/ui_windows.go +++ b/internal/uidriver/glfw/ui_windows.go @@ -54,24 +54,24 @@ var ( func getSystemMetrics(nIndex int) (int, error) { r, _, e := procGetSystemMetrics.Call(uintptr(nIndex)) - if e != nil && e.(windows.Errno) != 0 { - return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %w", e) } return int(r), nil } func getForegroundWindow() (uintptr, error) { r, _, e := procGetForegroundWindow.Call() - if e != nil && e.(windows.Errno) != 0 { - return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %w", e) } return r, nil } func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) { r, _, e := procMonitorFromWindow.Call(hwnd, uintptr(dwFlags)) - if e != nil && e.(windows.Errno) != 0 { - return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %w", e) } if r == 0 { return 0, fmt.Errorf("ui: MonitorFromWindow failed: returned value: %d", r) @@ -81,8 +81,8 @@ func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) { func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi))) - if e != nil && e.(windows.Errno) != 0 { - return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %d", e) + if e != nil && e != windows.ERROR_SUCCESS { + return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e) } if r == 0 { return fmt.Errorf("ui: GetMonitorInfoW failed: returned value: %d", r)