refactoring: better error handlings on Windows

This commit is contained in:
Hajime Hoshi 2022-01-23 18:26:29 +09:00
parent a049c403cf
commit cdf2335f5a
3 changed files with 26 additions and 17 deletions

View File

@ -3,6 +3,7 @@
package gl package gl
import ( import (
"fmt"
"unsafe" "unsafe"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -18,9 +19,15 @@ func getProcAddress(namea string) uintptr {
if err != nil { if err != nil {
panic(err) 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 return r
} }
p := opengl32.NewProc(namea) p := opengl32.NewProc(namea)
if err := p.Find(); err != nil { if err := p.Find(); err != nil {
// The proc is not found. // The proc is not found.

View File

@ -37,8 +37,8 @@ var (
func getCurrentProcessId() (uint32, error) { func getCurrentProcessId() (uint32, error) {
r, _, e := getCurrentProcessIdProc.Call() r, _, e := getCurrentProcessIdProc.Call()
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %d", e) return 0, fmt.Errorf("ui: GetCurrentProcessId failed: %w", e)
} }
return uint32(r), nil return uint32(r), nil
} }
@ -46,24 +46,26 @@ func getCurrentProcessId() (uint32, error) {
func getWindowThreadProcessId(hwnd uintptr) (uint32, error) { func getWindowThreadProcessId(hwnd uintptr) (uint32, error) {
pid := uint32(0) pid := uint32(0)
r, _, e := getWindowThreadProcessIdProc.Call(hwnd, uintptr(unsafe.Pointer(&pid))) 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 { if r == 0 {
return 0, fmt.Errorf("ui: GetWindowThreadProcessId failed: %d", e) return 0, fmt.Errorf("ui: GetWindowThreadProcessId returned 0")
} }
return pid, nil return pid, nil
} }
func getConsoleWindow() (uintptr, error) { func getConsoleWindow() (uintptr, error) {
r, _, e := getConsoleWindowProc.Call() r, _, e := getConsoleWindowProc.Call()
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetConsoleWindow failed: %d", e) return 0, fmt.Errorf("ui: GetConsoleWindow failed: %w", e)
} }
return r, nil return r, nil
} }
func freeConsole() error { func freeConsole() error {
_, _, e := freeConsoleWindowProc.Call() if _, _, e := freeConsoleWindowProc.Call(); e != nil && e != windows.ERROR_SUCCESS {
if e != nil && e.(windows.Errno) != 0 { return fmt.Errorf("ui: FreeConsole failed: %w", e)
return fmt.Errorf("ui: FreeConsole failed: %d", e)
} }
return nil return nil
} }

View File

@ -54,24 +54,24 @@ var (
func getSystemMetrics(nIndex int) (int, error) { func getSystemMetrics(nIndex int) (int, error) {
r, _, e := procGetSystemMetrics.Call(uintptr(nIndex)) r, _, e := procGetSystemMetrics.Call(uintptr(nIndex))
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %d", e) return 0, fmt.Errorf("ui: GetSystemMetrics failed: error code: %w", e)
} }
return int(r), nil return int(r), nil
} }
func getForegroundWindow() (uintptr, error) { func getForegroundWindow() (uintptr, error) {
r, _, e := procGetForegroundWindow.Call() r, _, e := procGetForegroundWindow.Call()
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %d", e) return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %w", e)
} }
return r, nil return r, nil
} }
func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) { func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) {
r, _, e := procMonitorFromWindow.Call(hwnd, uintptr(dwFlags)) r, _, e := procMonitorFromWindow.Call(hwnd, uintptr(dwFlags))
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %d", e) return 0, fmt.Errorf("ui: MonitorFromWindow failed: error code: %w", e)
} }
if r == 0 { if r == 0 {
return 0, fmt.Errorf("ui: MonitorFromWindow failed: returned value: %d", r) 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 { func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi))) r, _, e := procGetMonitorInfoW.Call(hMonitor, uintptr(unsafe.Pointer(lpmi)))
if e != nil && e.(windows.Errno) != 0 { if e != nil && e != windows.ERROR_SUCCESS {
return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %d", e) return fmt.Errorf("ui: GetMonitorInfoW failed: error code: %w", e)
} }
if r == 0 { if r == 0 {
return fmt.Errorf("ui: GetMonitorInfoW failed: returned value: %d", r) return fmt.Errorf("ui: GetMonitorInfoW failed: returned value: %d", r)