11

I am developing a python application and I want to get the HWND of each open windows. I need the name of the windows and the HWND to filter the list to manage some specifics windows, moving and resizing them.

I have tried to do it myself looking information around but I did not get the correct piece of code. I tried with this code but I only get the title of each windows (that is great), but I need the HWND too.

import ctypes
import win32gui
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

titles = []
def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)
        titles.append((hwnd, buff.value))
    return True
EnumWindows(EnumWindowsProc(foreach_window), 0)

for i in range(len(titles)):
    print(titles)[i]

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)

There is a error here:

win32gui.MoveWindow((titles)[5][0], 0, 0, 760, 500, True)
 TypeError: The object is not a PyHANDLE object
martineau
  • 119,623
  • 25
  • 170
  • 301
user1618465
  • 1,813
  • 2
  • 32
  • 58
  • 6
    SO isn't a supermarket where you can go with a shopping list and let others do your work for you. What have you done yourself? – Difusio Feb 01 '13 at 18:59
  • 1
    @Difusio: I have tried to do it myself looking information around but I did not get the correct piece of code. I tried with this http://sjohannes.wordpress.com/2012/03/23/win32-python-getting-all-window-titles/ but I only get the title of each windows (that is great), but I need the HWND too. – user1618465 Feb 01 '13 at 19:04
  • 4
    What me means is, please _post_ the _code_ you wrote (either based on what you found, or even copied) along with _errors_ you received or other things you've done. – g.d.d.c Feb 01 '13 at 19:11
  • 5
    Please don't edit your question to include the fixes from the answer, because that makes your question useless to anyone who comes along later and wants to understand it. – abarnert Feb 01 '13 at 20:03

4 Answers4

34

You mixed up ctypes and win32gui.
The hwnd you've got is obtained via ctypes and is a LP_c_long object. That's why win32gui.MoveWindow didn't accept it. You should pass it to

ctypes.windll.user32.MoveWindow(titles[5][0], 0, 0, 760, 500, True)

If you want to use win32gui.MoveWindow, you can use python function as callback directly.
For example,

import win32gui

def enumHandler(hwnd, lParam):
    if win32gui.IsWindowVisible(hwnd):
        if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
            win32gui.MoveWindow(hwnd, 0, 0, 760, 500, True)

win32gui.EnumWindows(enumHandler, None)
nymk
  • 3,323
  • 3
  • 34
  • 36
  • great answer! made my browser window jump across three monitors :-D – ssc Apr 04 '14 at 12:57
  • 1
    Very useful clarification. Basically do not mix ctypes and win32gui. This simple line can save a lot of time :) – BeHappy Jul 16 '19 at 23:37
2

To get the handles of all available main windows you pass 0 to win32gui.EnumChildWindows then check to make sure the window has text of length longer than 0 (since you want only actual windows not hidden/temporary/popup/special windows).

Script and Compile
  • 1,286
  • 3
  • 10
  • 17
  • i want to do the opposite i.e get the window text whenever a popup/prompt appears e.g. UAC Prompt. What should I do? because it returns none for uac prompt. – Mujtaba Dec 31 '22 at 04:19
2

Your problem (now that martineau has fixed your original problem of not storing the HWND values at all) is that you're trying to mix ctypes and win32gui.

You can do that if you know what you're doing—but if not, just don't do it.

If you want to get window handles you can use with win32gui, use win32gui.EnumWindows instead of calling the raw function out of the user32 DLL.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

Just modify the piece of code for getting all titles so it does something like this:

titles.append((hwnd, buff.value))

The titles list will then be a list of tuples containing the HWND and the title text.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you very much but this is that happends: (<__main__.LP_c_long object at 0x022A9030>, u'Spotify') I dont get the int that later i will use with win32gui.MoveWindow(hwnd, 0, 0, 760, 500, True). How can I get this number?? – user1618465 Feb 01 '13 at 19:34
  • I have updated the post with your help. But I cant use the HWND values in the win32gui.MoveWindow() function. Why? – user1618465 Feb 01 '13 at 19:42
  • 1
    In theory you should be able to use `hwnd.contents.value` to get the integer value, but on my system that hangs for unknown reasons. The problem is you're mixing `ctypes` `PyHANDLE` and `win32` `c_long` types. I think @abarnert has the right idea to avoid mixing them. – martineau Feb 01 '13 at 21:02
  • @martineau the problem seems to be that the second parameter to WINFUNCTPYE should be HWND, not a pointer. – Marc Stober Jul 07 '22 at 12:14