Macros for Windows
When using some new versions of Apis or new control features (such as the new version of comctl32.dll), you may get the error "error c2065: Undeclared identifier. The reason is that these functions depend on your operating system version. The definition in your header file is not up-to-date. (For MFC, It is stdafx. h) The following msdn article describes how to solve this problem and details the macros ntddi_version, _ win32_winnt, winver, and _ win32_ie corresponding to each Windows version.
Original article: http://msdn2.microsoft.com/en-us/library/aa383745.aspx
Title: using the Windows Headers
The header files for the Windows API enable you to create 32-and 64-bit applications. they include declarations for both Unicode and ANSI versions of the API. for more information, see Unicode in the Windows API. they use data types that allow you to build both 32-and 64-bit versions of your application from a single source code base. for more information, see getting ready for 64-bit windows. additional features include header annotations and strict type checking.
Microsoft Visual C ++ program des copies of the Windows header files that were current at the time Visual C ++ was released. therefore, if you install updated header files from an SDK, you may end up with multiple versions of the Windows header files on your computer. if you do not ensure that you are using the latest version of the SDK header files, you will receive the following error code when compiling code that uses features that were introduced after visual c ++ was released: Error c2065: Undeclared identifier.
Conditional declarations
Certain functions that depend on a particle version of Windows are declared using conditional code. this enables you to use the compiler to detect whether your application uses functions that are not supported on its target version (s) of windows. to compile an application that uses these functions, you must define the appropriate macros. otherwise, you will receive the c2065 error message.
The Windows header files use macros to indicate which versions of Windows support program programming elements. therefore, you must define these macros to use new functionality introduced in each major operating system release. (individual header files may use different macros; therefore, if compilation problems occur, check the header file that contains the definition for conditional definitions .) for more information, see sdkddkver. h.
The following table describes the preferred Macros in use by the Windows header files.
Minimum system required |
Macros to define |
Windows Vista |
Ntddi_version> = ntddi_longhorn |
Windows Server 2003 SP1 |
Ntddi_version> = ntddi_ws03sp1 |
Windows Server 2003 |
Ntddi_version> = ntddi_ws03 |
Windows XP SP2 |
Ntddi_version> = ntddi_winxpsp2 |
Windows XP SP1 |
Ntddi_version> = ntddi_winxpsp1 |
Windows XP |
Ntddi_version> = ntddi_winxp |
Windows 2000 SP4 |
Ntddi_version> = ntddi_win2ksp4 |
Windows 2000 sp3 |
Ntddi_version> = ntddi_win2ksp3 |
Windows 2000 sp2 |
Ntddi_version> = ntddi_win2ksp2 |
Windows 2000 sp1 |
Ntddi_version> = ntddi_win2ksp1 |
Windows 2000 |
Ntddi_version> = ntddi_win2k |
The following table describes the legacy Macros in use by the Windows header files.
Minimum system required |
Macros to define |
Windows Vista |
_ Win32_winnt> = 0x0600 Winver> = 0x0600 |
Windows Server 2003 |
_ Win32_winnt> = 0x0502 Winver> = 0x0502 |
Windows XP |
_ Win32_winnt> = 0x0501 Winver> = 0x0501 |
Windows 2000 |
_ Win32_winnt> = 0x0500 Winver> = 0x0500 |
Windows NT 4.0 |
_ Win32_winnt> = 0x0400 Winver> = 0x0400 |
Windows ME |
_ Win32_windows = 0x0500 Winver> = 0x0500 |
Windows 98 |
_ Win32_windows> = 0x0410 Winver> = 0x0410 |
Windows 95 |
_ Win32_windows> = 0x0400 Winver> = 0x0400 |
Internet Explorer 7.0 |
_ Win32_ie> = 0x0700 |
Internet Explorer 6.0 SP2 |
_ Win32_ie> = 0x0603 |
Internet Explorer 6.0 SP1 |
_ Win32_ie> = 0x0601 |
Internet Explorer 6.0 |
_ Win32_ie> = 0x0600 |
Internet Explorer 5.5 |
_ Win32_ie> = 0x0550 |
Internet Explorer 5.01 |
_ Win32_ie> = 0x0501 |
Internet Explorer 5.0, 5.0a, 5.0b |
_ Win32_ie> = 0x0500 |
Internet Explorer 4.01 |
_ Win32_ie> = 0x0401 |
Internet Explorer 4.0 |
_ Win32_ie> = 0x0400 |
Internet Explorer 3.0, 3.01, 3.02 |
_ Win32_ie> = 0x0300 |
Note that some features introduced in the latest version of Windows may be added to a service pack for a previous version of Windows. therefore, to target a service pack, you may need to define _ win32_winnt with the value for the next major operating system release. for example,GetdlldirectoryFunction was introduced in Windows Server 2003 and is conditionally defined if _ win32_winnt is 0x0502 or greater. this function was also added to Windows XP SP1. therefore, if you were to define _ win32_winnt 0x0501 to target Windows XP, you wocould Miss features that are defined in Windows XP SP1.
You can define these symbols by using the # define statement in each source file, or by specifying the/d compiler option supported by Visual C ++. To specify compiler options, go toProjectsMenu and clickProperties. GoConfiguration Properties, ThenC ++, ThenCommand Line. Enter the option underAdditional options.
Faster builds with smaller header files
You can reduce the size of the Windows header files by excluding some of the less common API declarations as follows:
- Define win32_lean_and_mean to exclude APIs such as cryptography, DDE, RPC, shell, and Windows Sockets.
- Define one or more of the NOAPISymbols to exclude the API. For example, nocomm excludes the serial communication API. For a list of support noAPISymbols, see Windows. h
Example:
// Stdafx. h: contains files in the standard system,
// Or frequently used but rarely changed
// Project-specific include files
# Pragma once
# Ifndef vc_extralean
# Define vc_extralean // exclude infrequently used data from the Windows Header
# Endif
// If you must use the platform before the platform specified in the following figure, modify the following definition.
// For the latest information about the values of different platforms, refer to msdn.
# Ifndef winver // allows specific features of Windows 95 and Windows NT 4 or later.
# Define winver 0x0500 // for Windows 98 and Windows 2000, and the updated version is changed to the appropriate value.
# Endif
# Ifndef _ win32_winnt // specific features of Windows NT 4 or later are allowed.
# DEFINE _ win32_winnt 0x0500 // change to the appropriate value for windows98, Windows 2000, and later versions.
# Endif
# Ifndef _ win32_windows // specific features of Windows 98 or later are allowed.
# DEFINE _ win32_windows 0x0410 // change the value of Windows ME and the updated version to an appropriate value.
# Endif
# Ifndef _ win32_ie // you can use the features of IE 4.0 or later.
# DEFINE _ win32_ie 0x0400 // It is IE 5.0 and the updated version is changed to an appropriate value.
# Endif
# DEFINE _ atl_cstring_explicit_constructors // some cstring constructors will be explicitly
// Disable MFC to hide some common warning messages that are often ignored by security.
# DEFINE _ afx_all_warnings
# Include <afxwin. h> // MFC core and standard components
# Include <afxext. h> // MFC extension
# Include <afxdisp. h> // MFC automation class
# Include <afxdtctl. h> // support for Internet Explorer 4 public control MFC
# Ifndef _ afx_no_afxcmn_support
# Include <afxcmn. h> // support for Windows public control MFC
# Endif // _ afx_no_afxcmn_support