Ticket #40: GDIHookHandler.py

File GDIHookHandler.py, 3.2 KB (added by pvagner, 4 years ago)

wrapper for GDIHook.dll. In this version all the code which tryes to avoid duplicates is removed as it's perfectly done within the library it-self. anyway my attempt was not correct.

Line 
1#GDIHookHandler.py
2
3from ctypes import *
4from ctypes.wintypes import *
5import os
6import winUser
7import globalVars
8
9#errors
10GDIHook_OK = 0
11GDIHook_NotFound = -1
12GDIHook_InternalError = -2
13
14MAXLEN=1024
15
16class TGDIHookRec(Structure):
17        _fields_=[('x',c_int),('y',c_int),('handle',c_uint),('text',c_wchar_p)]
18
19dll=windll.GDIHookHandler
20
21def attachToWindow(hwnd,libName=os.path.abspath(u"GDIHook.dll")):
22        return dll.AttachToWindow(hwnd,libName)
23
24def detachFromWindow(hwnd):
25        return dll.DetachFromWindow(hwnd)
26
27def getTextAtCoords(x,y):
28        textBuf=TGDIHookRec()
29        pTextBuf=pointer(textBuf)
30        elems=DWORD()
31        res=dll.GetTextAtCoords(x,y,byref(pTextBuf),byref(elems))
32        if res != GDIHook_OK:
33                if res == GDIHook_InternalError: globalVars.log.error("Internal Error in GDIHook Library")
34                return None
35        textBufArrayType=(TGDIHookRec*elems.value)
36        textBufArray=textBufArrayType()
37        textBuf=cast(pTextBuf, POINTER(textBufArrayType)).contents
38        internalList=[]
39        for pt in textBuf:
40                internalList.append((pt.x, pt.y, pt.handle, pt.text))
41        return internalList
42
43def getTextInRect(rect):
44        textBuf=TGDIHookRec()
45        pTextBuf=pointer(textBuf)
46        elems=DWORD()
47        res=dll.GetTextInRect(byref(RECT(rect[0],rect[1],rect[2],rect[3])),byref(pTextBuf),byref(elems))
48        if res != GDIHook_OK:
49                if res == GDIHook_InternalError: globalVars.log.error("Internal Error in GDIHook Library")
50                return None
51        textBufArrayType=(TGDIHookRec*elems.value)
52        textBufArray=textBufArrayType()
53        textBuf=cast(pTextBuf, POINTER(textBufArrayType)).contents
54        internalList=[]
55        for pt in textBuf:
56                internalList.append((pt.x, pt.y, pt.handle, pt.text))
57        return internalList
58
59def getTextFromWindow(handle):
60        textBuf=TGDIHookRec()
61        pTextBuf=pointer(textBuf)
62        elems=DWORD()
63        res=dll.GetTextFromWindow(handle,byref(pTextBuf),byref(elems))
64        if res != GDIHook_OK:
65                if res == GDIHook_InternalError: globalVars.log.error("Internal Error in GDIHook Library")
66                return None
67        textBufArrayType=(TGDIHookRec*elems.value)
68        textBufArray=textBufArrayType()
69        textBuf=cast(pTextBuf, POINTER(textBufArrayType)).contents
70        internalList=[]
71        for pt in textBuf:
72                internalList.append((pt.x, pt.y, pt.handle, pt.text))
73        return internalList
74
75def clearStorageInRect(l,t,r,b):
76        dll.ClearStorageInRect(l,t,r,b)
77
78def makeText(internalList,windowHandles=None):
79        if not internalList:
80                return None
81        x=None
82        y=None
83        oldX=None
84        oldY=None
85        textList=[]
86        for i in internalList:
87                x=i[0]
88                y=i[1]
89                if oldY and y>oldY:
90                        if (not windowHandles or i[2] in windowHandles):
91                                textList.append("\r\n%s"%i[3])
92                elif oldX and x>oldY:
93                        if (not windowHandles or i[2] in windowHandles):
94                                textList.append("\t%s"%i[3])
95                else:
96                        if (not windowHandles or i[2] in windowHandles):
97                                textList.append(i[3])
98                oldX=x
99                oldY=y
100        return "".join(textList)
101
102handleList=[]
103
104def getWindowHandles(obj):
105#       globalVars.log.warning("getting %d window handles"%len(handleList))
106        child=obj.firstChild
107        while child and winUser.isDescendantWindow(obj.windowHandle,child.windowHandle):
108                w=child.windowHandle
109                if w not in handleList:
110                        winUser.sendMessage(w,winUser.WM_PAINT,None,None)
111                        handleList.append(w)
112                        getWindowHandles(child)
113                try:
114                        child=child.next
115                except:
116                        child=None