Home

SetWindowLongPtr

SetWindowLongPtr is a Windows API function that stores or retrieves a per-window value in a manner that is safe for 64-bit applications. It is commonly used to subclass a window by replacing its window procedure or to associate application data with a window. On 64-bit Windows, SetWindowLongPtr provides a pointer-sized alternative to the older SetWindowLong function, which helps maintain compatibility and correctness across architectures.

The function takes three parameters: a handle to the window (HWND hWnd), an index specifying which value

The return value is the previous value stored at the specified slot. If the function fails, the

Compatibility notes: SetWindowLongPtr replaces SetWindowLong on 64-bit systems. For 32-bit code, SetWindowLongPtr is usually defined to

See also: GetWindowLongPtr, GetWindowLong, SetWindowLong, GWLP_WNDPROC, GWLP_USERDATA, CallWindowProc.

to
set
(int
nIndex),
and
the
new
value
to
store
(LONG_PTR
dwNewLong).
The
nIndex
parameter
is
typically
one
of
the
GWLP_
constants
for
long-pointer
values,
with
GWLP_WNDPROC
used
to
replace
the
window
procedure
and
GWLP_USERDATA
used
to
store
a
user-defined
pointer
or
data.
Other
GWLP_*
values
can
be
used
to
store
additional
per-window
information
as
appropriate.
In
contrast,
some
older
uses
relied
on
GWL_
constants;
SetWindowLongPtr
and
GetWindowLongPtr
adapt
these
values
for
64-bit
safety.
return
value
is
zero,
and
extended
error
information
can
be
obtained
by
calling
GetLastError.
When
subclassing
a
window,
the
previous
window
procedure
is
returned;
you
should
typically
forward
messages
to
the
original
procedure
via
CallWindowProc.
map
to
SetWindowLong.
Use
the
GWLP_*
indices
to
avoid
truncation
and
to
ensure
correct
behavior
in
64-bit
environments.