Results 1 to 4 of 4

Thread: can't find a previous post

  1. #1

    Thread Starter
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    There was a post a while back where someone wanted to find a running program if you only have a part of the programs caption. eg "Microsoft Word" or "Connecting to". I can't seem to find this anywhere. Can anyone find it?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's one good method, you enumarate all windows and then compare their caption with like operator. Use GetWindowsByPatter to return an array of handles:
    Code:
    Option Compare Text
    
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private handles&(), counter&
    
    Private Function EnmWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        ReDim Preserve handles(counter)
        handles(counter) = hwnd
        counter = counter + 1
        EnmWinProc = hwnd
    End Function
    Public Function GetWindows() As Long()
        ReDim handles(0)
        counter = 0
        Call EnumWindows(AddressOf EnmWinProc, 0&)
        GetWindows = handles
    End Function
    
    
    
    Function GetWindowsByPatter(pattern As String) ' As Long()
    Dim temp As String, n As Variant, l As Long, s As Long, matches() As Long
        For Each n In GetWindows
            l = GetWindowTextLength(CLng(n))
            temp = Space(256)
            GetWindowText n, temp, l
            If temp Like pattern Then
                ReDim Preserve matches(s)
                matches(s) = n
                s = s + 1
            End If
        Next n
        GetWindowsByPatter = matches
    End Function
    'To use
    Sub main()
        Dim n
        For Each n In GetWindowsByPatter("*Microsoft*")
            Debug.Print n;
        Next n
    End Sub
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Guest
    I remember this.

    Code:
    'Author: Evan
    'Origin: http://forums.vb-world.net/showthrea...threadid=35824
    'Purpose: Enter part of a caption and return the full caption
    'Version: VB5+ 
    
    Enter PART of a caption 
    in the function and it 
    will tell you the WHOLE 
    caption. 
    
    Public Const GW_HWNDNEXT = 2
    
    Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
      ByVal wCmd As Long) As Long
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
      (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindowThreadProcessId Lib "user32" _
      (ByVal hwnd As Long, lpdwprocessid As Long) As Long
    
        
    Function GetWinHandle(hInstance As String) As String
       Dim buffer As String
       Dim tempHwnd As Long
       Dim TempLen As Integer
       Dim TempCaption As String
        buffer = Space$(128)
       ' Grab the first window handle that Windows finds:
       tempHwnd = FindWindow(vbNullString, vbNullString)
       
       ' Loop until you find a match or there are no more window handles:
       Do Until tempHwnd = 0
            TempLen = GetWindowText(tempHwnd, buffer, Len(buffer))
            If TempLen <> 0 Then
            TempCaption = Left$(buffer, TempLen)
            Else
            TempCaption = ""
            End If
              If InStr(1, TempCaption, hInstance) <> 0 Then
                ' Return found handle
                GetWinHandle = TempCaption
                ' Exit search loop
                Exit Do
             End If
       
          ' Get the next window handle
          tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
       Loop
    End Function

  4. #4

    Thread Starter
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Thanks Matthew thats exactly what I was looking for!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width