From 5348ab66b34f9e088a9fdfbd39f71bba6223e7aa Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Wed, 2 Nov 2022 10:21:31 +0000 Subject: [PATCH] Fixes (windows). --- file_windows.go | 4 ++-- internal/win/comdlg32.go | 2 +- internal/win/kernel32.go | 14 ++++++++------ internal/win/ole32.go | 6 +++--- internal/win/shell32.go | 19 +++++++++---------- internal/win/user32.go | 5 ++++- internal/win/win32.go | 9 ++++++--- internal/win/zsyscall_windows.go | 17 ++++------------- 8 files changed, 37 insertions(+), 39 deletions(-) diff --git a/file_windows.go b/file_windows.go index 9123564..4dc2a2c 100644 --- a/file_windows.go +++ b/file_windows.go @@ -327,10 +327,10 @@ func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) { copy(name, syscall.StringToUTF16(n)) } if d != "" { - dir = strptr(d) + dir = syscall.StringToUTF16Ptr(d) } if len(e) > 1 { - ext = strptr(e[1:]) + ext = syscall.StringToUTF16Ptr(e[1:]) } return } diff --git a/internal/win/comdlg32.go b/internal/win/comdlg32.go index fb1cf53..f8d5578 100644 --- a/internal/win/comdlg32.go +++ b/internal/win/comdlg32.go @@ -71,7 +71,7 @@ func CommDlgError() error { if code := commDlgExtendedError(); code == 0 { return zenutil.ErrCanceled } else { - return fmt.Errorf("Common Dialog error: %x", code) + return fmt.Errorf("common dialog error: %x", code) } } diff --git a/internal/win/kernel32.go b/internal/win/kernel32.go index f495f2d..04005ed 100644 --- a/internal/win/kernel32.go +++ b/internal/win/kernel32.go @@ -15,11 +15,11 @@ const ( ACTCTX_FLAG_HMODULE_VALID = 0x080 // Control signals - CTRL_C_EVENT = 0 - CTRL_BREAK_EVENT = 1 - CTRL_CLOSE_EVENT = 2 - CTRL_LOGOFF_EVENT = 5 - CTRL_SHUTDOWN_EVENT = 6 + CTRL_C_EVENT = windows.CTRL_C_EVENT + CTRL_BREAK_EVENT = windows.CTRL_BREAK_EVENT + CTRL_CLOSE_EVENT = windows.CTRL_CLOSE_EVENT + CTRL_LOGOFF_EVENT = windows.CTRL_LOGOFF_EVENT + CTRL_SHUTDOWN_EVENT = windows.CTRL_SHUTDOWN_EVENT ) // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-actctxw @@ -37,11 +37,13 @@ type ACTCTX struct { func GetCurrentThreadId() (id uint32) { return windows.GetCurrentThreadId() } func GetSystemDirectory() (string, error) { return windows.GetSystemDirectory() } +func GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupID uint32) (err error) { + return windows.GenerateConsoleCtrlEvent(ctrlEvent, processGroupID) +} //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) diff --git a/internal/win/ole32.go b/internal/win/ole32.go index 5850a10..6f48e37 100644 --- a/internal/win/ole32.go +++ b/internal/win/ole32.go @@ -62,8 +62,8 @@ func unkcall(self, trap uintptr, a ...uintptr) (r1, r2 uintptr, lastErr error) { type IBindCtx struct{ IUnknown } -//sys CoCreateInstance(clsid uintptr, unkOuter *IUnknown, clsContext int32, iid uintptr, address unsafe.Pointer) (res error) = ole32.CoCreateInstance +//sys CoCreateInstance(clsid *GUID, unkOuter *IUnknown, clsContext int32, iid *GUID, address unsafe.Pointer) (res error) = ole32.CoCreateInstance -func uuid(s string) uintptr { - return (*reflect.StringHeader)(unsafe.Pointer(&s)).Data +func guid(s string) *GUID { + return (*GUID)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data)) } diff --git a/internal/win/shell32.go b/internal/win/shell32.go index be88997..ccfa0f0 100644 --- a/internal/win/shell32.go +++ b/internal/win/shell32.go @@ -3,9 +3,10 @@ package win import ( - "reflect" "syscall" "unsafe" + + "golang.org/x/sys/windows" ) const ( @@ -120,9 +121,9 @@ type IDLIST struct{} // https://github.com/wine-mirror/wine/blob/master/include/shobjidl.idl var ( - IID_IShellItem = uuid("\x1e\x6d\x82\x43\x18\xe7\xee\x42\xbc\x55\xa1\xe2\x61\xc3\x7b\xfe") - IID_IFileOpenDialog = uuid("\x88\x72\x7c\xd5\xad\xd4\x68\x47\xbe\x02\x9d\x96\x95\x32\xd9\x60") - CLSID_FileOpenDialog = uuid("\x9c\x5a\x1c\xdc\x8a\xe8\xde\x4d\xa5\xa1\x60\xf8\x2a\x20\xae\xf7") + IID_IShellItem = guid("\x1e\x6d\x82\x43\x18\xe7\xee\x42\xbc\x55\xa1\xe2\x61\xc3\x7b\xfe") + IID_IFileOpenDialog = guid("\x88\x72\x7c\xd5\xad\xd4\x68\x47\xbe\x02\x9d\x96\x95\x32\xd9\x60") + CLSID_FileOpenDialog = guid("\x9c\x5a\x1c\xdc\x8a\xe8\xde\x4d\xa5\xa1\x60\xf8\x2a\x20\xae\xf7") ) type IFileOpenDialog struct{ IFileDialog } @@ -240,16 +241,14 @@ type iShellItemVtbl struct { } func (u *IShellItem) GetDisplayName(name int) (res string, err error) { - var ptr uintptr + var ptr *uint16 vtbl := *(**iShellItemVtbl)(unsafe.Pointer(u)) hr, _, _ := u.call(vtbl.GetDisplayName, uintptr(name), uintptr(unsafe.Pointer(&ptr))) if hr != 0 { err = syscall.Errno(hr) } else { - var buf []uint16 - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) - hdr.Data, hdr.Len, hdr.Cap = uintptr(ptr), 32768, 32768 - res = syscall.UTF16ToString(buf) + res = windows.UTF16PtrToString(ptr) + CoTaskMemFree(unsafe.Pointer(ptr)) } return } @@ -286,6 +285,6 @@ func (u *IShellItemArray) GetItemAt(index uint32) (item *IShellItem, err error) //sys ExtractAssociatedIcon(instance Handle, path *uint16, icon *uint16) (ret Handle, err error) = shell32.ExtractAssociatedIconW //sys SHBrowseForFolder(bi *BROWSEINFO) (ret *IDLIST) = shell32.SHBrowseForFolder -//sys SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid uintptr, item **IShellItem) (res error) = shell32.SHCreateItemFromParsingName +//sys SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid *GUID, item **IShellItem) (res error) = shell32.SHCreateItemFromParsingName //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/user32.go b/internal/win/user32.go index 3957784..bfd1369 100644 --- a/internal/win/user32.go +++ b/internal/win/user32.go @@ -265,9 +265,12 @@ func GetWindowThreadProcessId(hwnd HWND, pid *uint32) (tid uint32, err error) { func GetWindowText(wnd HWND) string { len, _ := getWindowTextLength(wnd) + if len == 0 { + return "" + } buf := make([]uint16, len+1) getWindowText(wnd, &buf[0], len+1) - return syscall.UTF16ToString(buf) + return windows.UTF16ToString(buf) } func SendMessagePointer(wnd HWND, msg uint32, wparam uintptr, lparam unsafe.Pointer) (ret uintptr) { diff --git a/internal/win/win32.go b/internal/win/win32.go index b026bd4..35aa2be 100644 --- a/internal/win/win32.go +++ b/internal/win/win32.go @@ -5,9 +5,12 @@ package win import "golang.org/x/sys/windows" -type Handle = windows.Handle -type HWND = windows.HWND -type Pointer = windows.Pointer +type ( + Handle = windows.Handle + HWND = windows.HWND + Pointer = windows.Pointer + GUID = windows.GUID +) // https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime type SYSTEMTIME struct { diff --git a/internal/win/zsyscall_windows.go b/internal/win/zsyscall_windows.go index 2716faa..d5f6d8e 100644 --- a/internal/win/zsyscall_windows.go +++ b/internal/win/zsyscall_windows.go @@ -58,7 +58,6 @@ var ( procActivateActCtx = modkernel32.NewProc("ActivateActCtx") procCreateActCtxW = modkernel32.NewProc("CreateActCtxW") procDeactivateActCtx = modkernel32.NewProc("DeactivateActCtx") - procGenerateConsoleCtrlEvent = modkernel32.NewProc("GenerateConsoleCtrlEvent") procGetConsoleWindow = modkernel32.NewProc("GetConsoleWindow") procGetModuleHandleW = modkernel32.NewProc("GetModuleHandleW") procGlobalAlloc = modkernel32.NewProc("GlobalAlloc") @@ -182,14 +181,6 @@ func DeactivateActCtx(flags uint32, cookie uintptr) (err error) { return } -func GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupId int) (err error) { - r1, _, e1 := syscall.Syscall(procGenerateConsoleCtrlEvent.Addr(), 2, uintptr(ctrlEvent), uintptr(processGroupId), 0) - if r1 == 0 { - err = errnoErr(e1) - } - return -} - func GetConsoleWindow() (ret HWND) { r0, _, _ := syscall.Syscall(procGetConsoleWindow.Addr(), 0, 0, 0, 0) ret = HWND(r0) @@ -227,8 +218,8 @@ func ReleaseActCtx(actCtx Handle) { return } -func CoCreateInstance(clsid uintptr, unkOuter *IUnknown, clsContext int32, iid uintptr, address unsafe.Pointer) (res error) { - r0, _, _ := syscall.Syscall6(procCoCreateInstance.Addr(), 5, uintptr(clsid), uintptr(unsafe.Pointer(unkOuter)), uintptr(clsContext), uintptr(iid), uintptr(address), 0) +func CoCreateInstance(clsid *GUID, unkOuter *IUnknown, clsContext int32, iid *GUID, address unsafe.Pointer) (res error) { + r0, _, _ := syscall.Syscall6(procCoCreateInstance.Addr(), 5, uintptr(unsafe.Pointer(clsid)), uintptr(unsafe.Pointer(unkOuter)), uintptr(clsContext), uintptr(unsafe.Pointer(iid)), uintptr(address), 0) if r0 != 0 { res = syscall.Errno(r0) } @@ -250,8 +241,8 @@ func SHBrowseForFolder(bi *BROWSEINFO) (ret *IDLIST) { return } -func SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid uintptr, item **IShellItem) (res error) { - r0, _, _ := syscall.Syscall6(procSHCreateItemFromParsingName.Addr(), 4, uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(bc)), uintptr(iid), uintptr(unsafe.Pointer(item)), 0, 0) +func SHCreateItemFromParsingName(path *uint16, bc *IBindCtx, iid *GUID, item **IShellItem) (res error) { + r0, _, _ := syscall.Syscall6(procSHCreateItemFromParsingName.Addr(), 4, uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(bc)), uintptr(unsafe.Pointer(iid)), uintptr(unsafe.Pointer(item)), 0, 0) if r0 != 0 { res = syscall.Errno(r0) }