Home

SetWindowLong

SetWindowLong is a Windows API function used to set a value associated with a window, either in the window’s extra data or, commonly, its window procedure pointer. It is part of the family that includes GetWindowLong and their pointer-sized variants. In modern code, SetWindowLongPtr is preferred for proper 64-bit compatibility.

The API has two main forms. In 32-bit applications, the signature is LONG SetWindowLong(HWND hWnd, int nIndex,

The nIndex parameter selects what is being set. Common indices include GWLP_WNDPROC or GWL_WNDPROC to install

Return value: The function returns the previous value at the specified offset. If the function fails, the

Best practices: Use SetWindowLongPtr with GWLP_WNDPROC for subclassing, and prefer SetWindowLongPtr over SetWindowLong in modern, portable

LONG
dwNewLong).
In
64-bit
applications,
the
portable
form
is
LONG_PTR
SetWindowLongPtr(HWND
hWnd,
int
nIndex,
LONG_PTR
dwNewLongPtr).
The
SetWindowLong
macro
maps
to
SetWindowLongPtr
on
64-bit
builds,
but
direct
use
of
SetWindowLong
with
pointers
can
truncate
values
on
64-bit
systems,
so
SetWindowLongPtr
is
recommended.
a
new
window
procedure,
and
GWLP_USERDATA
or
other
GWLP/GWL
values
for
window-associated
data.
When
subclassing
a
window
by
replacing
its
WndProc,
the
usual
pattern
is
to
install
a
new
procedure
with
GWLP_WNDPROC,
store
the
old
procedure,
and
forward
messages
to
the
original
window
procedure
via
CallWindowProc.
return
value
is
zero
and
extended
error
information
is
available
via
GetLastError.
Because
a
legitimate
previous
value
can
be
zero,
a
failure
must
be
detected
with
GetLastError.
code.
This
API
can
affect
window
behavior
across
threads
and
processes,
so
changes
should
be
made
carefully
and
with
proper
error
handling.