Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:48:35+00:00 2026-05-13T10:48:35+00:00

I’m using the following code to iterate through and find windows containing specific strings

  • 0

I’m using the following code to iterate through and find windows containing specific strings in their title bars. I have two projects containing this identical information, yet for some reason – it works when targeted to x64, but FindWindowLike returns 0 always on x86. I need this to run in x86.

I am developing this on Windows 7 x64, but it needs to run on my machine as well as XPx32. What I can’t fathom is why it compiles/runs on x64 but not x32 (considering I’m targetting user32.dll)

Here comes the code:

    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowByClass(ByVal lpClassName As String, ByVal zero As IntPtr) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowByCaption(ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function IsWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

    Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Public Const GW_HWNDFIRST = 0
    Public Const GW_HWNDLAST = 1
    Public Const GW_HWNDNEXT = 2
    Public Const GW_HWNDPREV = 3
    Public Const GW_OWNER = 4
    Public Const GW_CHILD = 5
    Public listWindows As New List(Of String)

Private Function FindWindows() As Long
        Dim hWndStart As Long = 0
        Dim WindowText As String = "*message*"
        Dim Classname As String = "*"
        Dim hwnd As Long
        Dim sWindowText As String
        Dim sClassname As String
        Dim r As Long

        'Hold the level of recursion and
        'hold the number of matching windows
        Static level As Integer

        'Initialize if necessary. This is only executed 
        'when level = 0 and hWndStart = 0, normally 
        'only on the first call to the routine.
        If level = 0 Then
            If hWndStart = 0 Then hWndStart = GetDesktopWindow()
        End If

        'Increase recursion counter      
        level = level + 1

        'Get first child window
        hwnd = GetWindow(hWndStart, GW_CHILD)

        Do Until hwnd = 0

            'Search children by recursion
            Call FindWindows()

            'Get the window text and class name
            sWindowText = Space$(255)
            r = GetWindowText(hwnd, sWindowText, 255)
            sWindowText = Microsoft.VisualBasic.Left(sWindowText, r)
            sClassname = Space$(255)
            r = GetClassName(hwnd, sClassname, 255)
            sClassname = Microsoft.VisualBasic.Left(sClassname, r)

            'Check if window found matches the search parameters
            If (sWindowText Like WindowText) And (sClassname Like Classname) Then
                If listWindows.Contains(hwnd & "||||" & sClassname & "||||" & sWindowText) = False Then
                    listWindows.Add(hwnd & "||||" & sClassname & "||||" & sWindowText)
                End If
                FindWindows = hwnd

                'uncommenting the next line causes the routine to
                'only return the first matching window.
                'Exit Do

            End If

            'Get next child window
            hwnd = GetWindow(hwnd, GW_HWNDNEXT)

        Loop

        'Reduce the recursion counter
        level = level - 1
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:48:35+00:00Added an answer on May 13, 2026 at 10:48 am

    Your function definitions are wrong. In C LONG is 32 bits wide, but in C# and VB.NET long is 64 bits wide on both 32-bit and 64-bit systems. Also, your window handle parameters should be IntPtrs.

    Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    

    Note that you shouldn’t be passing Strings to GetWindowText and GetClassName. Try StringBuilder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The answer seems be.....cannot be done. May 13, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer Using an interface to represent configuration is a good idea.… May 13, 2026 at 11:22 pm
  • Editorial Team
    Editorial Team added an answer This is a tough one, I'll give a try. +1… May 13, 2026 at 11:22 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I want use html5's new tag to play a wav file (currently only supported
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I've got a string that has curly quotes in it. I'd like to replace

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.