For a long time I tried to use the LVM_GETITEMW
message with LVIF_TEXT
mask to get the text of a ListView. My program worked in 32 bit but not in 64 bit architecture.
I discovered that the problem was at the LVITEM
struct. Shortly, my question is which struct is the appropriate one for 64 bit and why.
The struct I used as the LVITEMW
struct had the following fields:
('mask', c_uint32),
('iItem', c_int32),
('iSubItem', c_int32),
('state', c_uint32),
('stateMask', c_uint32),
('pszText', c_uint32),
('cchTextMax', c_int32),
('iImage', c_int32),
('lParam', c_uint64),
('iIndent', c_int32),
('iGroupId', c_int32),
('cColumns', c_uint32),
('puColumns', c_uint32),
('piColFmt', c_int32),
('iGroup', c_int32)
(Written with python 2.7 ctypes, but this is just a form of writing - the language is really irrelevant).
These fields are just as documented.
After a lot of googling, I found this forum which had exactly what I needed - the 64 bit solution!
So in 64 bit the struct should have more "spaces", and should look something like this (the pointers are now 64 bit and also the stateMask
is 64 bit. That's a little bit different from what the forum suggested but works too):
('mask', c_uint32),
('iItem', c_int32),
('iSubItem', c_int32),
('state', c_uint32),
('stateMask', c_uint64), <-- Now 64 bit
('pszText', c_uint64), <-- Now 64 bit which makes sense since this is a pointer
('cchTextMax', c_int32),
('iImage', c_int32),
('lParam', c_uint64),
('iIndent', c_int32),
('iGroupId', c_int32),
('cColumns', c_uint32),
('puColumns', c_uint64), <-- Now 64 bit which makes sense since this is a pointer
('piColFmt', c_int64), <-- Now 64 bit which makes sense since this is a pointer
('iGroup', c_int32)
The forum suggested having:
('mask', c_uint32),
('iItem', c_int32),
('iSubItem', c_int32),
('state', c_uint32),
('stateMask', c_uint64),
('pszText', c_uint64),
('cchTextMax', c_int32),
('iImage', c_int64), <-- Now 64 bit
('lParam', c_uint32),
('iIndent', c_int32),
('iGroupId', c_int32),
('cColumns', c_uint32),
('puColumns', c_uint32),
('piColFmt', c_int32),
('iGroup', c_int64), <-- Now 128 bit all together
('iGroup2', c_int64) <-- continuation
Which also works, at list for my need which is the text pointed by pszText.
And my questions are:
- Is this documented anywhere?
- Why should the
stateMask
bec_uint64
- shouldn't it always be the same size as thestate
? - Which one is the true struct for 64 bit?
Thank you!
Thanks to the comment by Raymond Chen I was able to figure out the answer!
It's about data alignment. Every pointer should be 8 bytes and should also be aligned to an address that can be divided by 8, so sometimes there should be a padding before the pointer. Also, the size of the struct should be dividable by
min(max(sizeof(each field in the struct)), 8)
which is 8 in our case since the size of a pointer is 8.Or as this should really be written - without the
_pack_ = 1
:And indeed
ctypes.sizeof(LVITEMW)
returns 88, same asctypes.sizeof(LVITEMW_explicit)
.Thanks again for the helpful comments!