SetWindowLongPtrW function (winuser.h)

Changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory.

Note  To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetWindowLongPtr. When compiling for 32-bit Windows, SetWindowLongPtr is defined as a call to the SetWindowLong function.

 

Syntax

LONG_PTR SetWindowLongPtrW(
  [in] HWND     hWnd,
  [in] int      nIndex,
  [in] LONG_PTR dwNewLong
);

Parameters

[in] hWnd

Type: HWND

A handle to the window and, indirectly, the class to which the window belongs. The SetWindowLongPtr function fails if the process that owns the window specified by the hWnd parameter is at a higher process privilege in the UIPI hierarchy than the process the calling thread resides in.

Windows XP/2000:   The SetWindowLongPtr function fails if the window specified by the hWnd parameter does not belong to the same process as the calling thread.

[in] nIndex

Type: int

The zero-based offset to the value to be set. Valid values are in the range zero through the number of bytes of extra window memory, minus the size of a LONG_PTR. To set any other value, specify one of the following values.

Value Meaning
GWL_EXSTYLE
-20
Sets a new extended window style.
GWLP_HINSTANCE
-6
Sets a new application instance handle.
GWLP_ID
-12
Sets a new identifier of the child window. The window cannot be a top-level window.
GWL_STYLE
-16
Sets a new window style.
GWLP_USERDATA
-21
Sets 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.
GWLP_WNDPROC
-4
Sets a new address for the window procedure.
 

The following values are also available when the hWnd parameter identifies a dialog box.

Value Meaning
DWLP_DLGPROC
DWLP_MSGRESULT + sizeof(LRESULT)
Sets the new pointer to the dialog box procedure.
DWLP_MSGRESULT
0
Sets the return value of a message processed in the dialog box procedure.
DWLP_USER
DWLP_DLGPROC + sizeof(DLGPROC)
Sets new extra information that is private to the application, such as handles or pointers.

[in] dwNewLong

Type: LONG_PTR

The replacement value.

Return value

Type: LONG_PTR

If the function succeeds, the return value is the previous value of the specified offset.

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

If the previous value is zero and the function succeeds, the return value is zero, but the function does not clear the last error information. To determine success or failure, clear the last error information by calling SetLastError with 0, then call SetWindowLongPtr. Function failure will be indicated by a return value of zero and a GetLastError result that is nonzero.

Remarks

Certain window data is cached, so changes you make using SetWindowLongPtr will not take effect until you call the SetWindowPos function.

If you use SetWindowLongPtr with the GWLP_WNDPROC index to replace the window procedure, the window procedure must conform to the guidelines specified in the description of the WindowProc callback function.

If you use SetWindowLongPtr with the DWLP_MSGRESULT index to set the return value for a message processed by a dialog box procedure, the dialog box procedure should return TRUE directly afterward. Otherwise, if you call any function that results in your dialog box procedure receiving a window message, the nested window message could overwrite the return value you set by using DWLP_MSGRESULT.

Calling SetWindowLongPtr with the GWLP_WNDPROC index creates a subclass of the window class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process. The SetWindowLongPtr function creates the window subclass by changing the window procedure associated with a particular window class, causing the system to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.

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

Do not call SetWindowLongPtr with the GWLP_HWNDPARENT index to change the parent of a child window. Instead, use the SetParent function.

If the window has a class style of CS_CLASSDC or CS_PARENTDC, do not set the extended window styles WS_EX_COMPOSITED or WS_EX_LAYERED.

Calling SetWindowLongPtr to set the style on a progressbar will reset its position.

Note

The winuser.h header defines SetWindowLongPtr as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

Requirements

Requirement Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll
API set ext-ms-win-ntuser-windowclass-l1-1-0 (introduced in Windows 8)

See also

CallWindowProc

Conceptual

GetWindowLongPtr

Reference

RegisterClassEx

SetParent

WNDCLASSEX

Window Classes

WindowProc