From a3a7512489bda7a4ba633b5163a9dba6d4998ee1 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 11 Jul 2022 17:36:06 +0100 Subject: [PATCH] Fix #30. --- internal/win/kernel32.go | 2 ++ internal/win/user32.go | 35 +++++++++-------------- internal/win/zsyscall_windows.go | 48 ++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/internal/win/kernel32.go b/internal/win/kernel32.go index 7f44fe7..edf07a9 100644 --- a/internal/win/kernel32.go +++ b/internal/win/kernel32.go @@ -44,4 +44,6 @@ func GetSystemDirectory() (string, error) { return windows.GetSystemDirectory() //sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupId int) (err error) = kernel32.GenerateConsoleCtrlEvent //sys GetConsoleWindow() (ret HWND) = kernel32.GetConsoleWindow //sys GetModuleHandle(moduleName *uint16) (ret Handle, err error) = kernel32.GetModuleHandleW +//sys GlobalAlloc(flags uint32, bytes uintptr) (ret Handle, err error) = kernel32.GlobalAlloc +//sys GlobalFree(mem Handle) (err error) [failretval!=0] = kernel32.GlobalFree //sys ReleaseActCtx(actCtx Handle) = kernel32.ReleaseActCtx diff --git a/internal/win/user32.go b/internal/win/user32.go index bf52b0a..a09a7e2 100644 --- a/internal/win/user32.go +++ b/internal/win/user32.go @@ -281,30 +281,21 @@ func SendMessagePointer(wnd HWND, msg uint32, wparam uintptr, lparam unsafe.Poin return } -func GetDpiForWindow(wnd HWND) (ret int, err error) { - if err := procGetDpiForWindow.Find(); err != nil { - return 0, err - } - return getDpiForWindow(wnd), nil -} - -func SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) { - if err := procSetThreadDpiAwarenessContext.Find(); err != nil { - return 0, err - } - return setThreadDpiAwarenessContext(dpiContext), nil -} - // https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues func MessageLoop(wnd HWND) error { + msg, err := GlobalAlloc(0, unsafe.Sizeof(MSG{})) + if err != nil { + return err + } + defer GlobalFree(msg) + getMessage := procGetMessageW.Addr() translateMessage := procTranslateMessage.Addr() dispatchMessage := procDispatchMessageW.Addr() isDialogMessage := procIsDialogMessageW.Addr() for { - var msg MSG - s, _, err := syscall.Syscall6(getMessage, 4, uintptr(unsafe.Pointer(&msg)), 0, 0, 0, 0, 0) + s, _, err := syscall.Syscall6(getMessage, 4, uintptr(msg), 0, 0, 0, 0, 0) if int32(s) == -1 { return err } @@ -312,10 +303,10 @@ func MessageLoop(wnd HWND) error { return nil } - s, _, _ = syscall.Syscall(isDialogMessage, 2, uintptr(wnd), uintptr(unsafe.Pointer(&msg)), 0) + s, _, _ = syscall.Syscall(isDialogMessage, 2, uintptr(wnd), uintptr(msg), 0) if s == 0 { - syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) - syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0) + syscall.Syscall(translateMessage, 1, uintptr(msg), 0, 0) + syscall.Syscall(dispatchMessage, 1, uintptr(msg), 0, 0) } } } @@ -398,8 +389,8 @@ type CWPRETSTRUCT struct { //sys EnableWindow(wnd HWND, enable bool) (ok bool) = user32.EnableWindow //sys EnumWindows(enumFunc uintptr, lparam unsafe.Pointer) (err error) = user32.EnumChildWindows //sys GetDlgItem(dlg HWND, dlgItemID int) (ret HWND, err error) = user32.GetDlgItem -//sys getDpiForWindow(wnd HWND) (ret int) = user32.GetDpiForWindow -//sys GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr) = user32.GetMessageW +//sys GetDpiForWindow(wnd HWND) (ret int, err error) [false] = user32.GetDpiForWindow? +//sys GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr, err error) [int32(failretval)==-1] = user32.GetMessageW //sys GetSystemMetrics(index int) (ret int) = user32.GetSystemMetrics //sys GetWindowDC(wnd HWND) (ret Handle) = user32.GetWindowDC //sys GetWindowRect(wnd HWND, cmdShow *RECT) (err error) = user32.GetWindowRect @@ -415,7 +406,7 @@ type CWPRETSTRUCT struct { //sys SetDlgItemText(dlg HWND, dlgItemID int, str *uint16) (err error) = user32.SetDlgItemTextW //sys SetFocus(wnd HWND) (ret HWND, err error) = user32.SetFocus //sys SetForegroundWindow(wnd HWND) (ok bool) = user32.SetForegroundWindow -//sys setThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr) = user32.SetThreadDpiAwarenessContext +//sys SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) [false] = user32.SetThreadDpiAwarenessContext? //sys SetWindowLong(wnd HWND, index int, newLong int) (ret int, err error) = user32.SetWindowLongW //sys SetWindowPos(wnd HWND, wndInsertAfter HWND, x int, y int, cx int, cy int, flags int) (err error) = user32.SetWindowPos //sys SetWindowsHookEx(idHook int, fn uintptr, mod Handle, threadID uint32) (ret Handle, err error) = user32.SetWindowsHookExW diff --git a/internal/win/zsyscall_windows.go b/internal/win/zsyscall_windows.go index d28d2ae..6a8fb87 100644 --- a/internal/win/zsyscall_windows.go +++ b/internal/win/zsyscall_windows.go @@ -61,6 +61,8 @@ var ( procGenerateConsoleCtrlEvent = modkernel32.NewProc("GenerateConsoleCtrlEvent") procGetConsoleWindow = modkernel32.NewProc("GetConsoleWindow") procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW") + procGlobalAlloc = modkernel32.NewProc("GlobalAlloc") + procGlobalFree = modkernel32.NewProc("GlobalFree") procReleaseActCtx = modkernel32.NewProc("ReleaseActCtx") procCoCreateInstance = modole32.NewProc("CoCreateInstance") procSHBrowseForFolder = modshell32.NewProc("SHBrowseForFolder") @@ -203,6 +205,23 @@ func GetModuleHandle(moduleName *uint16) (ret Handle, err error) { return } +func GlobalAlloc(flags uint32, bytes uintptr) (ret Handle, err error) { + r0, _, e1 := syscall.Syscall(procGlobalAlloc.Addr(), 2, uintptr(flags), uintptr(bytes), 0) + ret = Handle(r0) + if ret == 0 { + err = errnoErr(e1) + } + return +} + +func GlobalFree(mem Handle) (err error) { + r1, _, e1 := syscall.Syscall(procGlobalFree.Addr(), 1, uintptr(mem), 0, 0) + if r1 != 0 { + err = errnoErr(e1) + } + return +} + func ReleaseActCtx(actCtx Handle) { syscall.Syscall(procReleaseActCtx.Addr(), 1, uintptr(actCtx), 0, 0) return @@ -332,15 +351,25 @@ func GetDlgItem(dlg HWND, dlgItemID int) (ret HWND, err error) { return } -func getDpiForWindow(wnd HWND) (ret int) { - r0, _, _ := syscall.Syscall(procGetDpiForWindow.Addr(), 1, uintptr(wnd), 0, 0) +func GetDpiForWindow(wnd HWND) (ret int, err error) { + err = procGetDpiForWindow.Find() + if err != nil { + return + } + r0, _, e1 := syscall.Syscall(procGetDpiForWindow.Addr(), 1, uintptr(wnd), 0, 0) ret = int(r0) + if false { + err = errnoErr(e1) + } return } -func GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr) { - r0, _, _ := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(wnd), uintptr(msgFilterMin), uintptr(msgFilterMax), 0, 0) +func GetMessage(msg *MSG, wnd HWND, msgFilterMin uint32, msgFilterMax uint32) (ret uintptr, err error) { + r0, _, e1 := syscall.Syscall6(procGetMessageW.Addr(), 4, uintptr(unsafe.Pointer(msg)), uintptr(wnd), uintptr(msgFilterMin), uintptr(msgFilterMax), 0, 0) ret = uintptr(r0) + if int32(ret) == -1 { + err = errnoErr(e1) + } return } @@ -454,9 +483,16 @@ func SetForegroundWindow(wnd HWND) (ok bool) { return } -func setThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr) { - r0, _, _ := syscall.Syscall(procSetThreadDpiAwarenessContext.Addr(), 1, uintptr(dpiContext), 0, 0) +func SetThreadDpiAwarenessContext(dpiContext uintptr) (ret uintptr, err error) { + err = procSetThreadDpiAwarenessContext.Find() + if err != nil { + return + } + r0, _, e1 := syscall.Syscall(procSetThreadDpiAwarenessContext.Addr(), 1, uintptr(dpiContext), 0, 0) ret = uintptr(r0) + if false { + err = errnoErr(e1) + } return }