Page 1 of 1

[Win32API] Field alignement in win32api structure

Posted: Fri May 22, 2020 3:55 pm
by Azias
Hello,

I'm currently playing around with the win32api and I encountered a tiny problem while I was defining the WLAN_BSS_ENTRY structure.

When I did a "SizeOf()" on it in purebasic, it returned 351 instead of 360, which I got in Visual Studio, and after a bit of digging I learned about alignement in structures. and fixed it with "_PADDING_x.x" fields.

Finally, I just wanted to ask if this is the right way to do it since the "Align" keyword didn't 'work' properly in this case, and if the win32api would work with this structure ?


My implementation:

Code: Select all

Structure WLAN_BSS_ENTRY 
	dot11Ssid.DOT11_SSID
	uPhyId.ULONG
	dot11Bssid.DOT11_MAC_ADDRESS
	
	_PADDING1.u
	
	dot11BssType.DOT11_BSS_TYPE
	dot11BssPhyType.DOT11_PHY_TYPE
	lRssi.LONG
	uLinkQuality.ULONG
	bInRegDomain.BOOLEAN
	
	_PADDING_2.BOOLEAN
	
	usBeaconPeriod.USHORT
	
	_PADDING_3.ULONG
	
	ullTimestamp.ULONGLONG
	ullHostTimestamp.ULONGLONG
	usCapabilityInformation.USHORT
	
	_PADDING_4.u
	
	ulChCenterFrequency.ULONG
	wlanRateSet.WLAN_RATE_SET
	ulIeOffset.ULONG
	ulIeSize.ULONG
EndStructure
Macro PWLAN_BSS_ENTRY : WLAN_BSS_ENTRY : EndMacro
Original in Wlanapi.h

Code: Select all

typedef struct _WLAN_BSS_ENTRY {
    DOT11_SSID dot11Ssid;
    ULONG uPhyId;
    DOT11_MAC_ADDRESS dot11Bssid;
    DOT11_BSS_TYPE dot11BssType;
    DOT11_PHY_TYPE dot11BssPhyType;
    LONG lRssi;
    ULONG uLinkQuality;
    BOOLEAN bInRegDomain;
    USHORT usBeaconPeriod;
    ULONGLONG ullTimestamp;
    ULONGLONG ullHostTimestamp;
    USHORT usCapabilityInformation;
    ULONG  ulChCenterFrequency;
    WLAN_RATE_SET wlanRateSet;
    // the beginning of the IE blob
    // the offset is w.r.t. the beginning of the entry
    ULONG ulIeOffset;
    // size of the IE blob
    ULONG ulIeSize;
} WLAN_BSS_ENTRY, * PWLAN_BSS_ENTRY;

Re: [Win32API] Field alignement in win32api structure

Posted: Fri May 22, 2020 4:38 pm
by infratec
Your solution is not compilable, how did you get the size :?:

My version:

Code: Select all

#DOT11_SSID_MAX_LENGTH = 32
#DOT11_RATE_SET_MAX_LENGTH = 126


#dot11_BSS_type_infrastructure = 1
#dot11_BSS_type_independent = 2
#dot11_BSS_type_any = 3


#dot11_phy_type_unknown     = 0
#dot11_phy_type_any         = 0
#dot11_phy_type_fhss        = 1
#dot11_phy_type_dsss        = 2
#dot11_phy_type_irbaseband  = 3
#dot11_phy_type_ofdm        = 4
#dot11_phy_type_hrdsss      = 5
#dot11_phy_type_erp         = 6
#dot11_phy_type_ht          = 7
#dot11_phy_type_vht         = 8
#dot11_phy_type_IHV_start   = $80000000
#dot11_phy_type_IHV_end     = $ffffffff


Macro DOT11_MAC_ADDRESS
  a[6]
EndMacro

Structure DOT11_SSID
  uSSIDLength.l
  ucSSID.a[#DOT11_SSID_MAX_LENGTH]
EndStructure

Structure WLAN_RATE_SET
  uRateSetLength.l
  usRateSet.u[#DOT11_RATE_SET_MAX_LENGTH]
EndStructure


Structure WLAN_BSS_ENTRY Align #PB_Structure_AlignC
   dot11Ssid.DOT11_SSID
   uPhyId.l
   dot11Bssid.DOT11_MAC_ADDRESS
   
   dot11BssType.i
   dot11BssPhyType.i
   lRssi.l
   uLinkQuality.l
   bInRegDomain.i
   
   usBeaconPeriod.u
   
   ullTimestamp.q
   ullHostTimestamp.q
   usCapabilityInformation.u
   
   ulChCenterFrequency.l
   wlanRateSet.WLAN_RATE_SET
   ulIeOffset.l
   ulIeSize.l
 EndStructure
 
 Debug SizeOf(WLAN_BSS_ENTRY)
Shows 360 with PB x86

Re: [Win32API] Field alignement in win32api structure

Posted: Fri May 22, 2020 5:13 pm
by Azias
I didn't give my include with the macros for the data types since i didn't think you would try to compile it.
And I also missed the constant after the align.

Here is the full preprocessed code:

Code: Select all

#DOT11_SSID_MAX_LENGTH = 32

#DOT11_RATE_SET_MAX_LENGTH = 126

Structure DOT11_SSID
	uSSIDLength.l 
	ucSSID.a [#DOT11_SSID_MAX_LENGTH]
EndStructure

; Shouldn't be a structure, but it doesn't affect the final result.
Structure DOT11_MAC_ADDRESS
	ucPart.a [6]
EndStructure

Structure WLAN_RATE_SET
	uRateSetLength.l 
	usRateSet.u [#DOT11_RATE_SET_MAX_LENGTH]
EndStructure

Structure WLAN_BSS_ENTRY ; Align #PB_Structure_AlignC
	dot11Ssid.DOT11_SSID
	uPhyId.l 
	dot11Bssid.DOT11_MAC_ADDRESS
	dot11BssType.l 
	dot11BssPhyType.l 
	lRssi.l 
	uLinkQuality.l 
	bInRegDomain.a  
	usBeaconPeriod.u 
	ullTimestamp.q 
	ullHostTimestamp.q 
	usCapabilityInformation.u 
	ulChCenterFrequency.l 
	wlanRateSet.WLAN_RATE_SET
	ulIeOffset.l 
	ulIeSize.l 
EndStructure

Debug SizeOf(WLAN_BSS_ENTRY)
When I compile it without the #PB_Structure_AlignC constant it outputs 351, but with it it's 360.
I missed it while reading the documentation.

Thank you.