Getting Pid and Details for Topmost Window

How could i detect info for the front most window in X?

To find out the window ID, try:

xprop -root|grep "_NET_CLIENT_LIST_STACKING(WINDOW): window id"

Window properties:

_NET_CLIENT_LIST_STACKING has bottom-to-top stacking order

One way to accomplish this is to parse the output of this command inside your application. The topmost window is the last on the list.

EDIT:

If you need to retrieve the process ID from the window ID, there's a small application here that shows how to do this trick. I successfully compiled it with:

g++ win_procid.cpp -o win_procid -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/libgtop-2.0 -lXtst -lgtop-2.0

I had to install the package libgtop2-dev because I didn't have it on my system.

Python Get windowtitle from Process ID or Process Name


import win32gui
import win32process
import psutil
import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

def getProcessIDByName():
qobuz_pids = []
process_name = "Qobuz.exe"

for proc in psutil.process_iter():
if process_name in proc.name():
qobuz_pids.append(proc.pid)

return qobuz_pids

def get_hwnds_for_pid(pid):
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 getWindowTitleByHandle(hwnd):
length = GetWindowTextLength(hwnd)
buff = ctypes.create_unicode_buffer(length + 1)
GetWindowText(hwnd, buff, length + 1)
return buff.value

def getQobuzHandle():
pids = getProcessIDByName()

for i in pids:
hwnds = get_hwnds_for_pid(i)
for hwnd in hwnds:
if IsWindowVisible(hwnd):
return hwnd


if __name__ == '__main__':
qobuz_handle = getQobuzHandle()

I solved it using this piece of code
With this I get the window handle of the qobuz window if it's open, else it's none

How can I get the process name of the current active window in windows with winapi?

You can use GetWindowThreadProcessId(), which takes in an HWND and outputs the ID of the window's owning process.

For example:

#include <tchar.h>

TCHAR wnd_title[256];
HWND hwnd = GetForegroundWindow(); // get handle of currently active window
GetWindowTextA(hwnd, wnd_title, 256);

DWORD dwPID;
GetWindowThreadProcessId(hwnd, &dwPID);

HANDLE Handle = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
dwPID
);
if (Handle)
{
TCHAR Buffer[MAX_PATH];
if (GetModuleFileNameEx(Handle, 0, Buffer, MAX_PATH))
{
_tprintf(_T("Path: %s"), Buffer);
// At this point, buffer contains the full path to the executable
}
CloseHandle(Handle);
}

How do I get a PID from a window title in windows OS using Python?

The GetWindowThreadProcessId function seems to do what you want for getting a PID from a HWND. And FindWindow is the usual way to find a window using its title. So the following gets a

import win32gui,win32process
def get_window_pid(title):
hwnd = win32gui.FindWindow(None, title)
threadid,pid = win32process.GetWindowThreadProcessId(hwnd)
return pid

I wonder if you should be trying to use UI Automation though if you are trying to drive the UI. I've not tried to use that via Python.

python why win32process.GetWindowThreadProcessId() PID returns a list

It's part of [GitHub]: mhammond/pywin32 - Python for Windows (pywin32) Extensions, which is a Python wrapper over WINAPIs.

  • [ActiveState.Docs]: win32process.GetWindowThreadProcessId (this is the closest that I could find of an official pywin32 doc) wraps [MS.Docs]: GetWindowThreadProcessId function
  • Returns a tuple consisting of 2 ints:

    • Thread ID (tid)
    • Process ID (pid)

Change (the relevant parts of) your code (not mandatory, just for readability) to:

tid, pid = win32process.GetWindowThreadProcessId(window)
active_window_path = psutil.Process(pid).exe()

.NET (C#): Getting child windows when you only have a process handle or PID?

If you don't mind using the Windows API, you could use EnumWindowsProc, and check each of the handles that that turns up using GetWindowThreadProcessId (to see that it's in your process), and then maybe IsWindowVisible, GetWindowCaption and GetWindowTextLength to determine which hWnd in your process is the one you want.

Though if you haven't used those functions before that approach will be a real pain, so hopefully there's a simpler way.



Related Topics



Leave a reply



Submit