Search code examples
structgounioncgo

How to access a variable in an union in a struct from the Windows API?


I got input.ki undefined (type C.INPUT has no field or method ki).

I tried using the 'union_' prefix but without any luck.

Any ideas?

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)
}

Solution

  • Because unions break type safety, the only way to access them in Go is with the unsafe package. I think you could do it something like this:

    *(*C.KEYBDINPUT)(unsafe.Pointer(uintptr(unsafe.Pointer(&input)) + unsafe.Sizeof(C.DWORD))) = keybdinput
    

    If I needed to deal with these types much, I would declare wrapper types to make it easier:

    type tagKbdInput struct {
        typ uint32
        ki  C.KEYBDINPUT
    }
    
    type tagMouseInput struct {
        typ uint32
        mi  C.MOUSEINPUT
    }
    
    type tagHardwareInput struct {
        typ uint32
        hi  C.HARDWAREINPUT
    }
    

    Then I could use a simpler conversion through unsafe.Pointer (without pointer arithmetic) to access them:

    (*tagKbdInput)(unsafe.Pointer(&input)).ki = keybdinput