Visual Basic 6 function "GetWindowCaption"

Go back

Below you'll find the source for the Visual Basic 6 function GetWindowCaption.

Attribute VB_Name = "modGetWindowCaption"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

Private Declare Function GetWindowTextA Lib "user32" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLengthA Lib "user32" (ByVal Hwnd As Long) As Long

''
' Gets a window caption
' @param    Long    Hwnd        The window handler
' @return   String              The caption of the window
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function GetWindowCaption(Hwnd As Long) As String
    Dim l As Long, s As String
    
    l = GetWindowTextLengthA(Hwnd) + 1
    s = String(l, 0)
    GetWindowTextA Hwnd, s, l
    
    GetWindowCaption = Left(s, InStr(s, vbNullChar) - 1)
End Function