Windows Target Version Macros

The Win32 SDK does not come in different versions for different platforms.  For example, if you choose to develop for Windows 98, you should work with the same SDK as you would when developing for Windows XP.  The solution Microsoft offers to enforce a minimum system requirement for a particular application works through a set of preprocessor macros used widely in the Windows header files.  These macros are WINVER, _WIN32_WINDOWS, _WIN32_WINNT, and _WIN32_IE.  You should define these macros to a hexadecimal number that varies for each platform.  Here’s a table that indicates which macros need to be defined for developing for a particular platform, and what number should they be defined to.

System Requirement WINVER _WIN32_WINDOWS _WIN32_WINNT _WIN32_IE
Windows 95 and NT 4.0 0×0400 n/a n/a n/a
Windows 98 and NT 4.0 0×0400 0×0410 n/a n/a
Windows NT 4.0 0×0400 n/a 0×0400 n/a
Windows 98 n/a 0×0410 n/a n/a
Windows Me n/a 0×0490 n/a n/a
Windows 2000 0×0500 n/a 0×0500 n/a
Windows XP and Windows .NET Server 0×0501 n/a 0×0501 n/a
Internet Explorer 3.0, 3.01, 3.02 n/a n/a n/a 0×0300
Internet Explorer 4.0 n/a n/a n/a 0×0400
Internet Explorer 4.01 n/a n/a n/a 0×0401
Internet Explorer 5.0, 5.0a, 5.0b n/a n/a n/a 0×0500
Internet Explorer 5.01, 5.5 n/a n/a n/a 0×0501
Internet Explorer 6.0 n/a n/a n/a 0×0560
or
0×0600

In order to use the above macros, you should define them before including any other header files.  For example, if you want to develop an application targeting Windows 98, Me, NT 4.0, 2000, XP, with a minimum of Internet Explorer 5.0 installed, you should include Windows headers like this:

#define WINVER         0x0400
#define _WIN32_WINDOWS 0x0410
#define _WIN32_IE 0x0500

#include <windows.h>
// include other necessary headers as well...

You should find the minimum system requirement, and define the macros dependently.  In the above example, the minimum system requirement is Windows 98 and NT 4.0 with Internet Explorer 5.0 installed.  In other words, any application developed with regard to those requirements is compatible with higher system versions.

Note that this is only a compile-time check.  You should perform run-time checks as well.  By being strict in defining the above macros as necessary, you ensure that you won’t use any features that are not present in the target platform.

This article originally appeared on BeginThread.com. It’s been republished here, and may contain modifications from the original article.

Posted in Visual C++