Get Process Window

Hello,
I have a process handle. How can I get a HWND to it's main window?
I couldn't find any function in MSDN that does this.
Thanks
[165 byte] By [nemok] at [2007-11-20 10:37:55]
# 1 Re: Get Process Window
One soilution can be the following

Use EnumWindows (http://msdn2.microsoft.com/en-us/library/ms633497.aspx) to enumerate the top-level windows.
In EnumWindowsProc (http://msdn2.microsoft.com/en-us/library/ms633498.aspx) callback function call
GetWindowThreadProcessId (http://msdn2.microsoft.com/en-us/library/ms633522.aspx) to "filter" the window created in the given process.


[ Redirected thread ]
ovidiucucu at 2007-11-9 13:32:06 >
# 2 Re: Get Process Window
So I have to do it backwards from the window to the process...
nemok at 2007-11-9 13:33:16 >
# 3 Re: Get Process Window
Well, I think, there is not a kind of "GetProcessMainWindow" function because in a process can be created zero, one, or many (top-level) windows.
So... which can we say "that's the process main window" :) ;) ?

BTW. How did you get the process handle?
ovidiucucu at 2007-11-9 13:34:15 >
# 4 Re: Get Process Window
I am using EnumProcesses() to find a specific window.
I know we can't determine the main window. I thinking of something like GetAllProcessWindows()... :D
nemok at 2007-11-9 13:35:09 >
# 5 Re: Get Process Window
You should be able to do that in the loop.

VB.NET

For Each p as Process In Process.GetProcesses

If p.Handle.ToInt32 = YourPHandle AndAlso p.MainWindowHandle.ToInt32 <> 0 Then
MessageBox.Show(p.MainWindowHandle.ToInt32.ToString)
End If

Next
TT-n at 2007-11-9 13:36:18 >
# 6 Re: Get Process Window
Hello,

I have a process handle. How can I get a HWND to it's main window?
I couldn't find any function in MSDN that does this.

Thanks

Look also at ::GetGUIThreadInfo() ( http://msdn2.microsoft.com/en-us/library/ms633506.aspx). ;)

Cheers
golanshahar at 2007-11-9 13:37:15 >
# 7 Re: Get Process Window
Look also at ::GetGUIThreadInfo() ( http://msdn2.microsoft.com/en-us/library/ms633506.aspx). ;)
Does that help more to find out the process main window?
See my previous question (re)mark:
... which can we say "that's the process main window" :) ;) ?
ovidiucucu at 2007-11-9 13:38:19 >
# 8 Re: Get Process Window
Does that help more to find out the process main window?
See my previous question (re)mark:

Your comment is correct we cant, but that API can be an alternative for using ::EnumWindows(), you can get the active/focus window without enum the windows... then ::GetAncestor() can be used on the focus/active window... but no one can guarantee if it will lead to the Main Process window :D, UNLESS Op want to track specific process and not random one, so one look in Spy++ can help him know the windows structure of that specific process.

Cheers
golanshahar at 2007-11-9 13:39:22 >