GWLPUSERDATA
GWLP_USERDATA is a Windows API constant used with the GetWindowLongPtr and SetWindowLongPtr functions to access a per-window user data value. It provides a single pointer-sized field associated with a specific window instance, allowing applications to attach and retrieve application-defined data to window handles.
- Setting data: SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pData) stores a pointer-sized value tied to that window.
- Retrieving data: pData = (Type*)GetWindowLongPtr(hWnd, GWLP_USERDATA) reads back the pointer-sized value.
- Common pattern: store a pointer to a C++ object or a structure that holds per-window state, enabling
GWLP_USERDATA refers to a slot in the window’s extra memory area, which is defined by the window
GWLP_USERDATA is the portable, pointer-sized variant of GWL_USERDATA. Use GetWindowLongPtr and SetWindowLongPtr to be 64-bit safe;
- The value is not automatically managed; it is the programmer’s responsibility to allocate, store, and release
- If the window is destroyed, the stored pointer does not get freed unless explicitly handled in
- This mechanism is commonly used in object-oriented wrappers and custom window procedures to associate per-window state