Member Alignment - CHOOSEFONTW Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
TheArkive
Posts: 491
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Member Alignment - CHOOSEFONTW

21 Jul 2020, 10:22

Just wondering if someone can help me check my math here for 32 and 64 bit on this structure:

Link: https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-choosefontw

Code: Select all

;                                           size        offset [32|64]
; typedef struct tagCHOOSEFONTW {
  ; DWORD        lStructSize;               |4        / 0
  ; HWND         hwndOwner;                 |[4|8]    / [ 4| 8]  A_PtrSize * 1
  ; HDC          hDC;                       |[4|8]    / [ 8|16]  A_PtrSize * 2
  ; LPLOGFONTW   lpLogFont;                 |[4|8]    / [12|24]  A_PtrSize * 3
  ; INT          iPointSize;                |4        / [16|32]  A_PtrSize * 4
  ; DWORD        Flags;                     |4        / [20|36]
  ; COLORREF     rgbColors;                 |4        / [24|40] --- this lines up with code
  ; LPARAM       lCustData;                 |[4|8]    / [28|48]
  ; LPCFHOOKPROC lpfnHook;                  |[4|8]    / [32|56]
  ; LPCWSTR      lpTemplateName;            |[4|8]    / [36|64]
  ; HINSTANCE    hInstance;                 |[4|8]    / [40|72]
  ; LPWSTR       lpszStyle;                 |[4|8]    / [44|80]
  ; WORD         nFontType;                 |2        / [48|88]
  ; WORD         ___MISSING_ALIGNMENT__;    |2        / [50|90]
  ; INT          nSizeMin;                  |4        / [52|92]
  ; INT          nSizeMax;                  |4        / [56|96] -- len: 60 or 64? / 100 or 104?
; } CHOOSEFONTW;

I think my confusion is with the WORD alignment. I think a word is a 2-byte value, but not sure when/if/how I'm supposed to pad that for proper alignment.
swagfag
Posts: 4243
Joined: 11 Jan 2017, 17:59

Re: Member Alignment - CHOOSEFONTW

21 Jul 2020, 10:52

size is always a multiple of A_PtrSize, so 60 and 104 look about right
User avatar
TheArkive
Posts: 491
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: Member Alignment - CHOOSEFONTW

21 Jul 2020, 11:05

so where did I go wrong in the last 4 members of the 64-bit side? A_PtrSize is 8 on x64, but not every member is padded for 8 bytes... that's where I'm having trouble, understanding when the padding happens, and when it doesnt
swagfag
Posts: 4243
Joined: 11 Jan 2017, 17:59

Re: Member Alignment - CHOOSEFONTW  Topic is solved

21 Jul 2020, 11:51

x64, padded are only:
  • DWORD lStructSize; - 4 bytes, because HWND hwndOwner;(8 bytes) wouldnt have fit next to it
  • COLORREF rgbColors; - 4 bytes, because LPARAM lCustData;(8 bytes) wouldnt have fit next to it
  • INT nSizeMax; - 4 bytes, because the struct size needs to be a multiple of sizeof size_t, ie 8
User avatar
TheArkive
Posts: 491
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: Member Alignment - CHOOSEFONTW

21 Jul 2020, 11:57

Thanks @swagfag. The 2 below i fully understand.
swagfag wrote:
21 Jul 2020, 11:51
x64, padded are only:
  • DWORD lStructSize; - 4 bytes, because HWND hwndOwner;(8 bytes) wouldnt have fit next to it
  • COLORREF rgbColors; - 4 bytes, because LPARAM lCustData;(8 bytes) wouldnt have fit next to it
INT nSizeMax; - 4 bytes, because the struct size needs to be a multiple of sizeof size_t, ie 8
... Oh! Ok, so technically there's 4 bytes of padding at the end of the x64 structure, because as you said, the whole size of the structure needs to be a multiple of 8?

I'm just trying to understand the nitty-gritty bits (no pun intended).

EDIT: After re-reading your last reply about 10x i realize that's basically what you said, but specifically, the padding is at the END right? Since the offset to access the last member still has to be 96?

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 1 guest