Results 1 to 5 of 5

Thread: [RESOLVED] How to get Window Caption using its exe name?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Resolved [RESOLVED] How to get Window Caption using its exe name?

    Hi all!

    Is it possible to get the window caption of an active process given its ".exe name"? If it's possible, how to do it?

    Thanks for any reply.

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: How to get Window Caption using its exe name?

    Quote Originally Posted by Momotae
    Hi all!

    Is it possible to get the window caption of an active process given its ".exe name"? If it's possible, how to do it?

    Thanks for any reply.
    Take a look at this LINK
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: How to get Window Caption using its exe name?

    Quote Originally Posted by Mark Gambo
    Take a look at this LINK
    It does not work for me

    I tried to use GetWindowText from the returned hWnd, and it does not work... (returns empty string, or returns "M"...)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const GW_HWNDFIRST = 0
    4. Private Const GW_HWNDNEXT = 2
    5. Private Const GW_CHILD = 5
    6.  
    7. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    8. Private Declare Function GetDesktopWindow Lib "user32" () As Long
    9. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    10. Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    11.  
    12. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    13. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    14.  
    15. Private lProcessID As Long
    16.  
    17. Private Sub Command1_Click()
    18.     lProcessID = Shell("Notepad.exe", vbNormalFocus)
    19. End Sub
    20.  
    21. Private Sub Command2_Click()
    22.     Dim lhRet As Long, LN As Long, WindowText As String
    23.    
    24.     lhRet = hWndFromProcID(lProcessID)
    25.    
    26.     LN = GetWindowTextLength(lhRet)
    27.     WindowText = String(LN + 10, 0)
    28.     GetWindowText lhRet, WindowText, Len(WindowText)
    29.    
    30.     Debug.Print WindowText
    31.    
    32.     'Call BringWindowToTop(lhRet)
    33. End Sub
    34.  
    35. Public Function hWndFromProcID(ProcID As Long) As Long
    36.     Dim lhTmp           As Long
    37.     Dim lRetVal         As Long
    38.     Dim lCurrentProcID  As Long
    39.    
    40.     On Error GoTo errhWndFromProcID
    41.    
    42.     lhTmp = GetDesktopWindow()
    43.     lhTmp = GetWindow(lhTmp, GW_CHILD)
    44.    
    45.     Do While lhTmp <> 0
    46.         lRetVal = GetWindowThreadProcessId(lhTmp, lCurrentProcID)
    47.        
    48.         If lCurrentProcID = ProcID Then
    49.             hWndFromProcID = lhTmp
    50.             Exit Do
    51.         End If
    52.        
    53.         lhTmp = GetWindow(lhTmp, GW_HWNDNEXT)
    54.     Loop
    55.    
    56. Exit Function
    57. errhWndFromProcID:
    58.     hWndFromProcID = 0
    59. End Function

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: How to get Window Caption using its exe name?

    Works for me, thanks Mark...

  5. #5
    Lively Member
    Join Date
    May 2006
    Posts
    84

    Re: [RESOLVED] How to get Window Caption using its exe name?

    Very Nice, it work for me too, Thx

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