I am using Windows 10.
I want to get the "handle" (or hWnd
) of the process after using subprocess.Popen
.
What can I do?
In order to obtain a window handle (hWnd
), you need to use an additional library pywin32 to use the Windows API functions.
import subprocess
import win32api
import win32con
import win32process
# Start a process
process = subprocess.Popen(["notepad.exe"])
# Get the process ID (PID)
pid = process.pid
print(f"Process ID (PID): {pid}")
# Get the process handle
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
# Get the window handle (hWnd) associated with the process
hWnd = win32process.GetWindowThreadProcessId(handle)
print(f"Window Handle (hWnd): {hWnd}")