我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用win32process.GetWindowThreadProcessId()。
def get_hwnds(pid): """return a list of window handlers based on it process id""" def callback(hwnd, hwnds): if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd): _, found_pid = win32process.GetWindowThreadProcessId(hwnd) if found_pid == pid: hwnds.append(hwnd) return True hwnds = [] win32gui.EnumWindows(callback, hwnds) return hwnds
def __init__(self, window_name=None, exe_file=None, exclude_border=True): hwnd = 0 # first check window_name if window_name is not None: hwnd = win32gui.FindWindow(None, window_name) if hwnd == 0: def callback(h, extra): if window_name in win32gui.GetWindowText(h): extra.append(h) return True extra = [] win32gui.EnumWindows(callback, extra) if extra: hwnd = extra[0] if hwnd == 0: raise WindowsAppNotFoundError("Windows Application <%s> not found!" % window_name) # check exe_file by checking all processes current running. elif exe_file is not None: pid = find_process_id(exe_file) if pid is not None: def callback(h, extra): if win32gui.IsWindowVisible(h) and win32gui.IsWindowEnabled(h): _, p = win32process.GetWindowThreadProcessId(h) if p == pid: extra.append(h) return True return True extra = [] win32gui.EnumWindows(callback, extra) #TODO: get main window from all windows. if extra: hwnd = extra[0] if hwnd == 0: raise WindowsAppNotFoundError("Windows Application <%s> is not running!" % exe_file) # if window_name & exe_file both are None, use the screen. if hwnd == 0: hwnd = win32gui.GetDesktopWindow() self.hwnd = hwnd self.exclude_border = exclude_border
def get_process_name(self, event): '''Acquire the process name from the window handle for use in the log filename. ''' if os.name == 'nt': hwnd = event.Window try: threadpid, procpid = win32process.GetWindowThreadProcessId(hwnd) # PROCESS_QUERY_INFORMATION (0x0400) or PROCESS_VM_READ (0x0010) or PROCESS_ALL_ACCESS (0x1F0FFF) mypyproc = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, procpid) procname = win32process.GetModuleFileNameEx(mypyproc, 0) return procname except: # this happens frequently enough - when the last event caused the closure of the window or program # so we just return a nice string and don't worry about it. return "noprocname" elif os.name == 'posix': return to_unicode(event.WindowProcName)
def get_process_name(self, event): '''Acquire the process name from the window handle for use in the log filename. ''' if os.name == 'nt': hwnd = event.Window try: threadpid, procpid = win32process.GetWindowThreadProcessId(hwnd) # PROCESS_QUERY_INFORMATION (0x0400) or PROCESS_VM_READ (0x0010) or PROCESS_ALL_ACCESS (0x1F0FFF) mypyproc = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, procpid) procname = win32process.GetModuleFileNameEx(mypyproc, 0) return procname except: # this happens frequently enough - when the last event caused the closure of the window or program # so we just return a nice string and don't worry about it. return "noprocname" elif os.name == 'posix': #this line was modified to get 64 bit working return str(event.WindowProcName)
def GetProcessNameFromHwnd(self, hwnd): '''Acquire the process name from the window handle for use in the log filename. ''' threadpid, procpid = win32process.GetWindowThreadProcessId(hwnd) # PROCESS_QUERY_INFORMATION (0x0400) or PROCESS_VM_READ (0x0010) or PROCESS_ALL_ACCESS (0x1F0FFF) mypyproc = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, procpid) procname = win32process.GetModuleFileNameEx(mypyproc, 0) return procname
def getpid(self): tmp, _pid = win32process.GetWindowThreadProcessId(self.hwnd) return _pid
def get_app_path(hwnd) -> Optional[str]: """Get application path given hwnd.""" path = None _, pid = win32process.GetWindowThreadProcessId(hwnd) for p in c.query('SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = %s' % str(pid)): path = p.ExecutablePath break return path
def get_app_name(hwnd) -> Optional[str]: """Get application filename given hwnd.""" name = None _, pid = win32process.GetWindowThreadProcessId(hwnd) for p in c.query('SELECT Name FROM Win32_Process WHERE ProcessId = %s' % str(pid)): name = p.Name break return name
def _get_aimp_exe_path(self): """Find the AIMP executable path given its window handler. :raises RuntimeError: The AIMP executable path cannot be found. :rtype: None """ win_thread_proc_id = win32process.GetWindowThreadProcessId(self._aimp_window) pwnd = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, win_thread_proc_id[1]) self._aimp_exe_path = win32process.GetModuleFileNameEx(pwnd, None) if not self._aimp_exe_path: raise RuntimeError('Unable to retrieve the AIMP executable.')
def __close__(self, hwnd, dummy): """ EnumWindows callback - sends WM_CLOSE to any window owned by this process. """ TId, PId = win32process.GetWindowThreadProcessId(hwnd) if PId == self.PId: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
def GetHWndByProcId(cls, procid): def callback(hwnd, procinfo): pid = procinfo.get("procid", None) t, pid_2 = win32process.GetWindowThreadProcessId(hwnd) #print pid,"==", find_pid if pid == pid_2: p_hwnd = win32gui.GetParent(hwnd) if p_hwnd == 0: # top window procinfo["hwnd"] = hwnd return True procinfo = { "procid": procid, "hwnd": None, } win32gui.EnumWindows(callback, procinfo) return procinfo["hwnd"]
def GetFocus(cls): curtid = win32api.GetCurrentThreadId() whd = win32gui.GetForegroundWindow() (tid, pid) = win32process.GetWindowThreadProcessId(whd) win32process.AttachThreadInput(curtid, tid,True) focus_whd = win32gui.GetFocus() win32process.AttachThreadInput(curtid, tid, False) return focus_whd
def updateWindowHandle(self, callback=None): def getSpotifyWindowHandle(handle, extra): pid = GetWindowThreadProcessId(handle)[1] processName = psutil.Process(pid).name().lower() songMatch = SONG_DATA_RE.match(GetWindowText(handle)) if(SPOTIFY in processName and songMatch): self.windowHandle = handle ## Should really be a return False here to kill off the ## enumeration when a suitable handle is found, but that ## produces a weird 'Things have gone VERY wrong' error. ## See: http://docs.activestate.com/activepython/3.1/pywin32/win32gui__EnumWindows_meth.html EnumWindows(getSpotifyWindowHandle, None) ## Can't know which window will display the currently playing song ## information unless it's playing music. if(not self.windowHandle): self._findWindowHandleAttempts += 1 if(self._findWindowHandleAttempts > ATTEMPT_LIMIT): self.stopScraping() raise RuntimeError("No valid " + SPOTIFY + " windows available. Is it currently open and running (and not playing any ads)?") self.playSong() time.sleep(WAIT_TIME) ## Give Spotify a moment to start playing. self.updateWindowHandle() if(callback): callback()
def FX_GetForegroundWindow(): window_info = FX_WINDOW_INFO() window_info.Win32Window = win32gui.GetForegroundWindow() thread_process = win32process.GetWindowThreadProcessId(window_info.Win32Window) window_info.ProcessID = thread_process[1] window_info.ThreadID = thread_process[0] window_info.Title = win32gui.GetWindowText(window_info.Win32Window) return FX_Window(window_info)
def FX_CreateWindowFromElement(FXElement): win_info = FX_WINDOW_INFO() win_info.Win32Window = FXElement.GetNativeWindowHandle() if win_info.Win32Window == 0: return None thread_process = win32process.GetWindowThreadProcessId(win_info.Win32Window) win_info.ProcessID = thread_process[1] win_info.ThreadID = thread_process[0] win_info.Title = win32gui.GetWindowText(win_info.Win32Window) return FX_Window(win_info, FXElement)
def FindWindowByClassName(self, class_name): win_info = FX_WINDOW_INFO() win_info.Win32Window = win32gui.FindWindowEx(None, None, class_name, None) print(win_info.Win32Window) if win_info.Win32Window == 0: return None thread_process = win32process.GetWindowThreadProcessId(win_info.Win32Window) win_info.ProcessID = thread_process[1] win_info.ThreadID = thread_process[0] win_info.Title = win32gui.GetWindowText(win_info.Win32Window) return FX_Window(win_info)
def FindWindowByName(self, title_name): win_info = FX_WINDOW_INFO() win_info.Win32Window = win32gui.FindWindowEx(None, None, None, title_name) if win_info.Win32Window == 0: return None thread_process = win32process.GetWindowThreadProcessId(win_info.Win32Window) win_info.ProcessID = thread_process[1] win_info.ThreadID = thread_process[0] win_info.Title = win32gui.GetWindowText(win_info.Win32Window) return FX_Window(win_info)
def GetForegroundWindow(self): win_info = FX_WINDOW_INFO() win_info.Window = win32gui.GetForegroundWindow() thread_process = win32process.GetWindowThreadProcessId(win_info.Window) self.ProcessID = thread_process[1] self.ThreadID = thread_process[0] win_info.Title = win32gui.GetWindowText(win_info.Win32Window) return FX_Window(win_info)