Go win.RegisterRarWinPutDevices始终返回false
我正在使用包访问Go中的低级Windows调用。我正在调用Go win.RegisterRarWinPutDevices始终返回false,go,unsafe-pointers,Go,Unsafe Pointers,我正在使用包访问Go中的低级Windows调用。我正在调用win.registerarWinPutDevices为原始输入数据注册设备,但它总是返回false。我用C#做过,没有问题。下面是我的代码: package main import ( "fmt" "syscall" "unsafe" "github.com/lxn/win" ) func WndProc(hWnd win.HWND, msg uint32, wParam, lParam uintpt
win.registerarWinPutDevices
为原始输入数据注册设备,但它总是返回false。我用C#做过,没有问题。下面是我的代码:
package main
import (
"fmt"
"syscall"
"unsafe"
"github.com/lxn/win"
)
func WndProc(hWnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_CREATE:
fmt.Println("Received WM_CREATE message")
devices := getRawInputDevices(hWnd)
len := uint32(len(devices))
size := uint32(unsafe.Sizeof(devices[0]))
if !win.RegisterRawInputDevices(&devices[0], len, size) {
panic("Unable to register devices")
}
case win.WM_DESTROY:
fmt.Println("Posting quit message")
win.PostQuitMessage(0)
default:
return win.DefWindowProc(hWnd, msg, wParam, lParam)
}
return 0
}
func getRawInputDevices(hWnd win.HWND) []win.RAWINPUTDEVICE {
devices := make([]win.RAWINPUTDEVICE, 5)
devices[0].UsUsagePage = 0x01
devices[0].UsUsage = 0x02
devices[0].DwFlags = win.RIDEV_INPUTSINK
devices[0].HwndTarget = hWnd
devices[1].UsUsagePage = 0x01
devices[1].UsUsage = 0x06
devices[1].DwFlags = win.RIDEV_INPUTSINK
devices[1].HwndTarget = hWnd
devices[2].UsUsagePage = 0x00
devices[2].UsUsage = 0x51
devices[2].DwFlags = win.RIDEV_INPUTSINK
devices[2].HwndTarget = hWnd
devices[3].UsUsagePage = 0x00
devices[3].UsUsage = 0x04
devices[3].DwFlags = win.RIDEV_INPUTSINK
devices[3].HwndTarget = hWnd
devices[4].UsUsagePage = 0x00
devices[4].UsUsage = 0x00
devices[4].DwFlags = win.RIDEV_INPUTSINK
devices[4].HwndTarget = hWnd
return devices
}
func WinMain() int {
hInstance := win.GetModuleHandle(syscall.StringToUTF16Ptr(""))
lpszClassName := syscall.StringToUTF16Ptr("WNDclass")
var wcex win.WNDCLASSEX
wcex.HInstance = hInstance
wcex.LpszClassName = lpszClassName
wcex.LpfnWndProc = syscall.NewCallback(WndProc)
wcex.CbSize = uint32(unsafe.Sizeof(wcex))
win.RegisterClassEx(&wcex)
win.CreateWindowEx(
0, lpszClassName, syscall.StringToUTF16Ptr("Simple Go Window!"),
win.WS_OVERLAPPEDWINDOW,
win.CW_USEDEFAULT, win.CW_USEDEFAULT, 400, 400, 0, 0, hInstance, nil)
var msg win.MSG
for {
if win.GetMessage(&msg, 0, 0, 0) == 0 {
break
}
win.TranslateMessage(&msg)
win.DispatchMessage(&msg)
}
return int(msg.WParam)
}
func main() {
WinMain()
return
}
我收到了
WM_CREATE
消息,但是win.RegisterRarWinPutDevices
总是返回false和“参数不正确”错误。正如我所提到的,我在C#中完成了这项工作,没有任何问题,因此我熟悉基本步骤。我是新来的,所以我可能错过了什么。有什么想法吗?经过几个小时研究Go的结构对齐策略和其他各种低级内容,我终于意识到我对RAWINPUTDEVICE.usususSagePage的值不正确!元素2、3和4应该将ususussagepage
设置为0x0D
,而不是0x00
!哦,好吧,至少我学到了很多关于Go如何将结构元素与单词边界对齐的知识