调用Windows系统的Go系统调用

huangapple go评论80阅读模式
英文:

Go syscall call windows

问题

我目前正在尝试在Go语言中使用user32.dll的EnumWindows函数,但似乎无法正常工作。

var (
    user32 = syscall.NewLazyDLL("user32.dll")
    procEnumWindows = user32.NewProc("EnumWindows")
)

func EnumWindows() int {
    ret, _, _ := procEnumWindows.Call(
        syscall.NewCallback(enumWindowsProc),
        uintptr(0),
    )
    return int(ret)
}

func enumWindowsProc(hwnd syscall.Handle, lparam uintptr) bool {
    return true
}

调用EnumWindows函数会出现以下错误:

panic: compileCallback: output parameter size is wrong

我不确定应该如何使用syscall包... 我似乎找不到足够的文档。

在MSDN文档页面上,它说回调函数应该返回一个BOOL,而这正是我正在做的吗?

英文:

Im currently trying to use user32.dll EnumWindows on Go but seems to not be working

var(
	user32 = syscall.NewLazyDLL("user32.dll")
	procEnumWindows = user32.NewProc("EnumWindows")
)

func EnumWindows() int {
	ret, _, _ := procEnumWindows.Call(
		syscall.NewCallback(enumWindowsProc),
		uintptr(0),
	)
	return int(ret)
}

func enumWindowsProc(hwnd syscall.Handle, lparam uintptr) bool {
	return true
}

Calling EnumWindows will give the following error:

panic: compileCallback: output parameter size is wrong

Im not sure how should I use the syscall package... I cant seem to find enough documentation on it

On the MSDN doc page it says that the callback should return a BOOL and thats what I am doing?

答案1

得分: 3

BOOL在WinAPI中被声明为typedef int BOOL。所以它与Go语言的bool类型不匹配。规范甚至没有提到它的大小是多少。它可能是1字节,但没有明确说明。你应该使用int32代替。

英文:

BOOL in WinAPI is declared as typedef int BOOL. So it doesn't match Go's bool. Specifications doesn't even mention what's the size it has. It's probably 1 byte but it doesn't say it. You should use int32 instead.

huangapple
  • 本文由 发表于 2015年12月19日 23:58:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/34372601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定