From d949424c56303171c8d68b6058b479cf3f8c2d89 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Fri, 31 May 2024 11:52:03 -0700 Subject: [PATCH] windows: use original ToUnicodeEx signature and update tests --- windows/syscall_windows.go | 11 +---------- windows/syscall_windows_test.go | 14 +++++++++++--- windows/zsyscall_windows.go | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 47701eed..e26da2bb 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -216,7 +216,7 @@ func NewCallbackCDecl(fn interface{}) uintptr { //sys LoadKeyboardLayout(name *uint16, flags uint32) (hkl Handle) = user32.LoadKeyboardLayoutW //sys UnloadKeyboardLayout(hkl Handle) (v bool) = user32.UnloadKeyboardLayout //sys GetKeyboardLayout(tid uint32) (hkl Handle) = user32.GetKeyboardLayout -//sys toUnicodeEx(vkey uint32, scancode uint32, keystate *byte, pwszBuff *uint16, cchBuff int32, flags uint32, hkl Handle) (ret int32) = user32.ToUnicodeEx +//sys ToUnicodeEx(vkey uint32, scancode uint32, keystate *byte, pwszBuff *uint16, cchBuff int32, flags uint32, hkl Handle) (ret int32) = user32.ToUnicodeEx //sys GetShellWindow() (shellWindow HWND) = user32.GetShellWindow //sys MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW //sys ExitWindowsEx(flags uint32, reason uint32) (err error) = user32.ExitWindowsEx @@ -1924,12 +1924,3 @@ const ( EV_ERR = 0x0080 EV_RING = 0x0100 ) - -// ToUnicodeEx Translates the specified virtual-key code and keyboard state to -// the corresponding Unicode character or characters. -func ToUnicodeEx(virtualKey, scanCode uint32, keyState [256]byte, buf []uint16, flags uint32, layout Handle) int32 { - if len(buf) == 0 { - return 0 - } - return toUnicodeEx(virtualKey, scanCode, &keyState[0], &buf[0], int32(len(buf)), flags, layout) -} diff --git a/windows/syscall_windows_test.go b/windows/syscall_windows_test.go index e9c4de02..be60a5a4 100644 --- a/windows/syscall_windows_test.go +++ b/windows/syscall_windows_test.go @@ -1289,13 +1289,18 @@ func TestGetKeyboardLayout(t *testing.T) { func TestToUnicodeEx(t *testing.T) { var utf16Buf [16]uint16 - const araLayout = windows.Handle(0x401) + ara, err := windows.UTF16PtrFromString("00000401") // ara layout 0x401 + if err != nil { + t.Fatalf("UTF16PtrFromString failed: %v", err) + } + araLayout := windows.LoadKeyboardLayout(ara, 0) var keyState [256]byte ret := windows.ToUnicodeEx( 0x41, // 'A' vkCode 0x1e, // 'A' scanCode - keyState, - utf16Buf[:], + &keyState[0], + &utf16Buf[0], + int32(len(utf16Buf)), 0x4, // don't change keyboard state araLayout, ) @@ -1306,4 +1311,7 @@ func TestToUnicodeEx(t *testing.T) { if utf16Buf[0] != 'ش' { t.Errorf("ToUnicodeEx failed, wanted 'ش', got %q", utf16Buf[0]) } + if !windows.UnloadKeyboardLayout(araLayout) { + t.Errorf("UnloadKeyboardLayout failed") + } } diff --git a/windows/zsyscall_windows.go b/windows/zsyscall_windows.go index 794f8a99..38cf9de2 100644 --- a/windows/zsyscall_windows.go +++ b/windows/zsyscall_windows.go @@ -4131,7 +4131,7 @@ func MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret i return } -func toUnicodeEx(vkey uint32, scancode uint32, keystate *byte, pwszBuff *uint16, cchBuff int32, flags uint32, hkl Handle) (ret int32) { +func ToUnicodeEx(vkey uint32, scancode uint32, keystate *byte, pwszBuff *uint16, cchBuff int32, flags uint32, hkl Handle) (ret int32) { r0, _, _ := syscall.Syscall9(procToUnicodeEx.Addr(), 7, uintptr(vkey), uintptr(scancode), uintptr(unsafe.Pointer(keystate)), uintptr(unsafe.Pointer(pwszBuff)), uintptr(cchBuff), uintptr(flags), uintptr(hkl), 0, 0) ret = int32(r0) return