Home

GWLPWNDPROC

GWLP_WNDPROC is a constant used with the Windows API to access or replace the window procedure associated with a specific window. It identifies the slot in the per-window user data where a pointer to the window procedure (WNDPROC) is stored. By using this index with GetWindowLongPtr or SetWindowLongPtr, applications can retrieve the current window procedure or install a new one, enabling window procedure subclassing or custom message handling.

In practice, a programmer commonly subclasses a window by setting a new window procedure and preserving the

The value of GWLP_WNDPROC is -4, and it is used with GetWindowLongPtr/SetWindowLongPtr to access the window

As an alternative to manual subclassing, modern code can use SetWindowSubclass, which provides safer, more managed

old
one
for
calls
to
the
original
procedure.
Typical
usage
is
to
call
SetWindowLongPtr
with
GWLP_WNDPROC
to
install
a
custom
procedure,
while
saving
the
previous
WNDPROC
value.
The
new
procedure
should
follow
the
standard
WNDPROC
signature
and
may
call
CallWindowProc
to
forward
messages
to
the
original
procedure
when
appropriate.
For
example:
SetWindowLongPtr(hWnd,
GWLP_WNDPROC,
(LONG_PTR)MyWndProc);
and
then
in
MyWndProc
use
CallWindowProc
to
pass
unhandled
messages
to
the
old
procedure.
procedure
pointer
in
both
32-bit
and
64-bit
environments.
On
64-bit
systems,
SetWindowLongPtr/GetWindowLongPtr
are
the
correct
APIs
to
avoid
truncation
issues
that
can
occur
with
the
older
GetWindowLong/SetWindowLong.
Proper
handling
requires
storing
the
previous
procedure
and
restoring
it
when
subclassing
ends,
and
ensuring
the
subclass
does
not
interfere
with
system
messages
or
window
destruction.
subclassing
with
automatic
cleanup.