Please help me with structure data alignment

50 views
Skip to first unread message

Maxim Velivanov

unread,
Nov 24, 2022, 12:08:54 AM11/24/22
to golang-nuts
Hi,

I'm developing a Go wrapper for a dll.
dll contains the following structure:
struct ck_version {
  unsigned char major;
  unsigned char minor;
};

struct ck_info {
  struct ck_version cryptoki_version;
  unsigned char manufacturer_id[32];
  ck_flags_t flags;
  unsigned char library_description[32];
  struct ck_version library_version;
};

in Go there is its analogue:
type Ck_version struct {
    Major uint8
    Minor uint8
}

type Ck_info struct {
    Cryptoki_version    Ck_version
    Manufacturer_id     [32]byte
    Flags               uint32
    Library_description [32]byte
    Library_version     Ck_version
}

when i call the function
    info := Ck_info{}
    rv, _, _ := c_GetInfo.Call(uintptr(unsafe.Pointer(&info)))

then I get the data offset starting from the flags field.

I understand that Go has different data alignment in structures than C compiler. But how then does a wrapper with tons of structures work in this project golang.org/x/sys/windows? Maybe I missed something?

Maxim Velivanov

unread,
Nov 24, 2022, 4:09:02 AM11/24/22
to golang-nuts
My bad. it turns out that the API dll has a directive
#pragma pack (1)

Reply all
Reply to author
Forward
0 new messages