14

I am following this tutorial: theForger's Win32 API Programming Tutorial

In section 4 (message looping) (Understanding the Message Loop), there is the code:

WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);
fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);

I tried to compile it, but I got this error message:

GWL_WNDPROC undeclared

What can I do to fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    That seems rather unlikely, if you properly #included windows.h Be sure to tell us if you got more than 1 error, the first 4 are the important ones. – Hans Passant Aug 09 '18 at 15:51
  • 3
    This is a question that requires a [mcve], together with a verbatim report of the compiler errors. – David Heffernan Aug 09 '18 at 15:52
  • `GWL_WNDPROC` is really undeclared. need use `GWLP_WNDPROC` – RbMm Aug 09 '18 at 16:57
  • 4
    @HansPassant: It literally takes less than a minute to look inside *WinUser.h*, and verify, that your statement is blatantly wrong. With currently 2 upvotes, it's probably wise to just purge that comment. – IInspectable Aug 09 '18 at 20:24
  • Hmm, it could just as well be blatantly right. Just flag it if you can't live with it. – Hans Passant Aug 09 '18 at 20:26
  • 2
    The reason for this is explained [here](https://stackoverflow.com/questions/18178628/how-do-i-call-setwindowlong-in-the-64-bit-versions-of-windows/18178661#18178661). – Jonathan Potter Aug 09 '18 at 20:59

1 Answers1

24

In <WinUser.h>, the following declarations exist:

/*
 * Window field offsets for GetWindowLong()
 */
#define GWL_WNDPROC         (-4)
...

#ifdef _WIN64

#undef GWL_WNDPROC
#undef GWL_HINSTANCE
#undef GWL_HWNDPARENT
#undef GWL_USERDATA

#endif /* _WIN64 */

#define GWLP_WNDPROC        (-4)

In case _WIN64 is defined (your target is 64 bit), the GWL_WNDPROC is really undefined. You need use GWLP_WNDPROC instead.

Also with GWLP_WNDPROC, you need use GetWindowLongPtr and SetWindowLongPtr, but not GetWindowLong and SetWindowLong.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RbMm
  • 31,280
  • 3
  • 35
  • 56