| 1 | #GDIHookHandler.py
|
|---|
| 2 |
|
|---|
| 3 | from ctypes import *
|
|---|
| 4 | from ctypes.wintypes import *
|
|---|
| 5 | import os
|
|---|
| 6 | import winUser
|
|---|
| 7 | import globalVars
|
|---|
| 8 |
|
|---|
| 9 | #errors
|
|---|
| 10 | GDIHook_OK = 0
|
|---|
| 11 | GDIHook_NotFound = -1
|
|---|
| 12 | GDIHook_InternalError = -2
|
|---|
| 13 |
|
|---|
| 14 | MAXLEN=1024
|
|---|
| 15 |
|
|---|
| 16 | class TGDIHookRec(Structure):
|
|---|
| 17 | _fields_=[('x',c_int),('y',c_int),('handle',c_uint),('text',c_wchar_p)]
|
|---|
| 18 |
|
|---|
| 19 | dll=windll.GDIHookHandler
|
|---|
| 20 |
|
|---|
| 21 | def attachToWindow(hwnd,libName=os.path.abspath(u"GDIHook.dll")):
|
|---|
| 22 | return dll.AttachToWindow(hwnd,libName)
|
|---|
| 23 |
|
|---|
| 24 | def detachFromWindow(hwnd):
|
|---|
| 25 | return dll.DetachFromWindow(hwnd)
|
|---|
| 26 |
|
|---|
| 27 | def 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 |
|
|---|
| 43 | def 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 |
|
|---|
| 59 | def 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 |
|
|---|
| 75 | def clearStorageInRect(l,t,r,b):
|
|---|
| 76 | dll.ClearStorageInRect(l,t,r,b)
|
|---|
| 77 |
|
|---|
| 78 | def 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 |
|
|---|
| 102 | handleList=[]
|
|---|
| 103 |
|
|---|
| 104 | def 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
|
|---|