CreateProcess and window hwnd

G

Guest

Hello
I have a question: what API function (from core.dll) should I P/Invoke to
get window hwnd for created process if I have process handle? FindWindow
requires either classname or window title.
thanks for any tip
 
P

Paul G. Tobey [eMVP]

Since a given process might have any number of windows, there's no single
answer to your question. If you're trying to close all top level windows
for some process, you could use EnumWindows( functionname, processid ) and
have the enumeration callback (functionname), check the process ID returned
by GetWindowThreadProcessId() against the parameter. If equal, it could
post a WM_CLOSE to the window or whatever. However, this is going to be a
challenge to do in the Compact Framework, as there's no built-in support for
using callback-required API functions like EnumWindows. Search the archives
for "enumwindows" and see what you can find on that strategy.

You might alternatively try calling GetWindow with suitable parameters over
and over until all windows have been enumerated (after getting each window
handle, you'd call GetWindowThreadProcessId() and see if it's a window from
the process that you want).

Paul T.
 
G

Guest

so I have only one problem:
after I create process by Opennetcf function:
ProcessStartInfo psi = new ProcessStartInfo(player_path,filename);
Process p = Process.Start(psi);

I search for window hwnd by processid:
hwnd=WindowHelper.EnumerateTopWindows(p.Handle);

public static IntPtr EnumerateTopWindows(IntPtr processhandle)
{
ArrayList winList = new ArrayList();
IntPtr th=IntPtr.Zero;
IntPtr hwnd = IntPtr.Zero;
IntPtr hwndProc=IntPtr.Zero;
System.Text.StringBuilder sb = null;
// Get the first window
hwnd = Win32Window.GetActiveWindow();
hwnd = Win32Window.GetWindow(hwnd,Win32Window.GetWindowParam.GW_HWNDFIRST);
while(hwnd != IntPtr.Zero)
{
IntPtr parentWin = GetParent(hwnd);
// Make sure that the window doesn't have a parent
if ((parentWin == IntPtr.Zero))
{
int length = (int) GetWindowTextLength(hwnd);
// Check if it has caption text
if (length > 0)
{
Ventana ventana = new Ventana();
sb = new System.Text.StringBuilder(length + 1);
GetWindowText(hwnd, sb, sb.Capacity);
ventana.Caption = sb.ToString();
ventana.WinHandle = hwnd;
hwndProc=IntPtr.Zero;
th=GetWindowThreadProcessId(hwnd, out hwndProc);
if(hwndProc==processhandle)
return hwnd;
winList.Add(ventana);
}
}
hwnd = Win32Window.GetWindow(hwnd,
Win32Window.GetWindowParam.GW_HWNDNEXT);
}
return hwnd;
}

but th=GetWindowThreadProcessId(hwnd, out hwndProc); returns wrong process
id - windows title is completely different. I initialize hwndProc with zero
as advised, but there's sth wrong.
I test on HP Hx4700
 
P

Paul G. Tobey [eMVP]

In general, it works. I use code very similar to this in a Telnet server
running on our devices to safely close processes which have windows without
calling TerminateProcess(). Try it in C/C++, using EnumWindows() to
enumerate the windows, and see if it works in that case. That will localize
the problem to either the OS itself or your managed code program.

Paul T.
 
S

Sergey Bogdanov

You are passing p.Handle instead of p.Id but the
GetWindowThreadProcessId returns process ID.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top