pywin32读取SysListView32

作者:hack1990 时间:14-10-17 阅读数:803人阅读

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


下载地址http://sourceforge.net/projects/pywin32/files/pywin32/


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


# -*- 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()



上一篇:R语言处理大文本

下一篇:Axure RP 7.0

发表评论