Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windowing
Windows
Windows Reference
Functions
 GetWindowText Function
GetWindowText Function

The 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 GetWindowText(      
    HWND hWnd,     LPTSTR lpString,     int nMaxCount );

Parameters

hWnd
[in] Handle to the window or control containing the text.
lpString
[out] Pointer to 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] Specifies 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

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.

Windows 95/98/Me: GetWindowTextW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Sending a Message.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Possible VB9 declaration      Đonny ... ziad ahmad   |   Edit   |   Show History
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
Tags What's this?: Add a tag
Flag as ContentBug
vb.net syntax      dmex   |   Edit   |   Show History
<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
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
Tags What's this?: c# (x) syntax (x) Add a tag
Flag as ContentBug
The secret life of GetWindowText      Jochen Kalmbach   |   Edit   |   Show History
The old New Thing:
http://blogs.msdn.com/oldnewthing/archive/2003/08/21/54675.aspx
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker