Refactor, styling improvements.

This commit is contained in:
Nuno Cruces 2021-03-29 19:07:44 +01:00
parent 22038b61ff
commit 1bb9ad397f
5 changed files with 149 additions and 251 deletions

View file

@ -266,6 +266,7 @@ func loadFlags() []zenity.Option {
setDefault(&okLabel, "Yes")
setDefault(&cancelLabel, "No")
case passwordDlg:
setDefault(&title, "Type your password")
setDefault(&icon, "dialog-password")
setDefault(&okLabel, "OK")
setDefault(&cancelLabel, "Cancel")

View file

@ -61,6 +61,6 @@ func entry(text string, opts options) (string, bool, error) {
func password(opts options) (string, string, bool, error) {
opts.hideText = true
pass, ok, err := entry("Type your password", opts)
pass, ok, err := entry("Password:", opts)
return "", pass, ok, err
}

View file

@ -14,7 +14,7 @@ func entry(text string, opts options) (string, bool, error) {
if opts.title != nil {
title = *opts.title
}
return editBox(title, text, opts.entryText, "ClassEntry", false)
return editBox(title, text, opts.entryText, "ClassEntry", opts.hideText)
}
func password(opts options) (string, string, bool, error) {
@ -22,89 +22,35 @@ func password(opts options) (string, string, bool, error) {
if opts.title != nil {
title = *opts.title
}
pass, ok, err := editBox(title, "Type your password", "", "ClassPassword", true)
pass, ok, err := editBox(title, "Password:", "", "ClassPassword", true)
return "", pass, ok, err
}
var (
gdi32 = syscall.NewLazyDLL("gdi32.dll")
createWindowEx = user32.NewProc("CreateWindowExW")
defWindowProc = user32.NewProc("DefWindowProcW")
destroyWindow = user32.NewProc("DestroyWindow")
dispatchMessage = user32.NewProc("DispatchMessageW")
postQuitMessage = user32.NewProc("PostQuitMessage")
registerClassEx = user32.NewProc("RegisterClassExW")
unregisterClassW = user32.NewProc("UnregisterClassW")
translateMessage = user32.NewProc("TranslateMessage")
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
getWindowTextW = user32.NewProc("GetWindowTextW")
getWindowRect = user32.NewProc("GetWindowRect")
setWindowPos = user32.NewProc("SetWindowPos")
showWindow = user32.NewProc("ShowWindow")
isDialogMessage = user32.NewProc("IsDialogMessageW")
getSystemMetricsW = user32.NewProc("GetSystemMetrics")
systemParametersInfo = user32.NewProc("SystemParametersInfoW")
getWindowDC = user32.NewProc("GetWindowDC")
releaseDC = user32.NewProc("ReleaseDC")
getDpiForWindow = user32.NewProc("GetDpiForWindow")
setFocus = user32.NewProc("SetFocus")
createWindowEx = user32.NewProc("CreateWindowExW")
defWindowProcW = user32.NewProc("DefWindowProcW")
destroyWindowW = user32.NewProc("DestroyWindow")
dispatchMessage = user32.NewProc("DispatchMessageW")
postQuitMessageW = user32.NewProc("PostQuitMessage")
registerClassExW = user32.NewProc("RegisterClassExW")
unregisterClassW = user32.NewProc("UnregisterClassW")
translateMessage = user32.NewProc("TranslateMessage")
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
getWindowTextW = user32.NewProc("GetWindowTextW")
getWindowLongW = user32.NewProc("GetWindowLongW")
setWindowLongW = user32.NewProc("SetWindowLongW")
getWindowRectW = user32.NewProc("GetWindowRect")
setWindowPosW = user32.NewProc("SetWindowPos")
showWindowW = user32.NewProc("ShowWindow")
updateWindowW = user32.NewProc("UpdateWindow")
isDialogMessage = user32.NewProc("IsDialogMessageW")
getSystemMetricsW = user32.NewProc("GetSystemMetrics")
systemParametersInfoW = user32.NewProc("SystemParametersInfoW")
getWindowDC = user32.NewProc("GetWindowDC")
releaseDC = user32.NewProc("ReleaseDC")
getDpiForWindow = user32.NewProc("GetDpiForWindow")
getDeviceCaps = gdi32.NewProc("GetDeviceCaps")
createFontIndirectW = gdi32.NewProc("CreateFontIndirectW")
getModuleHandleW = kernel32.NewProc("GetModuleHandleW")
)
const (
swShow = 5
swShowNormal = 1
swUseDefault = 0x80000000
swpNoZOrder = 0x0004
swpNoSize = 0x0001
smCxScreen = 0
smCyScreen = 1
wsThickFrame = 0x00040000
wsSysMenu = 0x00080000
wsBorder = 0x00800000
wsCaption = 0x00C00000
wsChild = 0x40000000
wsVisible = 0x10000000
wsMaximizeBox = 0x00010000
wsMinimizeBox = 0x00020000
wsTabStop = 0x00010000
wsGroup = 0x00020000
wsOverlappedWindow = 0x00CF0000
wsExClientEdge = 0x00000200
wmCreate = 0x0001
wmDestroy = 0x0002
wmClose = 0x0010
wmCommand = 0x0111
wmSetFont = 0x0030
wmKeydown = 0x0100
wmInitDialog = 0x0110
esPassword = 0x0020
esAutoVScroll = 0x0040
esAutoHScroll = 0x0080
dtmFirst = 0x1000
dtmGetSystemTime = dtmFirst + 1
dtmSetSystemTime = dtmFirst + 2
vkEscape = 0x1B
enUpdate = 0x0400
bsPushButton = 0
colorWindow = 5
spiGetNonClientMetrics = 0x0029
gwlStyle = -16
maxPath = 260
deleteObject = gdi32.NewProc("DeleteObject")
getDeviceCaps = gdi32.NewProc("GetDeviceCaps")
createFontIndirect = gdi32.NewProc("CreateFontIndirectW")
)
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexw
@ -183,175 +129,103 @@ type _RECT struct {
bottom int32
}
func getModuleHandle() (syscall.Handle, error) {
ret, _, err := getModuleHandleW.Call(uintptr(0))
if ret == 0 {
return 0, err
}
return syscall.Handle(ret), nil
}
type dpi uintptr
func (d dpi) Scale(dim uintptr) uintptr {
if d == 0 {
return dim
}
return dim * uintptr(d) / 96 // USER_DEFAULT_SCREEN_DPI
}
func getDPI(wnd uintptr) (dpi, error) {
var res uintptr = 96
func getDPI(wnd uintptr) dpi {
var res uintptr
if wnd != 0 && getDpiForWindow.Find() == nil {
res, _, _ = getDpiForWindow.Call(wnd)
} else {
dc, _, err := getWindowDC.Call(wnd)
if dc == 0 {
return 0, err
}
} else if dc, _, _ := getWindowDC.Call(wnd); dc != 0 {
defer releaseDC.Call(0, dc)
res, _, _ = getDeviceCaps.Call(dc, 90) // LOGPIXELSY
}
return dpi(res), nil
if res == 0 {
return 96 // USER_DEFAULT_SCREEN_DPI
}
return dpi(res)
}
func createWindow(exStyle uint64, className, windowName string, style, x, y, width, height uintptr,
parent, menu, instance syscall.Handle) (syscall.Handle, error) {
func createWindow(exStyle uint64, className, windowName string, style, x, y, width, height,
parent, menu, instance uintptr) (uintptr, error) {
ret, _, err := createWindowEx.Call(uintptr(exStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), uintptr(style), x, y,
width, height, uintptr(parent), uintptr(menu), uintptr(instance), uintptr(0))
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), style, x, y,
width, height, parent, menu, instance, 0)
if ret == 0 {
return 0, err
}
return syscall.Handle(ret), nil
return ret, nil
}
func destroyWindow(hwnd syscall.Handle) error {
ret, _, err := destroyWindowW.Call(uintptr(hwnd))
if ret == 0 {
return err
}
return nil
}
func defWindowProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
ret, _, _ := defWindowProcW.Call(uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam))
return uintptr(ret)
}
func registerClassEx(wcx *_WNDCLASSEX) (uint16, error) {
ret, _, err := registerClassExW.Call(uintptr(unsafe.Pointer(wcx)))
if ret == 0 {
return 0, err
}
return uint16(ret), nil
}
func unregisterClass(className string, instance syscall.Handle) bool {
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), uintptr(instance))
func unregisterClass(className string, instance uintptr) bool {
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), instance)
return ret != 0
}
func postQuitMessage(exitCode int32) {
postQuitMessageW.Call(uintptr(exitCode))
}
func getWindowTextLength(hwnd syscall.Handle) int {
ret, _, _ := getWindowTextLengthW.Call(uintptr(hwnd))
func getWindowTextLength(hwnd uintptr) int {
ret, _, _ := getWindowTextLengthW.Call(hwnd)
return int(ret)
}
func getWindowText(hwnd syscall.Handle) string {
func getWindowText(hwnd uintptr) string {
textLen := getWindowTextLength(hwnd) + 1
buf := make([]uint16, textLen)
getWindowTextW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&buf[0])), uintptr(textLen))
getWindowTextW.Call(hwnd, uintptr(unsafe.Pointer(&buf[0])), uintptr(textLen))
return syscall.UTF16ToString(buf)
}
func systemParametersInfo(uiAction, uiParam uint32, pvParam unsafe.Pointer, fWinIni uint32) bool {
ret, _, _ := systemParametersInfoW.Call(uintptr(uiAction), uintptr(uiParam), uintptr(pvParam), uintptr(fWinIni), 0, 0)
return int32(ret) != 0
}
func createFontIndirect(lplf *_LOGFONT) uintptr {
ret, _, _ := createFontIndirectW.Call(uintptr(unsafe.Pointer(lplf)), 0, 0)
return uintptr(ret)
}
func getWindowLong(hwnd syscall.Handle, index int32) int32 {
ret, _, _ := getWindowLongW.Call(uintptr(hwnd), uintptr(index), 0)
return int32(ret)
}
func setWindowLong(hwnd syscall.Handle, index, value int32) int32 {
ret, _, _ := setWindowLongW.Call(uintptr(hwnd), uintptr(index), uintptr(value))
return int32(ret)
}
func getWindowRect(hwnd syscall.Handle, rect *_RECT) bool {
ret, _, _ := getWindowRectW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(rect)), 0)
return ret != 0
}
func setWindowPos(hwnd, hwndInsertAfter syscall.Handle, x, y, width, height int32, flags uint32) bool {
ret, _, _ := setWindowPosW.Call(uintptr(hwnd), uintptr(hwndInsertAfter),
uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(flags), 0, 0)
return ret != 0
}
func showWindow(hwnd syscall.Handle, nCmdShow int32) bool {
ret, _, _ := showWindowW.Call(uintptr(hwnd), uintptr(nCmdShow), 0)
return ret != 0
}
func updateWindow(hwnd syscall.Handle) bool {
ret, _, _ := updateWindowW.Call(uintptr(hwnd), 0, 0)
return ret != 0
}
func getSystemMetrics(nindex int32) int32 {
ret, _, _ := getSystemMetricsW.Call(uintptr(nindex), 0, 0)
return int32(ret)
}
func centerWindow(hwnd syscall.Handle) {
var rc _RECT
getWindowRect(hwnd, &rc)
xPos := (getSystemMetrics(smCxScreen) - (rc.right - rc.left)) / 2
yPos := (getSystemMetrics(smCyScreen) - (rc.bottom - rc.top)) / 2
setWindowPos(hwnd, 0, xPos, yPos, 0, 0, swpNoZOrder|swpNoSize)
func centerWindow(wnd uintptr) {
var rect _RECT
getWindowRect.Call(wnd, uintptr(unsafe.Pointer(&rect)))
x := (getSystemMetrics(0 /* SM_CXSCREEN */) - (rect.right - rect.left)) / 2
y := (getSystemMetrics(1 /* SM_CYSCREEN */) - (rect.bottom - rect.top)) / 2
setWindowPos.Call(wnd, 0, uintptr(x), uintptr(y), 0, 0, 0x5) // SWP_NOZORDER|SWP_NOSIZE
}
func getMessageFont() uintptr {
var metrics _NONCLIENTMETRICS
metrics.Size = uint32(unsafe.Sizeof(metrics))
systemParametersInfo(spiGetNonClientMetrics, uint32(unsafe.Sizeof(metrics)), unsafe.Pointer(&metrics), 0)
return createFontIndirect(&metrics.MessageFont)
systemParametersInfo.Call(0x29, /* SPI_GETNONCLIENTMETRICS */
unsafe.Sizeof(metrics), uintptr(unsafe.Pointer(&metrics)), 0)
ret, _, _ := createFontIndirect.Call(uintptr(unsafe.Pointer(&metrics.MessageFont)))
return ret
}
func registerClass(className string, instance syscall.Handle, fn interface{}) error {
func registerClass(className string, instance uintptr, proc interface{}) error {
var wcx _WNDCLASSEX
wcx.Size = uint32(unsafe.Sizeof(wcx))
wcx.WndProc = syscall.NewCallback(fn)
wcx.Instance = uintptr(instance)
wcx.Background = colorWindow + 1
wcx.WndProc = syscall.NewCallback(proc)
wcx.Instance = instance
wcx.Background = 5 // COLOR_WINDOW
wcx.ClassName = syscall.StringToUTF16Ptr(className)
_, err := registerClassEx(&wcx)
return err
ret, _, err := registerClassEx.Call(uintptr(unsafe.Pointer(&wcx)))
if ret == 0 {
return err
}
return nil
}
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
func messageLoop(hwnd uintptr) error {
func messageLoop(wnd uintptr) error {
getMessage := getMessage.Addr()
isDialogMessage := isDialogMessage.Addr()
translateMessage := translateMessage.Addr()
@ -367,7 +241,7 @@ func messageLoop(hwnd uintptr) error {
return nil
}
ret, _, _ = syscall.Syscall(isDialogMessage, 2, hwnd, uintptr(unsafe.Pointer(&msg)), 0)
ret, _, _ = syscall.Syscall(isDialogMessage, 2, wnd, uintptr(unsafe.Pointer(&msg)), 0)
if ret == 0 {
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
@ -376,40 +250,52 @@ func messageLoop(hwnd uintptr) error {
}
// editBox displays textedit/inputbox dialog.
func editBox(title, text, defaultText, className string, password bool) (string, bool, error) {
var out string
notCancledOrClosed := true
func editBox(title, text, defaultText, className string, password bool) (out string, ok bool, err error) {
var wnd, textCtl, editCtl uintptr
var okBtn, cancelBtn, extraBtn uintptr
defWindowProc := defWindowProc.Addr()
var hwndEdit syscall.Handle
layout := func(dpi dpi) {
setWindowPos.Call(wnd, 0, 0, 0, dpi.Scale(281), dpi.Scale(140), 0x6) // SWP_NOZORDER|SWP_NOMOVE
setWindowPos.Call(textCtl, 0, dpi.Scale(12), dpi.Scale(10), dpi.Scale(241), dpi.Scale(16), 0x4) // SWP_NOZORDER
setWindowPos.Call(editCtl, 0, dpi.Scale(12), dpi.Scale(30), dpi.Scale(241), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(okBtn, 0, dpi.Scale(12), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(cancelBtn, 0, dpi.Scale(95), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
setWindowPos.Call(extraBtn, 0, dpi.Scale(178), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
instance, err := getModuleHandle()
if err != nil {
return out, false, err
font := getMessageFont()
sendMessage.Call(textCtl, 0x0030 /* WM_SETFONT */, font, 0)
sendMessage.Call(editCtl, 0x0030 /* WM_SETFONT */, font, 0)
sendMessage.Call(okBtn, 0x0030 /* WM_SETFONT */, font, 0)
sendMessage.Call(cancelBtn, 0x0030 /* WM_SETFONT */, font, 0)
sendMessage.Call(extraBtn, 0x0030 /* WM_SETFONT */, font, 0)
}
fn := func(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
proc := func(wnd uintptr, msg uint32, wparam, lparam uintptr) uintptr {
switch msg {
case wmClose:
notCancledOrClosed = false
destroyWindow(hwnd)
case wmDestroy:
postQuitMessage(0)
case wmKeydown:
if wparam == vkEscape {
notCancledOrClosed = false
destroyWindow(hwnd)
case 0x0002: // WM_DESTROY
postQuitMessage.Call(0)
case 0x0010: // WM_CLOSE
destroyWindow.Call(wnd)
case 0x0111: // WM_COMMAND
switch wparam {
default:
return 1
case 1, 6: // IDOK, IDYES
out = getWindowText(editCtl)
ok = true
case 2: // IDCANCEL
case 7: // IDNO
}
case wmCommand:
if wparam == 100 {
out = getWindowText(hwndEdit)
destroyWindow(hwnd)
} else if wparam == 110 {
notCancledOrClosed = false
destroyWindow(hwnd)
}
case 0x2e0: // WM_DPICHANGED
destroyWindow.Call(wnd)
case 0x02e0: // WM_DPICHANGED
layout(dpi(uint32(wparam) >> 16))
default:
ret := defWindowProc(hwnd, msg, wparam, lparam)
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
return ret
}
@ -418,43 +304,50 @@ func editBox(title, text, defaultText, className string, password bool) (string,
defer setup()()
err = registerClass(className, instance, fn)
instance, _, err := getModuleHandle.Call(0)
if instance == 0 {
return "", false, err
}
err = registerClass(className, instance, proc)
if err != nil {
return out, false, err
return "", false, err
}
defer unregisterClass(className, instance)
dpi, err := getDPI(0)
hwnd, _ := createWindow(0, className, title, wsOverlappedWindow, swUseDefault, swUseDefault, dpi.Scale(235), dpi.Scale(140), 0, 0, instance)
hwndText, _ := createWindow(0, "STATIC", text, wsChild|wsVisible, dpi.Scale(10), dpi.Scale(10), dpi.Scale(200), dpi.Scale(16), hwnd, 0, instance)
dpi := getDPI(0)
flags := wsBorder | wsChild | wsVisible | wsGroup | wsTabStop | esAutoHScroll
wnd, _ = createWindow(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME
className, title, 0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME
0x80000000 /* CW_USEDEFAULT */, 0x80000000, /* CW_USEDEFAULT */
dpi.Scale(281), dpi.Scale(140), 0, 0, instance)
textCtl, _ = createWindow(0, "STATIC", text, 0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX
dpi.Scale(12), dpi.Scale(10), dpi.Scale(241), dpi.Scale(16), wnd, 0, instance)
var flags uintptr = 0x50030080 // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|ES_AUTOHSCROLL
if password {
flags |= esPassword
flags |= 0x20 // ES_PASSWORD
}
hwndEdit, _ = createWindow(wsExClientEdge, "EDIT", defaultText, uintptr(flags), dpi.Scale(10), dpi.Scale(30), dpi.Scale(200), dpi.Scale(24), hwnd, 0, instance)
editCtl, _ = createWindow(0x200, // WS_EX_CLIENTEDGE
"EDIT", defaultText, flags,
dpi.Scale(12), dpi.Scale(30), dpi.Scale(241), dpi.Scale(24), wnd, 0, instance)
hwndOK, _ := createWindow(wsExClientEdge, "BUTTON", "OK", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, dpi.Scale(10), dpi.Scale(65), dpi.Scale(90), dpi.Scale(24), hwnd, 100, instance)
hwndCancel, _ := createWindow(wsExClientEdge, "BUTTON", "Cancel", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, dpi.Scale(120), dpi.Scale(65), dpi.Scale(90), dpi.Scale(24), hwnd, 110, instance)
okBtn, _ = createWindow(0, "BUTTON", "OK", 0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
dpi.Scale(12), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 1 /* IDOK */, instance)
cancelBtn, _ = createWindow(0, "BUTTON", "Cancel", 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
dpi.Scale(95), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 2 /* IDCANCEL */, instance)
extraBtn, _ = createWindow(0, "BUTTON", "Extra", 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
dpi.Scale(178), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 7 /* IDNO */, instance)
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMinimizeBox)
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMaximizeBox)
layout(getDPI(wnd))
centerWindow(wnd)
setFocus.Call(editCtl)
showWindow.Call(wnd, 1 /* SW_SHOWNORMAL */, 0)
font := getMessageFont()
sendMessage.Call(uintptr(hwndText), wmSetFont, font, 0)
sendMessage.Call(uintptr(hwndEdit), wmSetFont, font, 0)
sendMessage.Call(uintptr(hwndOK), wmSetFont, font, 0)
sendMessage.Call(uintptr(hwndCancel), wmSetFont, font, 0)
centerWindow(hwnd)
showWindow(hwnd, swShowNormal)
updateWindow(hwnd)
err = messageLoop(uintptr(hwnd))
err = messageLoop(wnd)
if err != nil {
return out, false, err
return "", false, err
}
return out, notCancledOrClosed, nil
return out, ok, nil
}

View file

@ -56,7 +56,6 @@ func message(kind messageKind, text string, opts options) (bool, error) {
title = syscall.StringToUTF16Ptr(*opts.title)
}
activate()
s, _, err := messageBox.Call(0,
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
uintptr(unsafe.Pointer(title)), flags)

View file

@ -13,6 +13,7 @@ import (
var (
comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
gdi32 = syscall.NewLazyDLL("gdi32.dll")
kernel32 = syscall.NewLazyDLL("kernel32.dll")
ole32 = syscall.NewLazyDLL("ole32.dll")
shell32 = syscall.NewLazyDLL("shell32.dll")
@ -21,6 +22,7 @@ var (
commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError")
getModuleHandle = kernel32.NewProc("GetModuleHandleW")
getCurrentThreadId = kernel32.NewProc("GetCurrentThreadId")
getConsoleWindow = kernel32.NewProc("GetConsoleWindow")
@ -109,9 +111,10 @@ func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook conte
}
var hook, wnd uintptr
callNextHookEx := callNextHookEx.Addr()
tid, _, _ := getCurrentThreadId.Call()
hook, _, err = setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET
syscall.NewCallback(func(code int, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
syscall.NewCallback(func(code int32, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
if lparam.Message == 0x0110 { // WM_INITDIALOG
name := [8]uint16{}
getClassName.Call(lparam.Wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
@ -131,7 +134,9 @@ func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook conte
}
}
}
next, _, _ := callNextHookEx.Call(hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)))
next, _, _ := syscall.Syscall6(callNextHookEx, 4,
hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)),
0, 0)
return next
}), 0, tid)