12 out of 33 rated this helpful Rate this topic

GetWindowText function

Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.

Syntax

int WINAPI GetWindowText(
  __in   HWND hWnd,
  __out  LPTSTR lpString,
  __in   int nMaxCount
);

Parameters

hWnd [in]

Type: HWND

A handle to the window or control containing the text.

lpString [out]

Type: LPTSTR

The buffer that will receive the text. If the string is as long or longer than the buffer, the string is truncated and terminated with a null character.

nMaxCount [in]

Type: int

The maximum number of characters to copy to the buffer, including the null character. If the text exceeds this limit, it is truncated.

Return value

Type: int

If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError.

This function cannot retrieve the text of an edit control in another application.

Remarks

If the target window is owned by the current process, GetWindowText causes a WM_GETTEXT message to be sent to the specified window or control. If the target window is owned by another process and has a caption, GetWindowText retrieves the window caption text. If the window does not have a caption, the return value is a null string. This behavior is by design. It allows applications to call GetWindowText without becoming unresponsive if the process that owns the target window is not responding. However, if the target window is not responding and it belongs to the calling application, GetWindowText will cause the calling application to become unresponsive.

To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.

Examples

For an example, see Sending a Message.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

GetWindowTextW (Unicode) and GetWindowTextA (ANSI)

See also

Reference
GetWindowTextLength
SetWindowText
WM_GETTEXT
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 9/11/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Ex: Getting parent Form's caption
Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Public Function GetParentFormCaption(ByVal lngHwnd As Long) As String
  Dim strBuff As String * 255
  Dim lngOldHwnd As Long, lngResult As Long
 
  lngOldHwnd = GetParent(lngHwnd)
  lngResult = GetWindowText(lngOldHwnd, strBuff, Len(strBuff))
 
  GetParentFormCaption = Trim(strBuff)
End Function

NOTE: The "GetParentFormCaption()" will get the parent-form's, only if the calling form is set as child at runtime [using 'SetParent()'].
The secret life of GetWindowText
The old New Thing:
http://blogs.msdn.com/oldnewthing/archive/2003/08/21/54675.aspx
C# syntax
[DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
vb.net syntax
<DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetWindowText(ByVal hWnd As IntPtr, <Out, MarshalAs(UnmanagedType.LPTStr)> ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
Possible VB9 declaration
Friend Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Int32, <Out()> ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32

Example:

                Dim len As Integer = API.GetWindowTextLength(hWnd)
If len > 0 Then
Dim b As New System.Text.StringBuilder(ChrW(0), len + 1)
Dim ret = API.GetWindowText(hWnd, b, b.Capacity)
If ret <> 0 Then
Return b.ToString
Else
Throw New API.Win32APIException
End If
Else
Dim ex As New API.Win32APIException
If ex.NativeErrorCode <> 0 Then
Throw New API.Win32APIException
Else
Return ""
End If
End If