Gianfra
Gianfra

Reputation: 1149

win32gui findwindow doesn't work win7

I use the code below to activate the command promt:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python demo.py")
win32gui.SetForegroundWindow(hwn)

Prompt dei comandi is the italian word of command promt :) and demo.py is the name of the python file. It works perfectly with window xp but when i try the same code with win7 it doesn't work anymore. I also check the list of the visible windows with this code:

import win32gui
def window_enum_handler(hwnd, resultList):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
        resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def get_app_list(handles=[]):
    mlst=[]
    win32gui.EnumWindows(window_enum_handler, handles)
    for handle in handles:
        mlst.append(handle)
    return mlst


appwindows = get_app_list()
return appwindows
for i in appwindows:
   print i

In that list i can see: Prompt dei comandi - python demo.py What does it change from win xp to win7??

Thanks

Upvotes: 0

Views: 4029

Answers (2)

Gianfra
Gianfra

Reputation: 1149

It turned out that the only different was just on one space! i will explain my self: this works for win xp:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python demo.py")

this works for win7:

hwn = win32gui.FindWindow(None, "Prompt dei comandi - python  demo.py")

it sounds crazy but it does.

Upvotes: 1

cdonts
cdonts

Reputation: 9599

There's a couple of things that could be. But I think it's about ANSI and UNICODE. Try using:

hwn = win32gui.FindWindow(None, u"Prompt dei comandi - python demo.py")

Or maybe using ctypes:

from ctypes import windll

FindWindowW = windll.user32.FindWindowW
FindWindowA = windll.user32.FindWindowA

print FindWindowW(0, u"Prompt dei comandi - python demo.py")
print FindWindowA(0, "Prompt dei comandi - python demo.py")

Hope it helps.

Upvotes: 2

Related Questions