* xjdrew <xj.d...@gmail.com> [170523 02:31]: > Marvin, thanks. > > What i'm doing is similar to your guessing. > > I use syscall.Syscall to call win32 api, and encounter some padding issues. > I have resolve my problems by mannual padding days ago, but I want to know > if there is a more graceful way to finish that. > > > One of the Apis is: > > DWORD WINAPI FwpmFilterAdd0( > _In_ HANDLE engineHandle, > _In_ const FWPM_FILTER0 *filter, > _In_opt_ SECURITY_DESCRIPTOR sd, > _Out_opt_ UINT64 *id > ); > > > I have call with a FWPM_FILTER0 struct as below: > > typedef struct FWPM_FILTER0_ { > GUID filterKey; > FWPM_DISPLAY_DATA0 displayData; > UINT32 flags; > GUID *providerKey; > FWP_BYTE_BLOB providerData; > GUID layerKey; > GUID subLayerKey; > FWP_VALUE0 weight; > UINT32 numFilterConditions; > FWPM_FILTER_CONDITION0 *filterCondition; > FWPM_ACTION0 action; > union { UINT64 rawContext; // makes this union aligns to > 8 bytes > GUID providerContextKey; > }; > GUID *reserved; > UINT64 filterId; // filterId aligns to 8 bytes > FWP_VALUE0 effectiveWeight; > } FWPM_FILTER0; > > > In win32, I have to define the sturct as below: > type FWPM_FILTER0 struct { > FilterKey GUID > DisplayData FWPM_DISPLAY_DATA0 > Flags uint32 > ProviderKey *GUID > ProviderData FWP_BYTE_BLOB > LayerKey GUID > SubLayerKey GUID > Weight FWP_VALUE0 > NumFilterConditions uint32 > FilterCondition *FWPM_FILTER_CONDITION0 > Action FWPM_ACTION0 > /* > union { > UINT64 rawContext; > GUID providerContextKey; > }; > */ > reserved1 uint32 // mannul padding > ProviderContextKey GUID // the max size field of that anonymous union > Reserved *GUID > reserved2 uint32 // mannul padding > FilterId uint64 > EffectiveWeight FWP_VALUE0 > } > > Now I can run my code in 32bit go. But I hope my code can run 64bit go > without any conditional compile.
I think manual padding is appropriate here. Note that the standard library uses manual padding in syscall in a couple ztypes_*.go files. Looking more closely at the MSDN docs for FwpmFilterAdd0 and FWPM_FILTER0, if I were doing this, I might use «RawContext uint64» instead of «ProviderContextKey GUID», which would be properly aligned without manual padding. I would then convert this value manually to a GUID if the appropriate flag were set in Flags. This would, of course, depend on my expected use of that context field. Also note that in Go, the idiomatic way to pad is with the blank identifier, «_», as in «_ uint32», rather than using «reserved1 uint32». ...Marvin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.