Mike Driscoll wrote: > Hi, > > I am trying to get a handle on an external process (Internet Explorer 6 in > this case) that I open using win32process. I need the handle so that I can > make said process the top window. Here's what I've tried so far: > > <code> > > import win32process > import win32gui > > info = win32process.CreateProcess(None, proc, None, None, 0, > win32process.NORMAL_PRIORITY_CLASS, None, None, > win32process.STARTUPINFO()) > handle = info[0] > > # attempt to make Internet Explorer 6 the Foreground Window > win32gui.SetForegroundWindow(handle) > > </code> > > Unfortunately, this results in a traceback: > > Traceback (most recent call last): > File "<pyshell#12>", line 1, in ? > win32gui.SetForegroundWindow(info[2]) > error: (1400, 'SetForegroundWindow', 'Invalid window handle.') > > I can get the handle by doing this: > > hwnd = win32gui.FindWindow('IEFrame',None) > > But if there's multiple Explorer windows open, I may not get the window I > want. That's why I would like to create my own so I can have what amounts > to an "exclusive" handle to it. Any hints would be appreciated.
I thought I'd posted a How-Do-I? on this one, but obviously not. At any rate, here's the code I intended to post up. Hope it helps as a starting point: <code> import subprocess import time import win32con import win32gui import win32process 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 if __name__ == '__main__': notepad = subprocess.Popen ([r"notepad.exe"]) # # sleep to give the window time to appear # time.sleep (2.0) for hwnd in get_hwnds_for_pid (notepad.pid): print hwnd, "=>", win32gui.GetWindowText (hwnd) win32gui.SendMessage (hwnd, win32con.WM_CLOSE, 0, 0) </code> TJG _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32