What to do about "error C2065: 'WH_KEYBOARD_LL': undeclared identifier"

If you find yourself trying to install low level windows keyboard handlers, perhaps to intercept the windows key, you'll likely encounter a cryptic error message:

  C2065: 'WH_KEYBOARD_LL': undeclared identifier

Chances are you're writing a LowLevelKeyboardHook function, you're calling CallNextHookEx, and you're installing the handler with something like:

  hook = SetWindowsHookEx ( WH_KEYBOARD_LL, LowLevelKeyboardHook, GetModuleHandle(NULL), 0 );

To make this compile, you have to tell Visual Studio that you're targeting only recent versions of Windows. You do this by creating a preprocessor definition, making _WIN32_WINNT be 0x0500.

You can put this at the very beginning of something like your stdafx.h file if you like. You have to be sure you define this before gets included though. The whole point of defining it is so that you get a different include of . (The nice thing about this approach is that you're less likely to forget about it when you go to cut and paste some code for a new project!)

Or you could do what I do: put it in the project definitions. Go to the Project properties for your project, go to "Configuration Properties", "C/C++", "Preprocessor", and add it into "Preprocessor Definitions". As you can see in the image below, I also have UNICODE and _UNICODE defined without values; multiple definitions are separated by semicolons


I like this approach because I can be totally sure that the definition will happen before gets included. And I've put this page up here on my website so I can remind myself.