How to find hWnd handle from enumerated process?

G

Gary

Hi i need to work with windows of processes that are running on my pc
from a certain programme. I've figured out how to build an array of
processes that contain just the processes i'm interested in by checking
what the process name starts with.

How do i find out the hWnd for the processes i've returned, because the
function i need to run wants me to pass it the hWnd of the processes.

Here's what I have so far, using notepad as my example : -

static void EnumerateWindows()
{
System.Diagnostics.Process[] processes;
processes =
System.Diagnostics.Process.GetProcessesByName("notepad");

foreach (System.Diagnostics.Process instance in processes)
{
MessageBox.Show(instance.Handle.ToString());

}

}
}

Thankyou experts,

Gary.
 
B

Barry Kelly

Gary said:
How do i find out the hWnd for the processes i've returned, because the
function i need to run wants me to pass it the hWnd of the processes.
static void EnumerateWindows()
MessageBox.Show(instance.Handle.ToString());

What you've got there is a HINSTANCE, not a HWND.

One way of doing it:
- use P/Invoke and call EnumWindows, passing in a callback.
- then use P/Invoke and call GetWindowThreadProcessId to get the PID
- you can then use the PID with Process.GetProcessById to do your
filtering.

http://pinvoke.net has some info on prototypes to use for EnumWindows()
and friends.

-- Barry
 
B

Barry Kelly

Barry said:
What you've got there is a HINSTANCE, not a HWND.

Whups - I mis-remembered. It's a handle to the process, but that doesn't
materially change my suggestion.

-- Barry
 

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