4 out of 6 rated this helpful - Rate this topic

GetWindowTextLength function

Applies to: desktop apps only

Retrieves the length, in characters, of the specified window's title bar text (if the window has a title bar). If the specified window is a control, the function retrieves the length of the text within the control. However, GetWindowTextLength cannot retrieve the length of the text of an edit control in another application.

Syntax

int WINAPI GetWindowTextLength(
  __in  HWND hWnd
);

Parameters

hWnd [in]

Type: HWND

A handle to the window or control.

Return value

Type:

Type: int

If the function succeeds, the return value is the length, in characters, of the text. Under certain conditions, this value may actually be greater than the length of the text. For more information, see the following Remarks section.

If the window has no text, the return value is zero. To get extended error information, call GetLastError.

Remarks

If the target window is owned by the current process, GetWindowTextLength causes a WM_GETTEXTLENGTH message to be sent to the specified window or control.

Under certain conditions, the GetWindowTextLength function may return a value that is larger than the actual length of the text. This occurs with certain mixtures of ANSI and Unicode, and is due to the system allowing for the possible existence of double-byte character set (DBCS) characters within the text. The return value, however, will always be at least as large as the actual length of the text; you can thus always use it to guide buffer allocation. This behavior can occur when an application uses both ANSI functions and common dialogs, which use Unicode. It can also occur when an application uses the ANSI version of GetWindowTextLength with a window whose window procedure is Unicode, or the Unicode version of GetWindowTextLength with a window whose window procedure is ANSI. For more information on ANSI and ANSI functions, see Conventions for Function Prototypes.

To obtain the exact length of the text, use the WM_GETTEXT, LB_GETTEXT, or CB_GETLBTEXT messages, or the GetWindowText function.

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

GetWindowTextLengthW (Unicode) and GetWindowTextLengthA (ANSI)

See also

Reference
GetWindowText
SetWindowText
WM_GETTEXT
WM_GETTEXTLENGTH
Conceptual
Windows
Other Resources
CB_GETLBTEXT
LB_GETTEXT

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Does the returned value include the null?
The documentation on GetWindowText is very specific on whether the passed or returned lengths include the NULL terminator. This function's documentation does not say.

[Edit: I am guessing no because a window with no text returns a value of 0 rather than 1, according to the docs.]
Nikki, try this
The return value cannot be less than 0.

int len = GetWindowTextLength(_hWnd);
if(len = 0) {
System.ComponentModel.Win32Exception ex = new System.ComponentModel.Win32Exception; // This automatically catches the error without issuing any commands that may overwrite it.
if(ex.NativeErrorCode > 0) {
System.Diagnostics.Trace.WriteLine("Error: " + ex.Message);
}
}
C# declaration

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
Returns error 183 "Cannot create a file when that file already exists."
In Visual C#, I am using:
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
staticexternint GetWindowTextLength(IntPtr hWnd);

int len = GetWindowTextLength(_hWnd);
if(len < 0) System.Diagnostics.Trace.WriteLine("Error:"+Marshal.GetLastWin32Error());

And it is giving error 183 "Cannot create a file when that file already exists."!!!
Possible VB9 declaration
Friend Declare Auto Function GetWindowTextLength Lib "user32.dll" (ByVal hwnd As Int32) As Int32