GetWindowLong Function

The GetWindowLong function retrieves information about the specified window. The function also retrieves the 32-bit (long) value at the specified offset into the extra window memory.

If you are retrieving a pointer or a handle, this function has been superseded by the GetWindowLongPtr function. (Pointers and handles are 32 bits on 32-bit Microsoft Windows and 64 bits on 64-bit Windows.) To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr.

Syntax

LONG GetWindowLong(      
    HWND hWnd,     int nIndex );

Parameters

hWnd
[in] Handle to the window and, indirectly, the class to which the window belongs.
nIndex
[in] Specifies the zero-based offset to the value to be retrieved. Valid values are in the range zero through the number of bytes of extra window memory, minus four; for example, if you specified 12 or more bytes of extra memory, a value of 8 would be an index to the third 32-bit integer. To retrieve any other value, specify one of the following values.
GWL_EXSTYLE
Retrieves the extended window styles. For more information, see CreateWindowEx.
GWL_STYLE
Retrieves the window styles.
GWL_WNDPROC
Retrieves the address of the window procedure, or a handle representing the address of the window procedure. You must use the CallWindowProc function to call the window procedure.
GWL_HINSTANCE
Retrieves a handle to the application instance.
GWL_HWNDPARENT
Retrieves a handle to the parent window, if any.
GWL_ID
Retrieves the identifier of the window.
GWL_USERDATA
Retrieves the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
The following values are also available when the hWnd parameter identifies a dialog box.
DWL_DLGPROC
Retrieves the address of the dialog box procedure, or a handle representing the address of the dialog box procedure. You must use the CallWindowProc function to call the dialog box procedure.
DWL_MSGRESULT
Retrieves the return value of a message processed in the dialog box procedure.
DWL_USER
Retrieves extra information private to the application, such as handles or pointers.

Return Value

If the function succeeds, the return value is the requested 32-bit value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

If SetWindowLong has not been called previously, GetWindowLong returns zero for values in the extra window or class memory.

Remarks

Reserve extra window memory by specifying a nonzero value in the cbWndExtra member of the WNDCLASSEX structure used with the RegisterClassEx function.

Windows 95/98/Me: GetWindowLongW is supported by the Microsoft Layer for Unicode (MSLU). GetWindowLongA is also supported to provide more consistent behavior across all Windows operating systems. To use these versions, 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 Creating, Enumerating, and Sizing Child Windows.

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 :


Community Content

Đonny
Possible VB9 declaration
Friend Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hwnd As Int32, ByVal nIndex As WindowLongs) As Int32
Friend Enum WindowLongs As Int32
GWL_EXSTYLE = -20
GWL_STYLE = -16
GWL_WNDPROC = -4
GWL_HINSTANCE = -6
GWL_HWNDPARENT = -8
GWL_ID = -12
GWL_USERDATA = -21
DWL_DLGPROC = 4
DWL_MSGRESULT = 0
DWL_USER = 8
End Enum

In order to obtain current wnd proc of window, you can use this declaration:

Friend Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hwnd As Int32, ByVal nIndex As WindowProcs) As Messages.WndProc
Public Delegate Function WndProc(ByVal hWnd As Integer, ByVal msg As Messages.WindowMessages, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Friend Enum WindowProcs As Int32
GWL_WNDPROC = WindowLongs.GWL_WNDPROC
DWL_DLGPROC = WindowLongs.DWL_DLGPROC
End Enum

WindwMessages is an enumeration of all known window messages and is of type Int32.

If such-declared function fails, return value is null (nothing).

Tags : api

dmex
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function GetWindowLong(ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer End Function
Tags : vb.net syntax

dmex
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowLong(Integer hWnd, int nIndex);
Tags : c# syntax

Page view tracker