So I've created a window using WinAPI and I have a WindowProc callback function that handles window messages. In this function I want to access my own struct that wraps around the WinAPI HWND
, to change other variables stored in this struct.
I know that GLFW supplies the functions glfwSetWindowUserPointer
and glfwGetWindowUserPointer
to fill this need. I tried searching for GetWindowUserPointer, WindowUserPointer, WindowPointer and UserPointer on https://learn.microsoft.com/en-us/windows/desktop, but found no results.
Is there any function or otherwise method to fill this need using WinAPI?
There is SetWindowLongPtr()
and GetWindowLongPtr()
The window creator always may use GWLP_USERDATA
, you can store any data pointer of your choice. If you reserve additional space (cbWndExtra
) when registering the window class, you can store additional data at positive offsets as well.
A common technique is to use WM_NCCREATE
to copy a pointer from the lpParam
parameter of CreateWindowEx
and copy it to the window data using SetWindowLongPtr
. Then other cases in the window procedure can retrieve the pointer and get your associated object.
Raymond Chen has a complete explanation of using this feature to forward messages to a member function.