Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32process.GetWindowThreadProcessId returning [0, 0] when ctypes.windll.user32.GetWindowThreadProcessId works fine #2457

Open
huntfx opened this issue Jan 9, 2025 · 0 comments
Labels

Comments

@huntfx
Copy link

huntfx commented Jan 9, 2025

Expected behavior and actual behavior

I've found a case where pywin32 fails to get a process ID when ctypes manages.
I've been running this code for 6+ years and have never seen it happen before, so I'm afraid the only way I know how to reproduce this is with Helldivers 2.

Passing the window handle of Helldivers 2 to win32process.GetWindowThreadProcessId to get its process ID will return [0, 0], but with ctypes.windll.user32.GetWindowThreadProcessId it returns the correct process ID.

For any other process these two values are always the same.

Steps to reproduce the problem

>>> def get_pid_ctypes(hwnd):
...     process_id = ctypes.c_ulong()
...     ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(process_id))
...     return process_id.value

>>> def get_pid_pywin32(hwnd):
...     return win32process.GetWindowThreadProcessId(hwnd)[1]

>>> hwnd = win32gui.FindWindow(None, 'HELLDIVERS™ 2')
>>> hwnd
5245634 
>>> get_pid_ctypes(hwnd)
210628 
>>> get_pid_pywin32(hwnd)
0
  1. Load Helldivers 2
  2. Get its window handle
  3. Attempt to get the PID via win32process.GetWindowThreadProcessId
  4. It returns [0, 0]

For the sake of linking issues, I created huntfx/MouseTracks#65 on my own project before finding that the ctypes method worked.

System information

Python version and distribution:

Python 3.7.9
Python 3.11.8

pywin32 version:

300 for 3.7
308 for 3.11

Windows Version:

10.0.19045 Build 19045

DLL locations:

C:\Users\Peter\AppData\Roaming\Python\Python311\site-packages\pywin32_system32\pywintypes311.dll
C:\Users\Peter\AppData\Roaming\Python\Python311\site-packages\pywin32_system32\pythoncom311.dll

@Avasam Avasam added the bug label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants