Find windows by Title

Hey guys I'm stuck and was hoping one of you could give me a hand with this... What I need to be able to do is look at all parent and child windows that are present on a screen and obtain the windows handle so that I can give it focus for another process...

This seems to be the line where the problem is...
sStr = GetWindowTextLength(lhWndP) + 1

sStr is always empty...
Option Strict Off
Option Explicit On
Imports System.Windows.Forms.Application
Imports System.Threading.Thread
Imports System.Runtime.InteropServices









Public Class Form1
    Dim hWnd As Long
    Dim obj As New TCaptureXLib.TextCaptureX
    Private strResults As String
    Private strWinTitle As String

    <DllImport("user32", EntryPoint:="GetWindowTextLength")> _
Private Shared Function GetWindowTextLength( _
ByVal hwnd As Int32) As Int32
    End Function

    <DllImport("user32", EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Int32
    End Function

    <DllImport("user32", EntryPoint:="GetWindowText")> _
    Private Shared Function GetWindowText( _
    ByVal hwnd As Int32, _
    ByVal lpString As String, _
    ByVal cch As Int32) As Int32
    End Function

    <DllImport("user32", EntryPoint:="GetWindow")> _
    Private Shared Function GetWindow( _
    ByVal hwnd As Int32, _
    ByVal wCmd As Int32) As Int32
    End Function

    Private Const GW_HWNDNEXT As Int32 = 2


    Private Sub CaptureWindow(ByVal hWnd)
        Do Until Not strResults = ""


            'hWnd = obj.GetActiveWindowHwnd()

            strResults = obj.CaptureActiveWindow()
            If strResults <> "" Then
                MsgBox("Got It")
                RichTextBox1.Text = strResults
            End If
            DoEvents()
            Sleep(250)
        Loop
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Clear()
        Dim lhWndP As Long
        If GetHandleFromPartialCaption(lhWndP, "Practice Plus") = True Then
            MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
        Else
            MsgBox("Window 'Horizon", vbOKOnly + vbExclamation)
        End If


    End Sub
    Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
        Dim lhWndP As Long
        Dim sStr As String
        sStr = ""
        GetHandleFromPartialCaption = False
        lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While lhWndP <> 0
            sStr = GetWindowTextLength(lhWndP) + 1
            GetWindowText(lhWndP, sStr, Len(sStr))
            sStr = sStr.Length - 1
            If InStr(1, sStr, sCaption) > 0 Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
            lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
        Loop
    End Function
End Class

Open in new window

romiebehuninAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mastooCommented:
Think you want something more like this...

int iStr = GetWindowTextLength(lhWndP) + 1
Dim sStr As New System.Text.StringBuilder(iStr)
            GetWindowText(lhWndP, sStr, iStr)
 
romiebehuninAuthor Commented:
Yeah last night I tried something similar... let me give that a shot real quick
romiebehuninAuthor Commented:
Problem there is this piece is looking for a string not string builder:
  <DllImport("user32", EntryPoint:="GetWindowText")> _
    Private Shared Function GetWindowText( _
    ByVal hwnd As Int32, _
    ByVal lpString As String, _
    ByVal cch As Int32) As Int32
    End Function

Should I change     ByVal lpString As String, _
to String Builder?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mastooCommented:
It's been a long time since I made a .net windows api call, but yes I believe you change the declaration to have a stringbuilder instead of string.  In managed code, api string outputs don't work because they haven't been allocated in managed memory.  I've got a book with some examples I can flip open if that still doesn't work.
romiebehuninAuthor Commented:
Ok got rid of all compile errors... on this line
Int(iStr = GetWindowTextLength(lhWndP) + 1)
iStr is always zero... Pasting up my modified code so you can get a new look at it            

Option Strict Off
Option Explicit On
Imports System.Windows.Forms.Application
Imports System.Threading.Thread
Imports System.Runtime.InteropServices









Public Class Form1
    Dim hWnd As Long
    Dim obj As New TCaptureXLib.TextCaptureX
    Private strResults As String
    Private strWinTitle As String

    <DllImport("user32", EntryPoint:="GetWindowTextLength")> _
Private Shared Function GetWindowTextLength( _
ByVal hwnd As Int32) As Int32
    End Function

    <DllImport("user32", EntryPoint:="FindWindow")> _
    Private Shared Function FindWindow( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Int32
    End Function

    <DllImport("user32", EntryPoint:="GetWindowText")> _
    Private Shared Function GetWindowText( _
    ByVal hwnd As Int32, _
    ByVal lpString As System.Text.StringBuilder, _
    ByVal cch As Int32) As Int32
    End Function

    <DllImport("user32", EntryPoint:="GetWindow")> _
    Private Shared Function GetWindow( _
    ByVal hwnd As Int32, _
    ByVal wCmd As Int32) As Int32
    End Function

    Private Const GW_HWNDNEXT As Int32 = 2


    Private Sub CaptureWindow(ByVal hWnd)
        Do Until Not strResults = ""


            'hWnd = obj.GetActiveWindowHwnd()

            strResults = obj.CaptureActiveWindow()
            If strResults <> "" Then
                MsgBox("Got It")
                RichTextBox1.Text = strResults
            End If
            DoEvents()
            Sleep(250)
        Loop
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RichTextBox1.Clear()
        Dim lhWndP As Long
        If GetHandleFromPartialCaption(lhWndP, "Practice Plus") = True Then
            MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
        Else
            MsgBox("Window 'Horizon", vbOKOnly + vbExclamation)
        End If


    End Sub
    Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
        Dim lhWndP As Long
        Dim strCaption As String
        Dim iStr As Integer

        'sStr = ""
        GetHandleFromPartialCaption = False
        lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While lhWndP <> 0
            Int(iStr = GetWindowTextLength(lhWndP) + 1)
            Dim sStr As New System.Text.StringBuilder(iStr)
            GetWindowText(lhWndP, sStr, iStr)


            'sStr = GetWindowTextLength(lhWndP) + 1
            'GetWindowText(lhWndP, sStr, Len(sStr))
            strCaption = sStr.ToString
            If InStr(1, strCaption, sCaption) > 0 Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
            lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
        Loop
    End Function
End Class

Open in new window

mastooCommented:
Sorry, that's my fault.  I'm mixing my C# and vb syntax.  This line:

Int(iStr = GetWindowTextLength(lhWndP) + 1)

should be

Dim iStr as int =  GetWindowTextLength(lhWndP) + 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romiebehuninAuthor Commented:
Cool... no problem I've been doing the same thing... ok so now iStr is only 1... think I see why though... the windows handle is declared inside the function... I'm going to redeclare it on the class level.  Yep that did it... this line:
 strCaption = sStr.ToString
           
is picking up windows captions now.... good job man!!!! Let me run some tests real quick.... I think that fixed it!
romiebehuninAuthor Commented:
Excellent Job!!!!!!! Thank you so much!!!
mastooCommented:
happy to help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.