用于Windows 64位的LVITEM

时间:2015-01-03 00:46:56

标签: windows winapi 64-bit pywin32 win64

很长一段时间,我尝试使用LVM_GETITEMW消息和LVIF_TEXT掩码来获取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位结构中应该有更多的“空格”,看起来应该是这样的(指针现在是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指向的文本。

我的问题是:

  1. 是否记录在任何地方?
  2. stateMask为什么c_uint64 - 不应该与state的大小相同?
  3. 哪一个是64位的真正结构?
  4. 谢谢!

1 个答案:

答案 0 :(得分:4)

感谢Raymond Chen的评论,我能够找到答案!

关于data alignment。每个指针应该是8个字节,并且还应该与可以除以8的地址对齐,所以有时在指针之前应该有一个填充。此外,结构的大小应该可以被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)返回88,与ctypes.sizeof(LVITEMW_explicit)相同。

再次感谢有用的评论!