在Golang中的Windows回调函数

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

Windows callback function in Golang

问题

我想在Golang中向Windows事件日志进行推送订阅。我应该如何传递回调函数?

EVT_SUBSCRIBE_CALLBACK是函数的指针,类似于:

typedef DWORD ( WINAPI *EVT_SUBSCRIBE_CALLBACK)(
   EVT_SUBSCRIBE_NOTIFY_ACTION Action,
   PVOID                       UserContext,
   EVT_HANDLE                  Event
);

所以,我的变量看起来像这样:

func logCallback() syscall.Handle {

	cb := func(_ uintptr, _ uintptr, _ uintptr) uint64 {
		fmt.Printf("callback called %v", data)
		return 0
	}
	ptr := syscall.NewCallback(cb)
	return syscall.Handle(ptr) // 类型为 syscall.Handle uintptr
}

我成功获取了订阅处理程序,没有错误,但它仍然不起作用。有任何想法为什么吗?我应该首先查看哪里?

英文:

I wanna make push subscription to Windows Event Log in Golang
How exactly should I pass a callback function?

EVT_SUBSCRIBE_CALLBACK is the pointer of function, like

typedef DWORD ( WINAPI *EVT_SUBSCRIBE_CALLBACK)(
   EVT_SUBSCRIBE_NOTIFY_ACTION Action,
   PVOID                       UserContext,
   EVT_HANDLE                  Event
);

So, my variant looks like this:

func logCallback() syscall.Handle {

	cb := func(_ uintptr, _ uintptr, _ uintptr) uint64 {
		fmt.Printf("callback called %v", data)
		return 0
	}
	ptr := syscall.NewCallback(cb)
	return syscall.Handle(ptr) // type syscall.Handle uintptr
}

I get successfully subscribed handler with no errors, but it still doesn't work.
Any ideas why? Where should I look first?

答案1

得分: 3

当使用系统调用时,请确保在文件的顶部包含import "C"。很高兴能帮到你。

英文:

When using syscall make sure the to include import "C" at the top of your file. Glad it helped you.

huangapple
  • 本文由 发表于 2017年7月7日 18:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/44968397.html
匿名

发表评论

匿名网友

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

确定