pywin32读取SysListView32

pywin32读取SysListView32,首先安装与自己py版本相应的扩展,下载地址

下面代码以任务栏管理器为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding:utf-8 -*-
'''Coding by b4dboy'''
import struct
import ctypes
import win32api
import win32gui
from win32con import *
from commctrl import *
OpenProcess = ctypes.windll.kernel32.OpenProcess
create_string_buffer = ctypes.create_string_buffer
VirtualFreeEx = ctypes.windll.kernel32.VirtualFreeEx
VirtualAllocEx = ctypes.windll.kernel32.VirtualAllocEx
ReadProcessMemory = ctypes.windll.kernel32.ReadProcessMemory
WriteProcessMemory = ctypes.windll.kernel32.WriteProcessMemory
GetWindowThreadProcessId = ctypes.windll.user32.GetWindowThreadProcessId
class LVITEM_32(ctypes.Structure):
_fields_ = [
('mask', ctypes.c_uint),
('iItem', ctypes.c_int),
('iSubItem', ctypes.c_int),
('state', ctypes.c_uint),
('stateMask', ctypes.c_uint),
('pszText', ctypes.c_uint32),
('cchTextMax', ctypes.c_int),
('iImage', ctypes.c_int),
('lParam', ctypes.c_uint32),
('iIndent', ctypes.c_int),
]
class LVITEM_64(ctypes.Structure):
_fields_ = [
('mask', ctypes.c_uint),
('iItem', ctypes.c_int),
('iSubItem', ctypes.c_int),
('state', ctypes.c_uint),
('stateMask', ctypes.c_uint),
('pszText', ctypes.c_uint64),
('cchTextMax', ctypes.c_int),
('iImage', ctypes.c_int),
('lParam', ctypes.c_uint64),
('iIndent', ctypes.c_int),
]
def GetItemCount(hwnd):
return win32gui.SendMessage(hwnd, LVM_GETITEMCOUNT)
def GetColumnCount(hwnd):
header = win32gui.SendMessage(hwnd, LVM_GETHEADER)
return win32gui.SendMessage(header, HDM_GETITEMCOUNT)
def GetItem(hwnd, item_index, subitem_index):
ITEMTEXT_LENGTH = 2048
if is_64bit(hwnd):
item = LVITEM_64()
else:
item = LVITEM_32()
target_ph = open_process(hwnd)
target_item_ptr = VirtualAllocEx(target_ph, 0, ctypes.sizeof(item), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)
target_pszText_ptr = VirtualAllocEx(target_ph, 0, ITEMTEXT_LENGTH, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)
item.mask = LVIF_STATE | LVIF_TEXT | LVIF_IMAGE | LVIF_INDENT
item.iItem = item_index
item.iSubItem = subitem_index
item.stateMask = ctypes.c_uint(-1)
item.pszText = target_pszText_ptr
item.cchTextMax = ITEMTEXT_LENGTH
WriteProcessMemory(target_ph, target_item_ptr, ctypes.pointer(item), ctypes.sizeof(item), 0)
win32gui.SendMessage(hwnd, LVM_GETITEMA, item_index, target_item_ptr)
ReadProcessMemory(target_ph, target_item_ptr, ctypes.byref(item), ctypes.sizeof(item), 0)
item_text = create_string_buffer(ITEMTEXT_LENGTH)
ReadProcessMemory(target_ph, target_pszText_ptr, ctypes.byref(item_text), ITEMTEXT_LENGTH, 0)
VirtualFreeEx(target_ph, target_pszText_ptr, 0, MEM_RELEASE)
VirtualFreeEx(target_ph, target_item_ptr, 0, MEM_RELEASE)
win32api.CloseHandle(target_ph)
return {"text": item_text.value, "state": item.state, "indent": item.iIndent, "image": item.iImage}
def open_process(handle):
pid = create_string_buffer(4)
pid_ptr = ctypes.addressof(pid)
GetWindowThreadProcessId(handle, pid_ptr)
return OpenProcess(PROCESS_ALL_ACCESS, False, struct.unpack("i", pid)[0])
def is_64bit(handle):
import os
if not "PROGRAMFILES(X86)" in os.environ:
return False
i = ctypes.c_int()
target_ph = open_process(handle)
ctypes.windll.kernel32.IsWow64Process(target_ph, ctypes.byref(i))
win32api.CloseHandle(target_ph)
return (i.value == 0)
def main():
hwnd = win32gui.FindWindow("#32770", None)
if hwnd == 0:
print("not find")
return
hwnd = win32gui.FindWindowEx(hwnd, None, "#32770", None);
hwnd = win32gui.FindWindowEx(hwnd, None, "SysListView32", None);
rows = GetItemCount(hwnd);
#cols = GetColumnCount(hwnd);
for item_index in range(0, rows):
print GetItem(hwnd, item_index, 0)
if __name__ == "__main__":
main()