我得到了input.ki undefined (type C.INPUT has no field or method ki)。我尝试使用 'union_' 前缀,但没有任何运气。有任何想法吗?package main// #include <windows.h>// #include <winuser.h>import "C"// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx// typedef struct tagINPUT {// DWORD type;// union {// MOUSEINPUT mi;// KEYBDINPUT ki;// HARDWAREINPUT hi;// };// } INPUT, *PINPUT;func main() { var input C.INPUT var keybdinput C.KEYBDINPUT input._type = 1 // ok! // input.ki = keybdinput // input.ki undefined (type C.INPUT has no field or method ki) // input.union_ki = keybdinput // input.union_ki undefined (type C.INPUT has no field or method union_ki)}
2 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
因为联合破坏了类型安全,在 Go 中访问它们的唯一方法是使用 unsafe 包。我认为你可以这样做:
*(*C.KEYBDINPUT)(unsafe.Pointer(uintptr(unsafe.Pointer(&input)) + unsafe.Sizeof(C.DWORD))) = keybdinput
如果我需要大量处理这些类型,我会声明包装器类型以使其更容易:
type tagKbdInput struct {
typ uint32
ki C.KEYBDINPUT
}
type tagMouseInput struct {
typ uint32
mi C.MOUSEINPUT
}
type tagHardwareInput struct {
typ uint32
hi C.HARDWAREINPUT
}
然后我可以通过 unsafe.Pointer (没有指针算法)使用更简单的转换来访问它们:
(*tagKbdInput)(unsafe.Pointer(&input)).ki = keybdinput
- 2 回答
- 0 关注
- 159 浏览
添加回答
举报
0/150
提交
取消