How to get the main window of a Process!!!

G

Guest

about the function: ShellExecute,
there's a Parameter: nShowCmd.

and in .net:
Process proc = ;// get process object;
IntPtr hwnd = proc.MainWindowHandle;

so,maybe there's a way to get the main window immediately.

how to do this, with api or sdk or mfc in vc++ ?

thx!!!
 
W

William DePalo [MVP VC++]

Funfound said:
about the function: ShellExecute,
there's a Parameter: nShowCmd.

and in .net:
Process proc = ;// get process object;
IntPtr hwnd = proc.MainWindowHandle;

so,maybe there's a way to get the main window immediately.

how to do this, with api or sdk or mfc in vc++ ?

The problem is that a process may have 0, 1 or more windows. It may have
multiple top-level, unowned windows. The "main" window doesn't have meaning
except in the case when you already know that there is a single un-owned
window.

That said EnumWindows() to create a list of all top-level windows. For each
item in the list you can call GetWindowThreadProcessId(). Then you can build
a sub-list of top-level windows that match the process id. If you have one
such window, you can call it the "main" window. If you have more than one,
you pick whichever strikes your fancy.

Regards,
Will
 
B

Ben Voigt


This looks like you control both applications? HWNDs are global handles,
you can pass them around without any DuplicateHandle or stuff like that. So
push it through a named pipe (or shared memory, or socket). Named pipes
look like files through Win32 API, so you should be able to open
\\.\Pipe\Blah from your .NET code.
 

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