From ad5ebae3ccd9f072fab976b32fa0626463e900af Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 22 May 2022 21:50:38 +0900 Subject: [PATCH] internal/ui: refactoring --- internal/ui/hideconsole_windows.go | 34 ++++++++++-------------------- internal/ui/ui_glfw_windows.go | 3 ++- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/internal/ui/hideconsole_windows.go b/internal/ui/hideconsole_windows.go index 0169542fd..bdcaa71d3 100644 --- a/internal/ui/hideconsole_windows.go +++ b/internal/ui/hideconsole_windows.go @@ -19,23 +19,15 @@ package ui import ( "fmt" - "unsafe" "golang.org/x/sys/windows" ) -const ( - processQueryLimitedInformation = 0x1000 -) - var ( kernel32 = windows.NewLazySystemDLL("kernel32.dll") - user32 = windows.NewLazySystemDLL("user32.dll") - procFreeConsoleWindow = kernel32.NewProc("FreeConsole") - procGetCurrentProcessId = kernel32.NewProc("GetCurrentProcessId") - procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow") - procGetWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId") + procFreeConsoleWindow = kernel32.NewProc("FreeConsole") + procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow") ) func freeConsole() error { @@ -49,28 +41,24 @@ func freeConsole() error { return nil } -func getCurrentProcessId() uint32 { - r, _, _ := procGetCurrentProcessId.Call() - return uint32(r) -} - func getConsoleWindow() windows.HWND { r, _, _ := procGetConsoleWindow.Call() return windows.HWND(r) } -func getWindowThreadProcessId(hwnd windows.HWND) (tid, pid uint32) { - r, _, _ := procGetWindowThreadProcessId.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&pid))) - tid = uint32(r) - return -} - // hideConsoleWindowOnWindows will hide the console window that is showing when // compiling on Windows without specifying the '-ldflags "-Hwindowsgui"' flag. func hideConsoleWindowOnWindows() { - pid := getCurrentProcessId() + pid := windows.GetCurrentProcessId() + // Get the process ID of the console's creator. - _, cpid := getWindowThreadProcessId(getConsoleWindow()) + var cpid uint32 + if _, err := windows.GetWindowThreadProcessId(getConsoleWindow(), &cpid); err != nil { + // Even if closing the console fails, this is not harmful. + // Ignore error. + return + } + if pid == cpid { // The current process created its own console. Hide this. // Ignore error. diff --git a/internal/ui/ui_glfw_windows.go b/internal/ui/ui_glfw_windows.go index 9119de9d2..b52e41753 100644 --- a/internal/ui/ui_glfw_windows.go +++ b/internal/ui/ui_glfw_windows.go @@ -87,7 +87,8 @@ type point struct { } var ( - // user32 is defined at hideconsole_windows.go + user32 = windows.NewLazySystemDLL("user32.dll") + procGetSystemMetrics = user32.NewProc("GetSystemMetrics") procMonitorFromWindow = user32.NewProc("MonitorFromWindow") procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")