[python-win32] Creating a process and getting a handle

Tim Roberts timr at probo.com
Wed Jan 23 02:14:09 CET 2008


Mike Driscoll wrote:
> 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. 

When you call CreateProcess, that window should automatically become the 
top window.  Are you saying that's not happening?  Have you tried 
creating a STARTUPINFO struct and filling in the wShowWindow element?

> 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)
>   

No, the error message is right.  A process handle is not the same as a 
window handle.  Indeed, a process need not have any windows at all.

> 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.
>   

One possibility is to enumerate through all of the top-level windows 
using EnumWindows, and for each window call GetWindowThreadProcessId to 
find the process ID associated with that window to find the one that 
matches your process.  Remember that the process ID is not the same as a 
process handle; the process ID is the third thing in the tuple 
CreateProcess returns.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list