windows 64 位的 LVITEM
LVITEM for windows 64 bit
很长一段时间我都尝试使用带有LVIF_TEXT
掩码的LVM_GETITEMW
消息来获取ListView的文本。我的程序在 32 位架构下运行,但在 64 位架构下运行失败。
我发现问题出在 LVITEM
结构上。很快,我的问题是哪种结构适合 64 位以及为什么。
我用作 LVITEMW
结构的结构具有以下字段:
('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)
(用python 2.7 ctypes写的,但这只是一种写法-语言真的无关紧要)。
这些字段与 documented.
经过大量谷歌搜索后,我发现这个 forum 正是我所需要的 - 64 位解决方案!
所以在 64 位中,结构应该有更多 "spaces",并且应该看起来像这样(指针现在是 64 位的,stateMask
也是 64 位的。这有点不同根据论坛的建议但也有效):
('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)
论坛建议有:
('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
这也有效,在我需要的列表中,这是 pszText 指向的文本。
我的问题是:
- 这有任何记录吗?
- 为什么
stateMask
应该是 c_uint64
- 它不应该总是和 state
一样大吗?
- 哪个是真正的 64 位结构?
谢谢!
感谢 Raymond Chen 的评论,我才找到答案!
大约 data alignment。每个指针都应该是8个字节,并且还应该对齐到一个可以被8整除的地址,所以有时候指针前应该有一个padding。此外,结构的大小应该可以被 min(max(sizeof(each field in the struct)), 8)
整除,在我们的例子中是 8,因为指针的大小是 8.
class LVITEMW_explicit(ctypes.Structure):
_pack_ = 1
_fields_ = [
('mask', c_uint32), # 0
('iItem', c_int32), # 4
('iSubItem', c_int32), # 8
('state', c_uint32), # 12
('stateMask', c_uint32), # 16
('padding1', c_int),
('pszText', c_uint64), # 20 --> 24 after padding (A pointer)
('cchTextMax', c_int32), # 32
('iImage', c_int32), # 36
('lParam', c_uint64), # 40 (On 32 bit should be c_long which is 32 bits)
('iIndent', c_int32), # 48
('iGroupId', c_int32), # 52
('cColumns', c_uint32), # 56
('padding2', c_int),
('puColumns', c_uint64), # 60 --> 64 after padding (A pointer)
('piColFmt', c_int64), # 72 (A pointer)
('iGroup', c_int32), # 80
('padding3', c_int32), # The total length was 84 before padding3 was added, which is not dividable by 8
]
或者这真的应该写成 - 没有 _pack_ = 1
:
class LVITEMW(ctypes.Structure):
_fields_ = [
('mask', c_uint32),
('iItem', c_int32),
('iSubItem', c_int32),
('state', c_uint32),
('stateMask', c_uint32),
('pszText', c_uint64),
('cchTextMax', c_int32),
('iImage', c_int32),
('lParam', c_uint64), # On 32 bit should be c_long
('iIndent', c_int32),
('iGroupId', c_int32),
('cColumns', c_uint32),
('puColumns', c_uint64),
('piColFmt', c_int64),
('iGroup', c_int32),
]
确实ctypes.sizeof(LVITEMW)
returns 88,与ctypes.sizeof(LVITEMW_explicit)
相同。
再次感谢您的宝贵意见!
很长一段时间我都尝试使用带有LVIF_TEXT
掩码的LVM_GETITEMW
消息来获取ListView的文本。我的程序在 32 位架构下运行,但在 64 位架构下运行失败。
我发现问题出在 LVITEM
结构上。很快,我的问题是哪种结构适合 64 位以及为什么。
我用作 LVITEMW
结构的结构具有以下字段:
('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)
(用python 2.7 ctypes写的,但这只是一种写法-语言真的无关紧要)。
这些字段与 documented.
经过大量谷歌搜索后,我发现这个 forum 正是我所需要的 - 64 位解决方案!
所以在 64 位中,结构应该有更多 "spaces",并且应该看起来像这样(指针现在是 64 位的,stateMask
也是 64 位的。这有点不同根据论坛的建议但也有效):
('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)
论坛建议有:
('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
这也有效,在我需要的列表中,这是 pszText 指向的文本。
我的问题是:
- 这有任何记录吗?
- 为什么
stateMask
应该是c_uint64
- 它不应该总是和state
一样大吗? - 哪个是真正的 64 位结构?
谢谢!
感谢 Raymond Chen 的评论,我才找到答案!
大约 data alignment。每个指针都应该是8个字节,并且还应该对齐到一个可以被8整除的地址,所以有时候指针前应该有一个padding。此外,结构的大小应该可以被 min(max(sizeof(each field in the struct)), 8)
整除,在我们的例子中是 8,因为指针的大小是 8.
class LVITEMW_explicit(ctypes.Structure):
_pack_ = 1
_fields_ = [
('mask', c_uint32), # 0
('iItem', c_int32), # 4
('iSubItem', c_int32), # 8
('state', c_uint32), # 12
('stateMask', c_uint32), # 16
('padding1', c_int),
('pszText', c_uint64), # 20 --> 24 after padding (A pointer)
('cchTextMax', c_int32), # 32
('iImage', c_int32), # 36
('lParam', c_uint64), # 40 (On 32 bit should be c_long which is 32 bits)
('iIndent', c_int32), # 48
('iGroupId', c_int32), # 52
('cColumns', c_uint32), # 56
('padding2', c_int),
('puColumns', c_uint64), # 60 --> 64 after padding (A pointer)
('piColFmt', c_int64), # 72 (A pointer)
('iGroup', c_int32), # 80
('padding3', c_int32), # The total length was 84 before padding3 was added, which is not dividable by 8
]
或者这真的应该写成 - 没有 _pack_ = 1
:
class LVITEMW(ctypes.Structure):
_fields_ = [
('mask', c_uint32),
('iItem', c_int32),
('iSubItem', c_int32),
('state', c_uint32),
('stateMask', c_uint32),
('pszText', c_uint64),
('cchTextMax', c_int32),
('iImage', c_int32),
('lParam', c_uint64), # On 32 bit should be c_long
('iIndent', c_int32),
('iGroupId', c_int32),
('cColumns', c_uint32),
('puColumns', c_uint64),
('piColFmt', c_int64),
('iGroup', c_int32),
]
确实ctypes.sizeof(LVITEMW)
returns 88,与ctypes.sizeof(LVITEMW_explicit)
相同。
再次感谢您的宝贵意见!