def _getTextRange(self,start,end): if self.obj.editAPIVersion>=2: bufLen=((end-start)+1)*2 if self.obj.isWindowUnicode: textRange=TextRangeUStruct() else: textRange=TextRangeAStruct() textRange.chrg.cpMin=start textRange.chrg.cpMax=end processHandle=self.obj.processHandle internalBuf=winKernel.virtualAllocEx(processHandle,None,bufLen,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: textRange.lpstrText=internalBuf internalTextRange=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(textRange),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalTextRange,ctypes.byref(textRange),ctypes.sizeof(textRange),None) res=watchdog.cancellableSendMessage(self.obj.windowHandle,EM_GETTEXTRANGE,0,internalTextRange) finally: winKernel.virtualFreeEx(processHandle,internalTextRange,0,winKernel.MEM_RELEASE) buf=(ctypes.c_byte*bufLen)() winKernel.readProcessMemory(processHandle,internalBuf,buf,bufLen,None) finally: winKernel.virtualFreeEx(processHandle,internalBuf,0,winKernel.MEM_RELEASE) if self.obj.isWindowUnicode or (res>1 and (buf[res]!=0 or buf[res+1]!=0)): text=ctypes.cast(buf,ctypes.c_wchar_p).value else: text=unicode(ctypes.cast(buf,ctypes.c_char_p).value, errors="replace", encoding=locale.getlocale()[1]) # #4095: Some protected richEdit controls do not hide their password characters. # We do this specifically. # Note that protected standard edit controls get characters hidden in _getStoryText. if text and controlTypes.STATE_PROTECTED in self.obj.states: text=u'*'*len(text) else: text=self._getStoryText()[start:end] return text
def _getColumnLocationRaw(self, index): processHandle = self.processHandle localRect = RECT(left=2, top=index) internalRect = winKernel.virtualAllocEx(processHandle, None, sizeof(localRect), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalRect, byref(localRect), sizeof(localRect), None) watchdog.cancellableSendMessage(self.windowHandle, LVM_GETSUBITEMRECT, (self.IAccessibleChildID - 1), internalRect) winKernel.readProcessMemory(processHandle, internalRect, byref(localRect), sizeof(localRect), None) finally: winKernel.virtualFreeEx(processHandle, internalRect, 0, winKernel.MEM_RELEASE) windll.user32.ClientToScreen(self.windowHandle, byref(localRect)) windll.user32.ClientToScreen(self.windowHandle, byref(localRect, 8)) return (localRect.left, localRect.top, localRect.right - localRect.left, localRect.bottom - localRect.top)
def _get_name(self): curIndex = watchdog.cancellableSendMessage( hwndWinamp, WM_WA_IPC, -1, IPC_PLAYLIST_GET_NEXT_SELECTED) if curIndex < 0: return None info = fileinfo2() info.fileindex = curIndex internalInfo = winKernel.virtualAllocEx(self.processHandle, None, sizeof(info), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(self.processHandle, internalInfo, byref(info), sizeof(info), None) watchdog.cancellableSendMessage(self.windowHandle, WM_WA_IPC, IPC_PE_GETINDEXTITLE, internalInfo) winKernel.readProcessMemory(self.processHandle, internalInfo, byref(info), sizeof(info), None) finally: winKernel.virtualFreeEx(self.processHandle, internalInfo, 0, winKernel.MEM_RELEASE) # file title is fetched in the current locale encoding. # We need to decode it to unicode first. encoding = locale.getlocale()[1] fileTitle = info.filetitle.decode(encoding, errors="replace") return "%d.\t%s\t%s" % (curIndex + 1, fileTitle, info.filelength)
def _getTextRange(self,start,end): if self.obj.editAPIVersion>=2: bufLen=((end-start)+1)*2 if self.obj.isWindowUnicode: textRange=TextRangeUStruct() else: textRange=TextRangeAStruct() textRange.chrg.cpMin=start textRange.chrg.cpMax=end processHandle=self.obj.processHandle internalBuf=winKernel.virtualAllocEx(processHandle,None,bufLen,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: textRange.lpstrText=internalBuf internalTextRange=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(textRange),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalTextRange,ctypes.byref(textRange),ctypes.sizeof(textRange),None) res=watchdog.cancellableSendMessage(self.obj.windowHandle,EM_GETTEXTRANGE,0,internalTextRange) finally: winKernel.virtualFreeEx(processHandle,internalTextRange,0,winKernel.MEM_RELEASE) buf=(ctypes.c_byte*bufLen)() winKernel.readProcessMemory(processHandle,internalBuf,buf,bufLen,None) finally: winKernel.virtualFreeEx(processHandle,internalBuf,0,winKernel.MEM_RELEASE) if self.obj.isWindowUnicode or (res>1 and (buf[res]!=0 or buf[res+1]!=0)): text=ctypes.cast(buf,ctypes.c_wchar_p).value else: text=unicode(ctypes.cast(buf,ctypes.c_char_p).value, errors="replace", encoding=locale.getlocale()[1]) else: text=self._getStoryText()[start:end] return text
def _getTextRange(self, start, end): bufLen = (end - start) + 1 textRange = TextRangeStruct() textRange.chrg.cpMin = start textRange.chrg.cpMax = end processHandle = self.obj.processHandle internalBuf = winKernel.virtualAllocEx( processHandle, None, bufLen, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE ) try: textRange.lpstrText = internalBuf internalTextRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(textRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE ) try: winKernel.writeProcessMemory( processHandle, internalTextRange, ctypes.byref(textRange), ctypes.sizeof(textRange), None ) watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETTEXTRANGE, 0, internalTextRange) finally: winKernel.virtualFreeEx(processHandle, internalTextRange, 0, winKernel.MEM_RELEASE) buf = ctypes.create_string_buffer(bufLen) winKernel.readProcessMemory(processHandle, internalBuf, buf, bufLen, None) finally: winKernel.virtualFreeEx(processHandle, internalBuf, 0, winKernel.MEM_RELEASE) cp = watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETCODEPAGE, 0, 0) if cp == SC_CP_UTF8: return unicode(buf.value, errors="replace", encoding="utf-8") else: return unicode(buf.value, errors="replace", encoding=locale.getlocale()[1])
def _getPointFromOffset(self, offset): if self.obj.editAPIVersion == 1 or self.obj.editAPIVersion >= 3: processHandle = self.obj.processHandle internalP = winKernel.virtualAllocEx(processHandle, None, ctypes.sizeof(PointLStruct), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: p = PointLStruct(0, 0) winKernel.writeProcessMemory(processHandle, internalP, ctypes.byref(p), ctypes.sizeof(p), None) watchdog.cancellableSendMessage(self.obj.windowHandle, EM_POSFROMCHAR, internalP, offset) winKernel.readProcessMemory(processHandle, internalP, ctypes.byref(p), ctypes.sizeof(p), None) finally: winKernel.virtualFreeEx(processHandle, internalP, 0, winKernel.MEM_RELEASE) point = textInfos.Point(p.x, p.y) else: res = watchdog.cancellableSendMessage(self.obj.windowHandle, EM_POSFROMCHAR, offset, None) point = textInfos.Point(winUser.LOWORD(res), winUser.HIWORD(res)) (left, top, width, height) = self.obj.location point.x = point.x + left point.y = point.y + top return point
def _getColumnImageIDRaw(self, index): processHandle = self.processHandle internalItem = winKernel.virtualAllocEx(processHandle, None, sizeof(self.LVITEM), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: item = self.LVITEM(iItem=self.IAccessibleChildID - 1, mask=LVIF_IMAGE | LVIF_COLUMNS, iSubItem=index) winKernel.writeProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) item.mask = LVIF_IMAGE | LVIF_COLUMNS winKernel.writeProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) watchdog.cancellableSendMessage(self.windowHandle, LVM_GETITEMW, 0, internalItem) winKernel.readProcessMemory(processHandle, internalItem, byref(item), sizeof(item), None) finally: winKernel.virtualFreeEx(processHandle, internalItem, 0, winKernel.MEM_RELEASE) return item.iImage
def _getSelectionOffsets(self): if self.obj.editAPIVersion >= 1: charRange = CharRangeStruct() processHandle = self.obj.processHandle internalCharRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(charRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: watchdog.cancellableSendMessage(self.obj.windowHandle, EM_EXGETSEL, 0, internalCharRange) winKernel.readProcessMemory(processHandle, internalCharRange, ctypes.byref(charRange), ctypes.sizeof(charRange), None) finally: winKernel.virtualFreeEx(processHandle, internalCharRange, 0, winKernel.MEM_RELEASE) return (charRange.cpMin, charRange.cpMax) else: start = ctypes.c_uint() end = ctypes.c_uint() res = watchdog.cancellableSendMessage(self.obj.windowHandle, EM_GETSEL, ctypes.byref(start), ctypes.byref(end)) return start.value, end.value
def _getCharFormat(self, offset): oldSel = self._getSelectionOffsets() if oldSel != (offset, offset + 1): self._setSelectionOffsets(offset, offset + 1) if self.obj.isWindowUnicode: charFormatStruct = CharFormat2WStruct else: charFormatStruct = CharFormat2AStruct charFormat = charFormatStruct() charFormat.cbSize = ctypes.sizeof(charFormatStruct) processHandle = self.obj.processHandle internalCharFormat = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(charFormat), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalCharFormat, ctypes.byref(charFormat), ctypes.sizeof(charFormat), None) watchdog.cancellableSendMessage(self.obj.windowHandle, EM_GETCHARFORMAT, SCF_SELECTION, internalCharFormat) winKernel.readProcessMemory(processHandle, internalCharFormat, ctypes.byref(charFormat), ctypes.sizeof(charFormat), None) finally: winKernel.virtualFreeEx(processHandle, internalCharFormat, 0, winKernel.MEM_RELEASE) if oldSel != (offset, offset + 1): self._setSelectionOffsets(oldSel[0], oldSel[1]) return charFormat
def _getColumnLocationRawOutProc(self, index: int) -> ctypes.wintypes.RECT: processHandle=self.processHandle # LVM_GETSUBITEMRECT requires a pointer to a RECT structure that will receive the subitem bounding rectangle information. localRect=RECT( # Returns the bounding rectangle of the entire item, including the icon and label. left=LVIR_LABEL, # According to Microsoft, top should be the one-based index of the subitem. # However, indexes coming from LVM_GETCOLUMNORDERARRAY are zero based. top=index ) internalRect=winKernel.virtualAllocEx(processHandle,None,sizeof(localRect),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalRect,byref(localRect),sizeof(localRect),None) res = watchdog.cancellableSendMessage( self.windowHandle, LVM_GETSUBITEMRECT, self.IAccessibleChildID - 1, internalRect ) if res: winKernel.readProcessMemory( processHandle, internalRect, ctypes.byref(localRect), ctypes.sizeof(localRect), None ) finally: winKernel.virtualFreeEx(processHandle,internalRect,0,winKernel.MEM_RELEASE) if res == 0: log.debugWarning(f"LVM_GETSUBITEMRECT failed for index {index} in list") return None return localRect
def _getCharFormat(self, range): oldSel = self.obj.ITextSelectionObject.duplicate if not (oldSel.start == range.start and oldSel.end == range.end): self.obj.ITextSelectionObject.start = range.start self.obj.ITextSelectionObject.end = range.end if self.obj.isWindowUnicode: charFormatStruct = CharFormat2WStruct else: charFormatStruct = CharFormat2AStruct charFormat = charFormatStruct() charFormat.cbSize = ctypes.sizeof(charFormatStruct) processHandle = self.obj.processHandle internalCharFormat = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(charFormat), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalCharFormat, ctypes.byref(charFormat), ctypes.sizeof(charFormat), None) watchdog.cancellableSendMessage(self.obj.windowHandle, EM_GETCHARFORMAT, SCF_SELECTION, internalCharFormat) winKernel.readProcessMemory(processHandle, internalCharFormat, ctypes.byref(charFormat), ctypes.sizeof(charFormat), None) finally: winKernel.virtualFreeEx(processHandle, internalCharFormat, 0, winKernel.MEM_RELEASE) if not (oldSel.start == range.start and oldSel.end == range.end): self.obj.ITextSelectionObject.start = oldSel.start self.obj.ITextSelectionObject.end = oldSel.end return charFormat
def _get_name(self): hItem = watchdog.cancellableSendMessage(self.windowHandle, CLM_GETSELECTION, 0, 0) internalBuf = winKernel.virtualAllocEx(self.processHandle, None, MAXITEMTEXTLEN, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: watchdog.cancellableSendMessage(self.windowHandle, CLM_GETITEMTEXT, hItem, internalBuf) buf = create_unicode_buffer(MAXITEMTEXTLEN) winKernel.readProcessMemory(self.processHandle, internalBuf, buf, MAXITEMTEXTLEN, None) text = buf.value statusMsgPtr = watchdog.cancellableSendMessage( self.windowHandle, CLM_GETSTATUSMSG, hItem, 0) if statusMsgPtr > 0: buf2 = create_unicode_buffer(MAXSTATUSMSGLEN) winKernel.readProcessMemory(self.processHandle, statusMsgPtr, buf2, MAXSTATUSMSGLEN, None) text = "%s %s" % (text, buf2.value) finally: winKernel.virtualFreeEx(self.processHandle, internalBuf, 0, winKernel.MEM_RELEASE) return text
def _getTextRange(self, start, end): bufLen = (end - start) + 1 textRange = TextRangeStruct() textRange.chrg.cpMin = start textRange.chrg.cpMax = end processHandle = self.obj.processHandle internalBuf = winKernel.virtualAllocEx(processHandle, None, bufLen, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: textRange.lpstrText = internalBuf internalTextRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(textRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalTextRange, ctypes.byref(textRange), ctypes.sizeof(textRange), None) numBytes = watchdog.cancellableSendMessage( self.obj.windowHandle, SCI_GETTEXTRANGE, 0, internalTextRange) finally: winKernel.virtualFreeEx(processHandle, internalTextRange, 0, winKernel.MEM_RELEASE) buf = ctypes.create_string_buffer(bufLen) winKernel.readProcessMemory(processHandle, internalBuf, buf, bufLen, None) finally: winKernel.virtualFreeEx(processHandle, internalBuf, 0, winKernel.MEM_RELEASE) return textUtils.getTextFromRawBytes(buf.raw, numChars=numBytes, encoding=self.encoding, errorsFallback="surrogateescape")
def _getTextRange(self, start, end): if self.obj.editAPIVersion >= 2: # Calculate a buffer size that is twice the size of the text range and a NULL terminating character. # As unicode characters are two bytes in size, # this ensures that our buffer can hold both ANSI and unicode character strings. bufLen = ((end - start) + 1) * 2 # Even though this can return unicode text, we use the ANSI version of the structure. # Using the unicode structure isn't strictly necessary and saves us some confusion textRange = TextRangeStruct() textRange.chrg.cpMin = start textRange.chrg.cpMax = end processHandle = self.obj.processHandle internalBuf = winKernel.virtualAllocEx(processHandle, None, bufLen, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: textRange.lpstrText = internalBuf internalTextRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(textRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalTextRange, ctypes.byref(textRange), ctypes.sizeof(textRange), None) # EM_GETTEXTRANGE returns the number of characters copied, # not including the terminating null character. # See https://docs.microsoft.com/en-us/windows/desktop/controls/em-gettextrange numChars = watchdog.cancellableSendMessage( self.obj.windowHandle, EM_GETTEXTRANGE, 0, internalTextRange) finally: winKernel.virtualFreeEx(processHandle, internalTextRange, 0, winKernel.MEM_RELEASE) buf = ctypes.create_string_buffer(bufLen) # Copy the text in the text range to our own buffer. winKernel.readProcessMemory(processHandle, internalBuf, buf, bufLen, None) finally: winKernel.virtualFreeEx(processHandle, internalBuf, 0, winKernel.MEM_RELEASE) # Find out which encoding to use to decode the bytes in the buffer. if ( # The window is unicode, the text range contains multi byte characters. self.obj.isWindowUnicode): encoding = textUtils.WCHAR_ENCODING else: # De encoding will be determined by L{textUtils.getTextFromRawBytes} encoding = None text = textUtils.getTextFromRawBytes(buf.raw, numChars, encoding) # #4095: Some protected richEdit controls do not hide their password characters. # We do this specifically. # Note that protected standard edit controls get characters hidden in _getStoryText. if text and controlTypes.State.PROTECTED in self.obj.states: text = u'*' * len(text) else: text = super(EditTextInfo, self)._getTextRange(start, end) return text
def _getLineNumFromOffset(self,offset): ciChar=AECHARINDEX() processHandle=self.obj.processHandle internalCiChar=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(ciChar),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winUser.sendMessage(self.obj.windowHandle,AEM_RICHOFFSETTOINDEX,offset,internalCiChar) winKernel.readProcessMemory(processHandle,internalCiChar,ctypes.byref(ciChar),ctypes.sizeof(ciChar),None) winKernel.virtualFreeEx(processHandle,internalCiChar,0,winKernel.MEM_RELEASE) return ciChar.nLine
def _getLineNumFromOffset(self,offset): ciChar=AECHARINDEX() processHandle=self.obj.processHandle internalCiChar=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(ciChar),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: watchdog.cancellableSendMessage(self.obj.windowHandle,AEM_RICHOFFSETTOINDEX,offset,internalCiChar) winKernel.readProcessMemory(processHandle,internalCiChar,ctypes.byref(ciChar),ctypes.sizeof(ciChar),None) finally: winKernel.virtualFreeEx(processHandle,internalCiChar,0,winKernel.MEM_RELEASE) return ciChar.nLine
def _get__columnOrderArray(self): coa=(c_int *self.columnCount)() processHandle=self.processHandle internalCoa=winKernel.virtualAllocEx(processHandle,None,sizeof(coa),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalCoa,byref(coa),sizeof(coa),None) res = watchdog.cancellableSendMessage(self.windowHandle,LVM_GETCOLUMNORDERARRAY, self.columnCount, internalCoa) if res: winKernel.readProcessMemory(processHandle,internalCoa,byref(coa),sizeof(coa),None) finally: winKernel.virtualFreeEx(processHandle,internalCoa,0,winKernel.MEM_RELEASE) return coa
def _getColumnLocationRaw(self,index): processHandle=self.processHandle localRect=RECT(left=2,top=index) internalRect=winKernel.virtualAllocEx(processHandle,None,sizeof(localRect),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalRect,byref(localRect),sizeof(localRect),None) watchdog.cancellableSendMessage(self.windowHandle,LVM_GETSUBITEMRECT, (self.IAccessibleChildID-1), internalRect) winKernel.readProcessMemory(processHandle,internalRect,byref(localRect),sizeof(localRect),None) finally: winKernel.virtualFreeEx(processHandle,internalRect,0,winKernel.MEM_RELEASE) windll.user32.ClientToScreen(self.windowHandle,byref(localRect)) windll.user32.ClientToScreen(self.windowHandle,byref(localRect,8)) return (localRect.left,localRect.top,localRect.right-localRect.left,localRect.bottom-localRect.top)
def _getColumnImageIDRaw(self, index): processHandle=self.processHandle internalItem=winKernel.virtualAllocEx(processHandle,None,sizeof(self.LVITEM),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: item=self.LVITEM(iItem=self.IAccessibleChildID-1,mask=LVIF_IMAGE|LVIF_COLUMNS,iSubItem=index) winKernel.writeProcessMemory(processHandle,internalItem,byref(item),sizeof(self.LVITEM),None) item.mask=LVIF_IMAGE|LVIF_COLUMNS winKernel.writeProcessMemory(processHandle,internalItem,byref(item),sizeof(self.LVITEM),None) watchdog.cancellableSendMessage(self.windowHandle,LVM_GETITEMW, 0, internalItem) winKernel.readProcessMemory(processHandle,internalItem,byref(item),sizeof(item),None) finally: winKernel.virtualFreeEx(processHandle,internalItem,0,winKernel.MEM_RELEASE) return item.iImage
def _getFormatFieldAndOffsets(self, offset, formatConfig, calculateOffsets=True): style = watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETSTYLEAT, offset, 0) if calculateOffsets: # we need to manually see how far the style goes, limit to line lineStart, lineEnd = self._getLineOffsets(offset) startOffset = offset while startOffset > lineStart: curStyle = watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETSTYLEAT, startOffset - 1, 0) if curStyle == style: startOffset -= 1 else: break endOffset = offset + 1 while endOffset < lineEnd: curStyle = watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETSTYLEAT, endOffset, 0) if curStyle == style: endOffset += 1 else: break else: startOffset, endOffset = (self._startOffset, self._endOffset) formatField = textInfos.FormatField() if formatConfig["reportFontName"]: # To get font name, We need to allocate memory with in Scintilla's process, and then copy it out fontNameBuf = ctypes.create_string_buffer(32) internalBuf = winKernel.virtualAllocEx( self.obj.processHandle, None, len(fontNameBuf), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE ) try: watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_STYLEGETFONT, style, internalBuf) winKernel.readProcessMemory(self.obj.processHandle, internalBuf, fontNameBuf, len(fontNameBuf), None) finally: winKernel.virtualFreeEx(self.obj.processHandle, internalBuf, 0, winKernel.MEM_RELEASE) formatField["font-name"] = fontNameBuf.value if formatConfig["reportFontSize"]: formatField["font-size"] = "%spt" % watchdog.cancellableSendMessage( self.obj.windowHandle, SCI_STYLEGETSIZE, style, 0 ) if formatConfig["reportLineNumber"]: formatField["line-number"] = self._getLineNumFromOffset(offset) + 1 if formatConfig["reportFontAttributes"]: formatField["bold"] = bool( watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_STYLEGETBOLD, style, 0) ) formatField["italic"] = bool( watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_STYLEGETITALIC, style, 0) ) formatField["underline"] = bool( watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_STYLEGETUNDERLINE, style, 0) ) return formatField, (startOffset, endOffset)
def _get_name(self): curIndex=watchdog.cancellableSendMessage(hwndWinamp,WM_WA_IPC,-1,IPC_PLAYLIST_GET_NEXT_SELECTED) if curIndex <0: return None info=fileinfo2() info.fileindex=curIndex internalInfo=winKernel.virtualAllocEx(self.processHandle,None,sizeof(info),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(self.processHandle,internalInfo,byref(info),sizeof(info),None) watchdog.cancellableSendMessage(self.windowHandle,WM_WA_IPC,IPC_PE_GETINDEXTITLE,internalInfo) winKernel.readProcessMemory(self.processHandle,internalInfo,byref(info),sizeof(info),None) finally: winKernel.virtualFreeEx(self.processHandle,internalInfo,0,winKernel.MEM_RELEASE) return unicode("%d.\t%s\t%s"%(curIndex+1,info.filetitle,info.filelength), errors="replace", encoding=locale.getlocale()[1])
def _get_name(self): hItem=winUser.sendMessage(self.windowHandle,CLM_GETSELECTION,0,0) internalBuf=winKernel.virtualAllocEx(self.processHandle,None,MAXITEMTEXTLEN,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winUser.sendMessage(self.windowHandle,CLM_GETITEMTEXT,hItem,internalBuf) buf=create_unicode_buffer(MAXITEMTEXTLEN) winKernel.readProcessMemory(self.processHandle,internalBuf,buf,MAXITEMTEXTLEN,None) text=buf.value statusMsgPtr=winUser.sendMessage(self.windowHandle,CLM_GETSTATUSMSG,hItem,0) if statusMsgPtr>0: buf2=create_unicode_buffer(MAXSTATUSMSGLEN) winKernel.readProcessMemory(self.processHandle,statusMsgPtr,buf2,MAXSTATUSMSGLEN,None) text="%s %s"%(text,buf2.value) winKernel.virtualFreeEx(self.processHandle,internalBuf,0,winKernel.MEM_RELEASE) return text
def _getSelectionOffsets(self): if self.obj.editAPIVersion>=1: charRange=CharRangeStruct() processHandle=self.obj.processHandle internalCharRange=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(charRange),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winUser.sendMessage(self.obj.windowHandle,EM_EXGETSEL,0, internalCharRange) winKernel.readProcessMemory(processHandle,internalCharRange,ctypes.byref(charRange),ctypes.sizeof(charRange),None) winKernel.virtualFreeEx(processHandle,internalCharRange,0,winKernel.MEM_RELEASE) return (charRange.cpMin,charRange.cpMax) else: start=ctypes.c_uint() end=ctypes.c_uint() res=winUser.sendMessage(self.obj.windowHandle,EM_GETSEL,ctypes.byref(start),ctypes.byref(end)) return start.value,end.value
def _getColumnLocationRaw(self, index): processHandle = self.processHandle # LVM_GETSUBITEMRECT requires a pointer to a RECT structure that will receive the subitem bounding rectangle information. localRect = RECT( # Returns the bounding rectangle of the entire item, including the icon and label. left=LVIR_LABEL, # According to Microsoft, top should be the one-based index of the subitem. # However, indexes coming from LVM_GETCOLUMNORDERARRAY are zero based. top=index) internalRect = winKernel.virtualAllocEx(processHandle, None, sizeof(localRect), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalRect, byref(localRect), sizeof(localRect), None) res = watchdog.cancellableSendMessage(self.windowHandle, LVM_GETSUBITEMRECT, self.IAccessibleChildID - 1, internalRect) if res: winKernel.readProcessMemory(processHandle, internalRect, ctypes.byref(localRect), ctypes.sizeof(localRect), None) finally: winKernel.virtualFreeEx(processHandle, internalRect, 0, winKernel.MEM_RELEASE) if res == 0: log.debugWarning( f"LVM_GETSUBITEMRECT failed for index {index} in list") return None # #8268: this might be a malformed rectangle # (i.e. with a left coordinate that is greater than the right coordinate). # This happens in Becky! Internet Mail, # as well in applications that expose zero width columns. left = localRect.left top = localRect.top right = localRect.right bottom = localRect.bottom if left > right: left = right if top > bottom: top = bottom return RectLTRB(left, top, right, bottom).toScreen(self.windowHandle).toLTWH()
def _getTextRange(self, start, end): if self.obj.editAPIVersion >= 2: bufLen = ((end - start) + 1) * 2 if self.obj.isWindowUnicode: textRange = TextRangeUStruct() else: textRange = TextRangeAStruct() textRange.chrg.cpMin = start textRange.chrg.cpMax = end processHandle = self.obj.processHandle internalBuf = winKernel.virtualAllocEx(processHandle, None, bufLen, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: textRange.lpstrText = internalBuf internalTextRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(textRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalTextRange, ctypes.byref(textRange), ctypes.sizeof(textRange), None) res = watchdog.cancellableSendMessage( self.obj.windowHandle, EM_GETTEXTRANGE, 0, internalTextRange) finally: winKernel.virtualFreeEx(processHandle, internalTextRange, 0, winKernel.MEM_RELEASE) buf = (ctypes.c_byte * bufLen)() winKernel.readProcessMemory(processHandle, internalBuf, buf, bufLen, None) finally: winKernel.virtualFreeEx(processHandle, internalBuf, 0, winKernel.MEM_RELEASE) if self.obj.isWindowUnicode or ( res > 1 and (buf[res] != 0 or buf[res + 1] != 0)): text = ctypes.cast(buf, ctypes.c_wchar_p).value else: text = unicode(ctypes.cast(buf, ctypes.c_char_p).value, errors="replace", encoding=locale.getlocale()[1]) else: text = self._getStoryText()[start:end] return text
def _getColumnContentRawOutProc(self, index: int) -> Optional[str]: """Retrieves text for a given column. Note that this method operates out of process and has to reserve memory inside a given application. As a consequence it may fail when reserved memory is above the range available for 32-bit processes. Use only when in process injection is not possible. """ buffer = None processHandle = self.processHandle internalItem = winKernel.virtualAllocEx(processHandle, None, sizeof(self.LVITEM), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: internalText = winKernel.virtualAllocEx(processHandle, None, CBEMAXSTRLEN * 2, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: item = self.LVITEM(iItem=self.IAccessibleChildID - 1, mask=LVIF_TEXT | LVIF_COLUMNS, iSubItem=index, pszText=internalText, cchTextMax=CBEMAXSTRLEN) winKernel.writeProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) len = watchdog.cancellableSendMessage( self.windowHandle, LVM_GETITEMTEXTW, (self.IAccessibleChildID - 1), internalItem) if len: winKernel.readProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) buffer = create_unicode_buffer(len) winKernel.readProcessMemory(processHandle, item.pszText, buffer, sizeof(buffer), None) finally: winKernel.virtualFreeEx(processHandle, internalText, 0, winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle, internalItem, 0, winKernel.MEM_RELEASE) return buffer.value if buffer else None
def _getColumnHeaderRaw(self,index): buffer=None processHandle=self.processHandle internalColumn=winKernel.virtualAllocEx(processHandle,None,sizeof(self.LVCOLUMN),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: internalText=winKernel.virtualAllocEx(processHandle,None,CBEMAXSTRLEN*2,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: column=self.LVCOLUMN(mask=LVCF_TEXT,iSubItem=index,pszText=internalText,cchTextMax=CBEMAXSTRLEN) winKernel.writeProcessMemory(processHandle,internalColumn,byref(column),sizeof(self.LVCOLUMN),None) res = watchdog.cancellableSendMessage(self.windowHandle,LVM_GETCOLUMNW, index, internalColumn) if res: winKernel.readProcessMemory(processHandle,internalColumn,byref(column),sizeof(self.LVCOLUMN),None) buffer=create_unicode_buffer(column.cchTextMax) winKernel.readProcessMemory(processHandle,column.pszText,buffer,sizeof(buffer),None) finally: winKernel.virtualFreeEx(processHandle,internalText,0,winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle,internalColumn,0,winKernel.MEM_RELEASE) return buffer.value if buffer else None
def _getCharFormat(self,offset): oldSel=self._getSelectionOffsets() if oldSel!=(offset,offset+1): self._setSelectionOffsets(offset,offset+1) if self.obj.isWindowUnicode: charFormatStruct=CharFormat2WStruct else: charFormatStruct=CharFormat2AStruct charFormat=charFormatStruct() charFormat.cbSize=ctypes.sizeof(charFormatStruct) processHandle=self.obj.processHandle internalCharFormat=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(charFormat),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(processHandle,internalCharFormat,ctypes.byref(charFormat),ctypes.sizeof(charFormat),None) winUser.sendMessage(self.obj.windowHandle,EM_GETCHARFORMAT,SCF_SELECTION, internalCharFormat) winKernel.readProcessMemory(processHandle,internalCharFormat,ctypes.byref(charFormat),ctypes.sizeof(charFormat),None) winKernel.virtualFreeEx(processHandle,internalCharFormat,0,winKernel.MEM_RELEASE) if oldSel!=(offset,offset+1): self._setSelectionOffsets(oldSel[0],oldSel[1]) return charFormat
def _getColumnOrderArrayRawOutProc(self, columnCount: int) -> Optional[ctypes.Array]: coa = (ctypes.c_int * columnCount)() processHandle=self.processHandle internalCoa=winKernel.virtualAllocEx(processHandle,None,sizeof(coa),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalCoa,byref(coa),sizeof(coa),None) res = watchdog.cancellableSendMessage( self.windowHandle, LVM_GETCOLUMNORDERARRAY, columnCount, internalCoa ) if res: winKernel.readProcessMemory(processHandle,internalCoa,byref(coa),sizeof(coa),None) else: log.debugWarning("LVM_GETCOLUMNORDERARRAY failed for list") finally: winKernel.virtualFreeEx(processHandle,internalCoa,0,winKernel.MEM_RELEASE) return coa
def _get_lvAppImageID(self): item=LVItemStruct(iItem=self.IAccessibleChildID-1,mask=LVIF_IMAGE) (processID,threadID)=winUser.getWindowThreadProcessID(self.windowHandle) processHandle=self.processHandle internalItem=winKernel.virtualAllocEx(processHandle,None,sizeof(LVItemStruct),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(processHandle,internalItem,byref(item),sizeof(LVItemStruct),None) winUser.sendMessage(self.windowHandle,LVM_GETITEM,0,internalItem) dispInfo=NMLVDispInfoStruct() dispInfo.item=internalItem dispInfo.hdr.hwndFrom=self.windowHandle dispInfo.hdr.idFrom=self.windowControlID dispInfo.hdr.code=LVN_GETDISPINFO internalDispInfo=winKernel.virtualAllocEx(processHandle,None,sizeof(NMLVDispInfoStruct),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(processHandle,internalDispInfo,byref(dispInfo),sizeof(NMLVDispInfoStruct),None) winUser.sendMessage(self.parent.parent.windowHandle,winUser.WM_NOTIFY,LVN_GETDISPINFO,internalDispInfo) winKernel.virtualFreeEx(processHandle,internalDispInfo,0,winKernel.MEM_RELEASE) winKernel.readProcessMemory(processHandle,internalItem,byref(item),sizeof(LVItemStruct),None) winKernel.virtualFreeEx(processHandle,internalItem,0,winKernel.MEM_RELEASE) return item.iImage
def _getColumnHeaderRawOutProc(self, index: int) -> Optional[str]: """Retrieves text of the header for the given column. Note that this method operates out of process and has to reserve memory inside a given application. As a consequence it may fail when reserved memory is above the range available for 32-bit processes. Use only when in process injection is not possible. """ buffer = None processHandle = self.processHandle internalColumn = winKernel.virtualAllocEx(processHandle, None, sizeof(self.LVCOLUMN), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: internalText = winKernel.virtualAllocEx(processHandle, None, CBEMAXSTRLEN * 2, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: column = self.LVCOLUMN(mask=LVCF_TEXT, iSubItem=index, pszText=internalText, cchTextMax=CBEMAXSTRLEN) winKernel.writeProcessMemory(processHandle, internalColumn, byref(column), sizeof(self.LVCOLUMN), None) res = watchdog.cancellableSendMessage(self.windowHandle, LVM_GETCOLUMNW, index, internalColumn) if res: winKernel.readProcessMemory(processHandle, internalColumn, byref(column), sizeof(self.LVCOLUMN), None) buffer = create_unicode_buffer(column.cchTextMax) winKernel.readProcessMemory(processHandle, column.pszText, buffer, sizeof(buffer), None) finally: winKernel.virtualFreeEx(processHandle, internalText, 0, winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle, internalColumn, 0, winKernel.MEM_RELEASE) return buffer.value if buffer else None
def _getColumnContentRaw(self, index): buffer=None processHandle=self.processHandle internalItem=winKernel.virtualAllocEx(processHandle,None,sizeof(self.LVITEM),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: internalText=winKernel.virtualAllocEx(processHandle,None,CBEMAXSTRLEN*2,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: item=self.LVITEM(iItem=self.IAccessibleChildID-1,mask=LVIF_TEXT|LVIF_COLUMNS,iSubItem=index,pszText=internalText,cchTextMax=CBEMAXSTRLEN) winKernel.writeProcessMemory(processHandle,internalItem,byref(item),sizeof(self.LVITEM),None) len = watchdog.cancellableSendMessage(self.windowHandle,LVM_GETITEMTEXTW, (self.IAccessibleChildID-1), internalItem) if len: winKernel.readProcessMemory(processHandle,internalItem,byref(item),sizeof(self.LVITEM),None) buffer=create_unicode_buffer(len) winKernel.readProcessMemory(processHandle,item.pszText,buffer,sizeof(buffer),None) finally: winKernel.virtualFreeEx(processHandle,internalText,0,winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle,internalItem,0,winKernel.MEM_RELEASE) return buffer.value if buffer else None
def _getPointFromOffset(self,offset): if self.obj.editAPIVersion==1 or self.obj.editAPIVersion>=3: processHandle=self.obj.processHandle internalP=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(PointLStruct),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: p=PointLStruct(0,0) winKernel.writeProcessMemory(processHandle,internalP,ctypes.byref(p),ctypes.sizeof(p),None) watchdog.cancellableSendMessage(self.obj.windowHandle,EM_POSFROMCHAR,internalP,offset) winKernel.readProcessMemory(processHandle,internalP,ctypes.byref(p),ctypes.sizeof(p),None) finally: winKernel.virtualFreeEx(processHandle,internalP,0,winKernel.MEM_RELEASE) point=textInfos.Point(p.x,p.y) else: res=watchdog.cancellableSendMessage(self.obj.windowHandle,EM_POSFROMCHAR,offset,None) point=textInfos.Point(winUser.LOWORD(res),winUser.HIWORD(res)) (left,top,width,height)=self.obj.location point.x=point.x+left point.y=point.y+top return point
def _getPointFromOffset(self, offset): if self.obj.editAPIVersion == 1 or self.obj.editAPIVersion >= 3: processHandle = self.obj.processHandle internalP = winKernel.virtualAllocEx(processHandle, None, ctypes.sizeof(PointLStruct), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: p = PointLStruct(0, 0) winKernel.writeProcessMemory(processHandle, internalP, ctypes.byref(p), ctypes.sizeof(p), None) watchdog.cancellableSendMessage(self.obj.windowHandle, winUser.EM_POSFROMCHAR, internalP, offset) winKernel.readProcessMemory(processHandle, internalP, ctypes.byref(p), ctypes.sizeof(p), None) finally: winKernel.virtualFreeEx(processHandle, internalP, 0, winKernel.MEM_RELEASE) point = locationHelper.Point(p.x, p.y) else: res = watchdog.cancellableSendMessage(self.obj.windowHandle, winUser.EM_POSFROMCHAR, offset, None) point = locationHelper.Point(winUser.GET_X_LPARAM(res), winUser.GET_Y_LPARAM(res)) # A returned coordinate can be a negative value if # the specified character is not displayed in the edit control's client area. # If the specified index is greater than the index of the last character in the control, # the control returns -1. if point.x < 0 or point.y < 0: raise LookupError( "Point with client coordinates x=%d, y=%d not within client area of object" % (point.x, point.y)) try: return point.toScreen(self.obj.windowHandle) except WindowsError as e: raise LookupError( f"Couldn't convert point at offset {offset} to screen coordinates: {e.strerror}" )
def _getSelectionOffsets(self): if self.obj.editAPIVersion>=1: charRange=CharRangeStruct() processHandle=self.obj.processHandle internalCharRange=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(charRange),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: watchdog.cancellableSendMessage(self.obj.windowHandle,EM_EXGETSEL,0, internalCharRange) winKernel.readProcessMemory(processHandle,internalCharRange,ctypes.byref(charRange),ctypes.sizeof(charRange),None) finally: winKernel.virtualFreeEx(processHandle,internalCharRange,0,winKernel.MEM_RELEASE) #if self._needsWorkAroundEncoding(): # s,n = self._startEndInBytesToStartEndInUnicodeChars(charRange.cpMin,charRange.cpMax) # log.info('charRange.cpMin:%d charRange.cpMax:%d -> s:%d n:%d' % (charRange.cpMin,charRange.cpMax,s,n)) # return (s,n) return (charRange.cpMin,charRange.cpMax) else: start=ctypes.c_uint() end=ctypes.c_uint() res=watchdog.cancellableSendMessage(self.obj.windowHandle,EM_GETSEL,ctypes.byref(start),ctypes.byref(end)) return start.value,end.value
def _getFormatFieldAndOffsets(self,offset,formatConfig,calculateOffsets=True): style=watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_GETSTYLEAT,offset,0) if calculateOffsets: #we need to manually see how far the style goes, limit to line lineStart,lineEnd=self._getLineOffsets(offset) startOffset=offset while startOffset>lineStart: curStyle=watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_GETSTYLEAT,startOffset-1,0) if curStyle==style: startOffset-=1 else: break endOffset=offset+1 while endOffset<lineEnd: curStyle=watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_GETSTYLEAT,endOffset,0) if curStyle==style: endOffset+=1 else: break else: startOffset,endOffset=(self._startOffset,self._endOffset) formatField=textInfos.FormatField() if formatConfig["reportFontName"]: #To get font name, We need to allocate memory with in Scintilla's process, and then copy it out fontNameBuf=ctypes.create_string_buffer(32) internalBuf=winKernel.virtualAllocEx(self.obj.processHandle,None,len(fontNameBuf),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_STYLEGETFONT,style, internalBuf) winKernel.readProcessMemory(self.obj.processHandle,internalBuf,fontNameBuf,len(fontNameBuf),None) finally: winKernel.virtualFreeEx(self.obj.processHandle,internalBuf,0,winKernel.MEM_RELEASE) formatField["font-name"]=fontNameBuf.value.decode("utf-8") if formatConfig["reportFontSize"]: formatField["font-size"]="%spt"%watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_STYLEGETSIZE,style,0) if formatConfig["reportLineNumber"]: formatField["line-number"]=self._getLineNumFromOffset(offset)+1 if formatConfig["reportFontAttributes"]: formatField["bold"]=bool(watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_STYLEGETBOLD,style,0)) formatField["italic"]=bool(watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_STYLEGETITALIC,style,0)) formatField["underline"]=bool(watchdog.cancellableSendMessage(self.obj.windowHandle,SCI_STYLEGETUNDERLINE,style,0)) return formatField,(startOffset,endOffset)
def _getCharFormat(self,range): oldSel=self.obj.ITextSelectionObject.duplicate if not (oldSel.start==range.start and oldSel.end==range.end): self.obj.ITextSelectionObject.start=range.start self.obj.ITextSelectionObject.end=range.end if self.obj.isWindowUnicode: charFormatStruct=CharFormat2WStruct else: charFormatStruct=CharFormat2AStruct charFormat=charFormatStruct() charFormat.cbSize=ctypes.sizeof(charFormatStruct) processHandle=self.obj.processHandle internalCharFormat=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(charFormat),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(processHandle,internalCharFormat,ctypes.byref(charFormat),ctypes.sizeof(charFormat),None) winUser.sendMessage(self.obj.windowHandle,EM_GETCHARFORMAT,SCF_SELECTION, internalCharFormat) winKernel.readProcessMemory(processHandle,internalCharFormat,ctypes.byref(charFormat),ctypes.sizeof(charFormat),None) winKernel.virtualFreeEx(processHandle,internalCharFormat,0,winKernel.MEM_RELEASE) if not (oldSel.start==range.start and oldSel.end==range.end): self.obj.ITextSelectionObject.start=oldSel.start self.obj.ITextSelectionObject.end=oldSel.end return charFormat
def _get_name(self): curIndex = winUser.sendMessage(self.appModule.hwndWinamp, WM_WA_IPC, -1, IPC_PLAYLIST_GET_NEXT_SELECTED) if curIndex < 0: return None info = fileinfo2W() info.fileindex = curIndex internalInfo = winKernel.virtualAllocEx(self.processHandle, None, sizeof(info), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(self.processHandle, internalInfo, byref(info), sizeof(info), None) winUser.sendMessage(self.windowHandle, WM_WA_IPC, IPC_PE_GETINDEXTITLE, internalInfo) winKernel.readProcessMemory(self.processHandle, internalInfo, byref(info), sizeof(info), None) winKernel.virtualFreeEx(self.processHandle, internalInfo, 0, winKernel.MEM_RELEASE) return str("%d.\t%s\t%s" % (curIndex + 1, info.filetitle, info.filelength))
def _getColumnLocationRaw(self,index): assert index>0, "Invalid index: %d" % index processHandle=self.processHandle # LVM_GETSUBITEMRECT requires a pointer to a RECT structure that will receive the subitem bounding rectangle information. localRect=RECT( left=LVIR_LABEL, # Returns the bounding rectangle of the entire item, including the icon and label top=index # The one-based index of the subitem ) internalRect=winKernel.virtualAllocEx(processHandle,None,sizeof(localRect),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle,internalRect,byref(localRect),sizeof(localRect),None) watchdog.cancellableSendMessage(self.windowHandle,LVM_GETSUBITEMRECT, (self.IAccessibleChildID-1), internalRect) winKernel.readProcessMemory(processHandle,internalRect,byref(localRect),sizeof(localRect),None) finally: winKernel.virtualFreeEx(processHandle,internalRect,0,winKernel.MEM_RELEASE) # #8268: this might be a malformed rectangle # (i.e. with a left coordinate that is greather than the right coordinate). left = min(localRect.left, localRect.right) top = min(localRect.top, localRect.bottom) right = max(localRect.left, localRect.right) bottom = max(localRect.top, localRect.bottom) return RectLTRB(left, top, right, bottom).toScreen(self.windowHandle).toLTWH()
def _getColumnLocationRawOutProc(self, index: int) -> ctypes.wintypes.RECT: """Retrieves rectangle containing coordinates for a given column. Note that this method operates out of process and has to reserve memory inside a given application. As a consequence it may fail when reserved memory is above the range available for 32-bit processes. Use only when in process injection is not possible. """ processHandle = self.processHandle # LVM_GETSUBITEMRECT requires a pointer to a RECT structure that will receive the subitem bounding rectangle information. localRect = RECT( # Returns the bounding rectangle of the entire item, including the icon and label. left=LVIR_LABEL, # According to Microsoft, top should be the one-based index of the subitem. # However, indexes coming from LVM_GETCOLUMNORDERARRAY are zero based. top=index) internalRect = winKernel.virtualAllocEx(processHandle, None, sizeof(localRect), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalRect, byref(localRect), sizeof(localRect), None) res = watchdog.cancellableSendMessage(self.windowHandle, LVM_GETSUBITEMRECT, self.IAccessibleChildID - 1, internalRect) if res: winKernel.readProcessMemory(processHandle, internalRect, ctypes.byref(localRect), ctypes.sizeof(localRect), None) finally: winKernel.virtualFreeEx(processHandle, internalRect, 0, winKernel.MEM_RELEASE) if res == 0: log.debugWarning( f"LVM_GETSUBITEMRECT failed for index {index} in list") return None return localRect
def _getTextRange(self, start, end): bufLen = (end - start) + 1 textRange = TextRangeStruct() textRange.chrg.cpMin = start textRange.chrg.cpMax = end processHandle = self.obj.processHandle internalBuf = winKernel.virtualAllocEx(processHandle, None, bufLen, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: textRange.lpstrText = internalBuf internalTextRange = winKernel.virtualAllocEx( processHandle, None, ctypes.sizeof(textRange), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalTextRange, ctypes.byref(textRange), ctypes.sizeof(textRange), None) watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETTEXTRANGE, 0, internalTextRange) finally: winKernel.virtualFreeEx(processHandle, internalTextRange, 0, winKernel.MEM_RELEASE) buf = ctypes.create_string_buffer(bufLen) winKernel.readProcessMemory(processHandle, internalBuf, buf, bufLen, None) finally: winKernel.virtualFreeEx(processHandle, internalBuf, 0, winKernel.MEM_RELEASE) cp = watchdog.cancellableSendMessage(self.obj.windowHandle, SCI_GETCODEPAGE, 0, 0) if cp == SC_CP_UTF8: return unicode(buf.value, errors="replace", encoding="utf-8") else: return unicode(buf.value, errors="replace", encoding=locale.getlocale()[1])
def _getColumnContent(obj, col): import winKernel from NVDAObjects.IAccessible import sysListView32 # Borrowed from SysListView32 implementation. buffer=None processHandle=obj.processHandle sizeofLVITEM = ctypes.sizeof(sysListView32.LVITEM) internalItem=winKernel.virtualAllocEx(processHandle,None,sizeofLVITEM,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: internalText=winKernel.virtualAllocEx(processHandle,None,520,winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: item=sysListView32.LVITEM(iItem=obj.IAccessibleChildID-1,mask=sysListView32.LVIF_TEXT|sysListView32.LVIF_COLUMNS,iSubItem=col,pszText=internalText,cchTextMax=260) winKernel.writeProcessMemory(processHandle,internalItem,ctypes.byref(item),sizeofLVITEM,None) len = sendMessage(obj.windowHandle,sysListView32.LVM_GETITEMTEXTW, (obj.IAccessibleChildID-1), internalItem) if len: winKernel.readProcessMemory(processHandle,internalItem,ctypes.byref(item),sizeofLVITEM,None) buffer=ctypes.create_unicode_buffer(len) winKernel.readProcessMemory(processHandle,item.pszText,buffer,ctypes.sizeof(buffer),None) finally: winKernel.virtualFreeEx(processHandle,internalText,0,winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle,internalItem,0,winKernel.MEM_RELEASE) return buffer.value if buffer else None
def _getColumnOrderArrayRawOutProc( self, columnCount: int) -> Optional[ctypes.Array]: """Retrieves a list of column indexes for a given list control. See `_getColumnOrderArrayRaw` for more comments. Note that this method operates out of process and has to reserve memory inside a given application. As a consequence it may fail when reserved memory is above the range available for 32-bit processes. Use only when in process injection is not possible. """ coa = (ctypes.c_int * columnCount)() processHandle = self.processHandle internalCoa = winKernel.virtualAllocEx(processHandle, None, sizeof(coa), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: winKernel.writeProcessMemory(processHandle, internalCoa, byref(coa), sizeof(coa), None) # The meaning of the return value depends on the message sent, for LVM_GETCOLUMNORDERARRAY, # it returns nonzero if successful, or 0 otherwise. # https://docs.microsoft.com/en-us/windows/win32/controls/lvm-getcolumnorderarray#return-value res = watchdog.cancellableSendMessage(self.windowHandle, LVM_GETCOLUMNORDERARRAY, columnCount, internalCoa) if res: winKernel.readProcessMemory(processHandle, internalCoa, byref(coa), sizeof(coa), None) else: coa = None log.debugWarning( f"LVM_GETCOLUMNORDERARRAY failed for list. " f"Windows Error: {ctypes.GetLastError()}, Handle: {self.windowHandle}" ) finally: winKernel.virtualFreeEx(processHandle, internalCoa, 0, winKernel.MEM_RELEASE) return coa
def _getPointFromOffset(self,offset): if self.obj.editAPIVersion==1 or self.obj.editAPIVersion>=3: processHandle=self.obj.processHandle internalP=winKernel.virtualAllocEx(processHandle,None,ctypes.sizeof(PointLStruct),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) try: p=PointLStruct(0,0) winKernel.writeProcessMemory(processHandle,internalP,ctypes.byref(p),ctypes.sizeof(p),None) watchdog.cancellableSendMessage(self.obj.windowHandle,winUser.EM_POSFROMCHAR,internalP,offset) winKernel.readProcessMemory(processHandle,internalP,ctypes.byref(p),ctypes.sizeof(p),None) finally: winKernel.virtualFreeEx(processHandle,internalP,0,winKernel.MEM_RELEASE) point=textInfos.Point(p.x,p.y) else: res=watchdog.cancellableSendMessage(self.obj.windowHandle,winUser.EM_POSFROMCHAR,offset,None) point=textInfos.Point(winUser.GET_X_LPARAM(res),winUser.GET_Y_LPARAM(res)) # A returned coordinate can be a negative value if # the specified character is not displayed in the edit control's client area. # If the specified index is greater than the index of the last character in the control, # the control returns -1. if point.x <0 or point.y <0: raise LookupError("Point with client coordinates x=%d, y=%d not within client area of object" % (point.x, point.y)) point.x, point.y = winUser.ClientToScreen(self.obj.windowHandle, point.x, point.y) return point
def getListGroupInfo(windowHandle,groupIndex): (processID,threadID)=winUser.getWindowThreadProcessID(windowHandle) processHandle=oleacc.GetProcessHandleFromHwnd(windowHandle) localInfo=LVGROUP() localInfo.cbSize=sizeof(LVGROUP) localInfo.mask=LVGF_HEADER|LVGF_FOOTER|LVGF_STATE|LVGF_ALIGN|LVGF_GROUPID localInfo.stateMask=0xffffffff remoteInfo=winKernel.virtualAllocEx(processHandle,None,sizeof(LVGROUP),winKernel.MEM_COMMIT,winKernel.PAGE_READWRITE) winKernel.writeProcessMemory(processHandle,remoteInfo,byref(localInfo),sizeof(LVGROUP),None) messageRes=winUser.sendMessage(windowHandle,LVM_GETGROUPINFOBYINDEX,groupIndex,remoteInfo) winKernel.readProcessMemory(processHandle,remoteInfo,byref(localInfo),sizeof(LVGROUP),None) winKernel.virtualFreeEx(processHandle,remoteInfo,0,winKernel.MEM_RELEASE) localHeader=create_unicode_buffer(localInfo.cchHeader) winKernel.readProcessMemory(processHandle,localInfo.pszHeader,localHeader,localInfo.cchHeader*2,None) localFooter=create_unicode_buffer(localInfo.cchFooter) winKernel.readProcessMemory(processHandle,localInfo.pszFooter,localFooter,localInfo.cchFooter*2,None) winKernel.closeHandle(processHandle) if messageRes==1: return dict(header=localHeader.value,footer=localFooter.value,groupID=localInfo.iGroupId,state=localInfo.state,uAlign=localInfo.uAlign,groupIndex=groupIndex) else: return None
def _getColumnContentRaw(self, index): buffer = None processHandle = self.processHandle internalItem = winKernel.virtualAllocEx(processHandle, None, sizeof(self.LVITEM), winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: internalText = winKernel.virtualAllocEx(processHandle, None, CBEMAXSTRLEN * 2, winKernel.MEM_COMMIT, winKernel.PAGE_READWRITE) try: item = self.LVITEM(iItem=self.IAccessibleChildID - 1, mask=LVIF_TEXT | LVIF_COLUMNS, iSubItem=index, pszText=internalText, cchTextMax=CBEMAXSTRLEN) winKernel.writeProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) len = watchdog.cancellableSendMessage( self.windowHandle, LVM_GETITEMTEXTA, (self.IAccessibleChildID - 1), internalItem) if len: winKernel.readProcessMemory(processHandle, internalItem, byref(item), sizeof(self.LVITEM), None) buffer = create_string_buffer(len) winKernel.readProcessMemory(processHandle, item.pszText, buffer, sizeof(buffer), None) finally: winKernel.virtualFreeEx(processHandle, internalText, 0, winKernel.MEM_RELEASE) finally: winKernel.virtualFreeEx(processHandle, internalItem, 0, winKernel.MEM_RELEASE) if buffer: colContentBytes = buffer.value left, top, width, height = self._getColumnLocationRaw(index) displayedColContent = DisplayModelTextInfo( self, getattr(textInfos, "Rect", locationHelper.RectLTRB)(left, top, left + width, top + height)).text COL_INCOMPLETE_END = "..." if u'\uffff' in displayedColContent: displayedColContent = [] else: displayedColContent = displayedColContent.split(" ") if (displayedColContent[-1] == COL_INCOMPLETE_END or displayedColContent[-1].endswith(COL_INCOMPLETE_END) or not displayedColContent[-1]): displayedColContent = displayedColContent[:-1] for encoding in self.POSSIBLE_ENCODINGS: try: decodedColContent = colContentBytes.decode(encoding) if self._displayedContentMatchesRetrieved( displayedColContent, decodedColContent): return decodedColContent continue except UnicodeDecodeError: continue try: return colContentBytes.decode("utf8") except UnicodeDecodeError: return colContentBytes.decode("unicode_escape") else: return None