From 3274dd7bdbbf4552434d674fabb660dcab7fb925 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 12 Jul 2022 00:09:50 +0100 Subject: [PATCH] Review windows API. --- internal/win/kernel32.go | 18 +++++++++--------- internal/win/shell32.go | 2 +- internal/win/zsyscall_windows.go | 11 ++++------- notify_windows.go | 8 ++------ 4 files changed, 16 insertions(+), 23 deletions(-) diff --git a/internal/win/kernel32.go b/internal/win/kernel32.go index edf07a9..f495f2d 100644 --- a/internal/win/kernel32.go +++ b/internal/win/kernel32.go @@ -38,12 +38,12 @@ type ACTCTX struct { func GetCurrentThreadId() (id uint32) { return windows.GetCurrentThreadId() } func GetSystemDirectory() (string, error) { return windows.GetSystemDirectory() } -//sys ActivateActCtx(actCtx Handle, cookie *uintptr) (err error) = kernel32.ActivateActCtx -//sys CreateActCtx(actCtx *ACTCTX) (ret Handle, err error) = kernel32.CreateActCtxW -//sys DeactivateActCtx(flags uint32, cookie uintptr) (err error) = kernel32.DeactivateActCtx -//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 +//sys ActivateActCtx(actCtx Handle, cookie *uintptr) (err error) +//sys CreateActCtx(actCtx *ACTCTX) (ret Handle, err error) [failretval==^Handle(0)] = CreateActCtxW +//sys DeactivateActCtx(flags uint32, cookie uintptr) (err error) +//sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupId int) (err error) +//sys GetConsoleWindow() (ret HWND) +//sys GetModuleHandle(moduleName *uint16) (ret Handle, err error) = GetModuleHandleW +//sys GlobalAlloc(flags uint32, bytes uintptr) (ret Handle, err error) +//sys GlobalFree(mem Handle) (err error) [failretval!=0] +//sys ReleaseActCtx(actCtx Handle) diff --git a/internal/win/shell32.go b/internal/win/shell32.go index 0a05e2f..9f0e79e 100644 --- a/internal/win/shell32.go +++ b/internal/win/shell32.go @@ -286,5 +286,5 @@ func (u *IShellItemArray) GetItemAt(index uint32) (item *IShellItem, err error) //sys SHBrowseForFolder(bi *BROWSEINFO) (ret *IDLIST) = shell32.SHBrowseForFolder //sys SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid uintptr, item **IShellItem) (res error) = shell32.SHCreateItemFromParsingName -//sys ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ret int, err error) = shell32.Shell_NotifyIconW +//sys ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ok bool) = shell32.Shell_NotifyIconW //sys SHGetPathFromIDListEx(ptr *IDLIST, path *uint16, pathLen int, opts int) (ok bool) = shell32.SHGetPathFromIDListEx diff --git a/internal/win/zsyscall_windows.go b/internal/win/zsyscall_windows.go index 6a8fb87..c360f94 100644 --- a/internal/win/zsyscall_windows.go +++ b/internal/win/zsyscall_windows.go @@ -168,7 +168,7 @@ func ActivateActCtx(actCtx Handle, cookie *uintptr) (err error) { func CreateActCtx(actCtx *ACTCTX) (ret Handle, err error) { r0, _, e1 := syscall.Syscall(procCreateActCtxW.Addr(), 1, uintptr(unsafe.Pointer(actCtx)), 0, 0) ret = Handle(r0) - if ret == 0 { + if ret == ^Handle(0) { err = errnoErr(e1) } return @@ -255,12 +255,9 @@ func SHGetPathFromIDListEx(ptr *IDLIST, path *uint16, pathLen int, opts int) (ok return } -func ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ret int, err error) { - r0, _, e1 := syscall.Syscall(procShell_NotifyIconW.Addr(), 2, uintptr(message), uintptr(unsafe.Pointer(data)), 0) - ret = int(r0) - if ret == 0 { - err = errnoErr(e1) - } +func ShellNotifyIcon(message uint32, data *NOTIFYICONDATA) (ok bool) { + r0, _, _ := syscall.Syscall(procShell_NotifyIconW.Addr(), 2, uintptr(message), uintptr(unsafe.Pointer(data)), 0) + ok = r0 != 0 return } diff --git a/notify_windows.go b/notify_windows.go index 3251727..4b8a3e7 100644 --- a/notify_windows.go +++ b/notify_windows.go @@ -50,12 +50,8 @@ func notify(text string, opts options) error { runtime.LockOSThread() defer runtime.UnlockOSThread() - s, err := win.ShellNotifyIcon(win.NIM_ADD, &args) - if s == 0 { - if errno, ok := err.(syscall.Errno); ok && errno == 0 { - return wtsMessage(text, opts) - } - return err + if !win.ShellNotifyIcon(win.NIM_ADD, &args) { + return wtsMessage(text, opts) } major, minor, _ := win.RtlGetNtVersionNumbers()