将具有联合字段的 C 结构映射到 Go 结构

Map C struct with union field to Go struct

我正在从 Go 中的某些 WinApi 的系统调用中获取结果。我很容易从 C 代码映射简单的结构,但是如何处理像下面这样的 C 结构?

typedef struct SPC_LINK_
{
    DWORD dwLinkChoice;
#               define          SPC_URL_LINK_CHOICE         1
#               define          SPC_MONIKER_LINK_CHOICE     2
#               define          SPC_FILE_LINK_CHOICE        3

    union
    {
        LPWSTR                  pwszUrl;
        SPC_SERIALIZED_OBJECT   Moniker;
        LPWSTR                  pwszFile;
    };

} SPC_LINK, *PSPC_LINK;

如果 Go 中定义了所有可能的类型

type SPC_LINK struct {
    dwLinkChoice  DWORD
    Moniker       SPC_SERIALIZED_OBJECT
    pwszFile      LPWSTR
    pwszUrl       LPWSTR
}

在以 unsafe.Pointer 作为参数对这个 Go 结构进行系统调用之后,我将它保存在内存中并且可以像往常一样在 Go 中访问它,但只有 dwLinkChoice 之后的第一个字段(上面代码中的 Moniker) 总是填充的,其他两个总是空的。我知道这是 C 中的预期行为,因为您一次只能有一个联合字段。考虑到这一点,我是否应该忽略整个联合结构并在我的 Go 结构中有某种占位符?

type SPC_LINK struct {
    dwLinkChoice  DWORD
    dwLink        uintptr // a placeholder, will hold any possible value
}

我将占位符的类型设置为 uintptr,但是如果原始 C 结构在联合块中有一些其他非指针类型怎么办?我真的不确定如何处理 C 联合并寻找任何建议。

cgo documentation

中所述

As Go doesn't have support for C's union type in the general case, C's union types are represented as a Go byte array with the same length.

也许你应该试试这个

type SPC_LINK struct {
    dwLinkChoice  DWORD
    dwLink        [{size of the union}]byte
}