How can I get all the window handlers that belongs to a process?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!!!

By any chance, do you know how can I get all the window handlers that
belongs to a process?

I'm trying to send some messages to the winproc that has focus, so I
can automate a data capture process. So far, I can access the Process by
using the Process class, and I can identify and send messages to the main
window. However, the main window is seldom the one that has focus. I saw with
the Microsoft's Spy++ that there are many Window handlers (one for each
control). If I can create a collection of their handlers, I will be able to
send messages to each one, automating the data capture process.

Any help is greatelly appreciated!!!

Tarh ik
 
Use this class:

Imports System.Diagnostics

Public Class Win32Windows

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As
EnumWindowsDelegate, _
ByVal lParam As Integer) As Integer
Private Delegate Function EnumWindowsDelegate(ByVal Handle As IntPtr,
ByVal iParam As Integer) As Boolean
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal
hWnd As Integer, _
ByRef lpdwProcessId As Integer) As Integer

Public Sub New(ByVal ProcessID As Integer)
EnumWindows(AddressOf EnumWindowProc, ProcessID)
End Sub

Private ReadOnly m_oWindowList As New ArrayList

Private Function EnumWindowProc(ByVal Handle As IntPtr, ByVal iParam As
Integer) As Boolean
Dim hWnd As Integer = Handle.ToInt32()
Dim iProcessID As Integer
Dim iThreadID As Integer = GetWindowThreadProcessId(hWnd,
iProcessID)

If iProcessID = iParam Then
m_oWindowList.Add(Handle.ToInt32())
End If

Return True
End Function

Public ReadOnly Property WindowList() As IList
Get
Return m_oWindowList
End Get
End Property
End Class


Example:
Dim list As New Win32Windows(Process.GetCurrentProcess().Id)
For Each iHandle As Integer In list.WindowList
Console.WriteLine(iHandle.ToString())
Next



Hope this helps!

Shariq Khan
(e-mail address removed)
 
Thanks Shariq!!!

The code that you gave me works great!!!! It has actually routed me to
the right path. I'm still getting only one handle, though, which is the
handler of the main window. However, I'm going to use your code to extract
all the handles of the child windows (I read somewhere that there is a
similar function for child windows... let's see...)

Thank you very much for your help!!!!!

Tarh ik
 
Back
Top