def getStatusBarItems(self, hwnd, buf_len=512): """If success, return statusbar texts like list of strings. Otherwise return either '>>> No process ! <<<' or '>>> No parts ! <<<'. Mandatory argument: handle of statusbar. Option argument: length of text buffer.""" pid = GetWindowThreadProcessId(hwnd)[1] process = _kernel32.OpenProcess( PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, False, pid) res_val = ['>>> No process ! <<<'] if process: parts = win32guiSendMessage(hwnd, SB_GETPARTS, 0, 0) partList = [] res_val = ['>>> No parts ! <<<'] if parts > 0: remBuf = _kernel32.VirtualAllocEx(process, None, buf_len, MEM_COMMIT, PAGE_READWRITE) locBuf = create_unicode_buffer(buf_len) for item in range(parts): win32guiSendMessage(hwnd, SB_GETTEXTW, item, remBuf) _kernel32.ReadProcessMemory(process, remBuf, locBuf, buf_len, None) #copy remBuf to locBuf partList.append(locBuf.value) res_val = partList _kernel32.VirtualFreeEx(process, remBuf, 0, MEM_RELEASE) CloseHandle(process) return res_val
def SelectHwnd(self, hwnd): if hwnd is None: self.Unselect() return _, pid = GetWindowThreadProcessId(hwnd) item, cookie = self.GetFirstChild(self.root) while self.GetPyData(item) != pid: item, cookie = self.GetNextChild(self.root, cookie) if not item.IsOk(): return chain = [hwnd] rootHwnd = GetAncestor(hwnd, GA_ROOT) tmp = hwnd while tmp != rootHwnd: tmp = GetAncestor(tmp, GA_PARENT) chain.append(tmp) lastItem = item for child in chain[::-1]: self.Expand(item) item, cookie = self.GetFirstChild(lastItem) while self.GetPyData(item) != child: item, cookie = self.GetNextChild(lastItem, cookie) if not item.IsOk(): return lastItem = item self.SelectItem(lastItem)
def AppendPrograms(self): self.pids.clear() processes = EnumProcesses() # get PID list for pid in processes: self.pids[pid] = [] hwnds = GetTopLevelWindowList(self.includeInvisible) for hwnd in hwnds: pid = GetWindowThreadProcessId(hwnd)[1] if pid == eg.processId: continue self.pids[pid].append(hwnd) for pid in processes: if len(self.pids[pid]) == 0: continue iconIndex = 0 for hwnd in self.pids[pid]: icon = GetHwndIcon(hwnd) if icon: iconIndex = self.imageList.AddIcon(icon) break exe = GetProcessName(pid) item = self.AppendItem(self.root, exe) self.SetItemHasChildren(item, True) self.SetPyData(item, pid) self.SetItemImage(item, iconIndex, which=wx.TreeItemIcon_Normal)
def getListViewItems(self, hwnd): col = LVCOLUMN() col.mask = LVCF_FMT | LVCF_IMAGE | LVCF_ORDER | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH pid = GetWindowThreadProcessId(hwnd)[1] hProcHnd = _kernel32.OpenProcess(PROCESS_ALL_ACCESS, False, pid) pLVI = _kernel32.VirtualAllocEx(hProcHnd, 0, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) col.cchTextMax = 2000 col.pszText = pLVI + sizeof(col) + 1 ret = _kernel32.WriteProcessMemory(hProcHnd, pLVI, addressof(col), sizeof(col), 0) if not ret: raise WinError() retval = 1 col_count = 0 while retval: # Columns enumeration try: retval = win32guiSendMessage(hwnd, LVM_GETCOLUMN, col_count, pLVI) except: retval = 0 raise col_count += 1 pBuffer = _kernel32.VirtualAllocEx(hProcHnd, 0, 4096, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) lvitem_str = 20 * "\x00" + pack_int(pBuffer) + pack_int( 4096) + 8 * "\x00" lvitem_buffer = create_string_buffer(lvitem_str) num_items = win32guiSendMessage(hwnd, LVM_GETITEMCOUNT) res = [] for column_index in range(col_count): lvitem_buffer.__setslice__( 8, 12, pack_int(column_index)) #column index increment _kernel32.WriteProcessMemory(hProcHnd, pLVI, addressof(lvitem_buffer), sizeof(lvitem_buffer), 0) target_buff = create_string_buffer(4096) item_texts = [] for item_index in range(num_items): if self.only_sel: if not win32guiSendMessage(hwnd, LVM_GETITEMSTATE, item_index, LVIS_SELECTED): continue win32guiSendMessage(hwnd, LVM_GETITEMTEXT, item_index, pLVI) _kernel32.ReadProcessMemory(hProcHnd, pBuffer, addressof(target_buff), 4096, 0) item_texts.append(target_buff.value) res.append(item_texts) _kernel32.VirtualFreeEx(hProcHnd, pBuffer, 0, MEM_RELEASE) _kernel32.VirtualFreeEx(hProcHnd, pLVI, 0, MEM_RELEASE) CloseHandle(hProcHnd) return map( list, zip(*res)) #Transposing Two-Dimensional Arrays by Steve Holden
def OnDrag(self, event): # get the mouse coordinates point = GetCursorPos() # find the window under cursor hwnd = BestWindowFromPoint(point) # do we have targeted a new window? if hwnd != self.lastTarget: if self.lastTarget is not None: # unhighlight previous window HighlightWindow(self.lastTarget) _, pid = GetWindowThreadProcessId(hwnd) if pid == eg.processId: self.lastTarget = None else: HighlightWindow(hwnd) self.lastTarget = hwnd event.Skip()
def OnSelectionChanged(self, event): event.Skip() tree = self.tree item = tree.GetSelection() if not item.IsOk(): return hwnd = tree.GetPyData(item) eg.PrintDebugNotice("HWND:", hwnd) if tree.GetItemParent(item) == tree.root: # only selected a program pid = hwnd hwnd = None else: pid = GetWindowThreadProcessId(hwnd)[1] if pid == self.lastPid and hwnd == self.lastHwnd: return self.lastPid = pid self.lastHwnd = hwnd exe = GetProcessName(pid) def SetOption(flag, option, value): checkBox, textCtrl = option checkBox.SetValue(flag) textCtrl.SetValue(value) textCtrl.Enable(flag) rootHwnd = None options = self.options SetOption(bool(exe), options[0], exe) if hwnd is not None: rootHwnd = GetAncestor(hwnd, GA_ROOT) targetWinName = GetWindowText(rootHwnd) targetWinClass = GetClassName(rootHwnd) SetOption(True, options[1], targetWinName) SetOption(True, options[2], targetWinClass) else: SetOption(False, options[1], "") SetOption(False, options[2], "") if rootHwnd is not None and rootHwnd != hwnd: targetWinName = GetWindowText(hwnd) targetWinClass = GetClassName(hwnd) SetOption(True, options[3], targetWinName) SetOption(True, options[4], targetWinClass) searchHwnd = hwnd data = [ child for child in GetWindowChildsList( rootHwnd, tree.includeInvisible ) if ( GetClassName(child) == targetWinClass and GetWindowText(child) == targetWinName ) ] try: count = data.index(searchHwnd) + 1 except: count = 0 else: SetOption(False, options[3], "") SetOption(False, options[4], "") if rootHwnd is not None: data = [ hwnd for hwnd in GetTopLevelWindowList(tree.includeInvisible) # NOQA if ( GetClassName(hwnd) == targetWinClass and GetWindowText(hwnd) != targetWinName and GetWindowThreadProcessId(hwnd)[1] == pid ) ] count = len(data) else: count = 0 SetOption(count > 0, options[5], count or 1)