Home All Groups Group Topic Archive Search About

How can I get caption of all open windows??

Author
31 Mar 2006 7:58 AM
M O J O
Hi,

I need to to get the caption of all open windows. Not only for my
application, but for all running programs.

I know it has something to do with Win32 EnumWindows, but I've tried some
examples, but can't make it work.

I need the sample code .... please! :o)

Thank you in advance!

M o j o

Author
31 Mar 2006 11:16 AM
Ken Tucker [MVP]
Hi,

    Dim cb As CallBack

    ' VB.NET declaration
    Public Delegate Function CallBack( _
        ByVal hwnd As IntPtr, _
        ByVal lParam As IntPtr) As Boolean

    Public Declare Function EnumWindows Lib "user32" ( _
        ByVal lpEnumFunc As CallBack, _
        ByVal lParam As Integer) As Integer

    Declare Function GetWindowTextLength Lib "user32" Alias
"GetWindowTextLengthA" ( _
        ByVal hwnd As IntPtr) As Integer

    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
            (ByVal hwnd As IntPtr, _
            ByVal lpString As StringBuilder, _
            ByVal cch As Integer) As Integer

    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
IntPtr, _
        ByVal hWndNewParent As IntPtr) As Integer

    Public Function MyCallBack(ByVal hwnd As IntPtr, ByVal lParam As IntPtr)
As Boolean
        Dim intLen As Integer = GetWindowTextLength(hwnd) + 1
        If intLen > 1 Then
            Dim strText As New StringBuilder(intLen)
            Try
                GetWindowText(hwnd, strText, intLen)
                ListBox1.Items.Add(strText.ToString)
            Catch ex As Exception
                Trace.WriteLine(ex.ToString)
            End Try
        End If
        Return True
    End Function


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        cb = New CallBack(AddressOf MyCallBack)
        EnumWindows(cb, 0)
    End Sub

Ken
----------------
Show quoteHide quote
"M O J O" <M***@discussions.microsoft.com> wrote in message
news:AD7A4A6A-56D1-4DEB-9C63-894CF2967810@microsoft.com...
> Hi,
>
> I need to to get the caption of all open windows. Not only for my
> application, but for all running programs.
>
> I know it has something to do with Win32 EnumWindows, but I've tried some
> examples, but can't make it work.
>
> I need the sample code .... please! :o)
>
> Thank you in advance!
>
> M o j o
Author
31 Mar 2006 11:38 AM
M O J O
Hi Ken,

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I've spent two days now searching for the answer, converting vb6 and cs
files without any luck.

Your solution works PERFECTLY! :o))))

Thanks once more!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Kind regards,
M o j o

Show quoteHide quote
"Ken Tucker [MVP]" wrote:

> Hi,
>
>     Dim cb As CallBack
>
>     ' VB.NET declaration
>     Public Delegate Function CallBack( _
>         ByVal hwnd As IntPtr, _
>         ByVal lParam As IntPtr) As Boolean
>
>     Public Declare Function EnumWindows Lib "user32" ( _
>         ByVal lpEnumFunc As CallBack, _
>         ByVal lParam As Integer) As Integer
>
>     Declare Function GetWindowTextLength Lib "user32" Alias
> "GetWindowTextLengthA" ( _
>         ByVal hwnd As IntPtr) As Integer
>
>     Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
>             (ByVal hwnd As IntPtr, _
>             ByVal lpString As StringBuilder, _
>             ByVal cch As Integer) As Integer
>
>     Private Declare Function SetParent Lib "user32" (ByVal hWndChild As
> IntPtr, _
>         ByVal hWndNewParent As IntPtr) As Integer
>
>     Public Function MyCallBack(ByVal hwnd As IntPtr, ByVal lParam As IntPtr)
> As Boolean
>         Dim intLen As Integer = GetWindowTextLength(hwnd) + 1
>         If intLen > 1 Then
>             Dim strText As New StringBuilder(intLen)
>             Try
>                 GetWindowText(hwnd, strText, intLen)
>                 ListBox1.Items.Add(strText.ToString)
>             Catch ex As Exception
>                 Trace.WriteLine(ex.ToString)
>             End Try
>         End If
>         Return True
>     End Function
>
>
>     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>         cb = New CallBack(AddressOf MyCallBack)
>         EnumWindows(cb, 0)
>     End Sub
>
> Ken
> ----------------
> "M O J O" <M***@discussions.microsoft.com> wrote in message
> news:AD7A4A6A-56D1-4DEB-9C63-894CF2967810@microsoft.com...
> > Hi,
> >
> > I need to to get the caption of all open windows. Not only for my
> > application, but for all running programs.
> >
> > I know it has something to do with Win32 EnumWindows, but I've tried some
> > examples, but can't make it work.
> >
> > I need the sample code .... please! :o)
> >
> > Thank you in advance!
> >
> > M o j o
>
>
>