设为首页 收藏本站
查看: 1430|回复: 0

[经验分享] 有关Windows版本的宏

[复制链接]

尚未签到

发表于 2016-5-20 07:28:47 | 显示全部楼层 |阅读模式
  有关Windows版本的宏

在使用一些新版本的API,或者控件的新特性(比如新版的ComCtl32.dll)的时候,你可能会得到“error C2065: undeclared identifier.“这个错误。原因是这些功能是依赖于你的操作系统的版本的。而你的头文件中的定义并不是最新的。(对于MFC,就是stdafx.h)下面这篇MSDN文章介绍了如何解决这个问题,并详细列举了每个Windows版本对应的NTDDI_VERSION_WIN32_WINNTWINVER_WIN32_IE这些宏。

原文:http://msdn2.microsoft.com/en-us/library/aa383745.aspx
标题: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, seeUnicode 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, seeGetting Ready for 64-bit Windows. Additional features includeHeader AnnotationsandSTRICT Type Checking.

Microsoft Visual C++ includes 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 particular 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 many 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 requiredMacros to define
Windows VistaNTDDI_VERSION >=NTDDI_LONGHORN
Windows Server 2003 SP1NTDDI_VERSION >=NTDDI_WS03SP1
Windows Server 2003NTDDI_VERSION >=NTDDI_WS03
Windows XP SP2NTDDI_VERSION >=NTDDI_WINXPSP2
Windows XP SP1NTDDI_VERSION >=NTDDI_WINXPSP1
Windows XPNTDDI_VERSION >=NTDDI_WINXP
Windows 2000 SP4NTDDI_VERSION >=NTDDI_WIN2KSP4
Windows 2000 SP3NTDDI_VERSION >=NTDDI_WIN2KSP3
Windows 2000 SP2NTDDI_VERSION >=NTDDI_WIN2KSP2
Windows 2000 SP1NTDDI_VERSION >=NTDDI_WIN2KSP1
Windows 2000NTDDI_VERSION >=NTDDI_WIN2K

The following table describes the legacy macros in use by the Windows header files.
Minimum system requiredMacros to defineWindows Vista_WIN32_WINNT>=0x0600
WINVER>=0x0600Windows Server 2003_WIN32_WINNT>=0x0502
WINVER>=0x0502Windows XP_WIN32_WINNT>=0x0501
WINVER>=0x0501Windows 2000_WIN32_WINNT>=0x0500
WINVER>=0x0500Windows NT 4.0_WIN32_WINNT>=0x0400
WINVER>=0x0400Windows Me_WIN32_WINDOWS=0x0500
WINVER>=0x0500Windows 98_WIN32_WINDOWS>=0x0410
WINVER>=0x0410Windows 95_WIN32_WINDOWS>=0x0400
WINVER>=0x0400Internet Explorer 7.0_WIN32_IE>=0x0700Internet Explorer 6.0 SP2_WIN32_IE>=0x0603Internet Explorer 6.0 SP1_WIN32_IE>=0x0601Internet Explorer 6.0_WIN32_IE>=0x0600Internet Explorer 5.5_WIN32_IE>=0x0550Internet Explorer 5.01_WIN32_IE>=0x0501Internet Explorer 5.0, 5.0a, 5.0b_WIN32_IE>=0x0500Internet Explorer 4.01_WIN32_IE>=0x0401Internet Explorer 4.0_WIN32_IE>=0x0400Internet 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, theGetDllDirectoryfunction 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 would 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 to theProjectsmenu and clickProperties. Go toConfiguration 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

  例:
//stdafx.h:标准系统包含文件的包含文件,
//或是经常使用但不常更改的
//项目特定的包含文件

#pragmaonce

#ifndefVC_EXTRALEAN
#defineVC_EXTRALEAN//从Windows标头中排除不常使用的资料
#endif

//如果您必须使用下列所指定的平台之前的平台,则修改下面的定义。
//有关不同平台的相应值的最新信息,请参考MSDN。
#ifndefWINVER//允许使用Windows95和WindowsNT4或更高版本的特定功能。
#defineWINVER0x0500//为Windows98和Windows2000及更新版本改变为适当的值。
#endif

#ifndef_WIN32_WINNT//允许使用WindowsNT4或更高版本的特定功能。
#define_WIN32_WINNT0x0500//为Windows98和Windows2000及更新版本改变为适当的值。
#endif

#ifndef_WIN32_WINDOWS//允许使用Windows98或更高版本的特定功能。
#define_WIN32_WINDOWS0x0410//为WindowsMe及更新版本改变为适当的值。
#endif

#ifndef_WIN32_IE//允许使用IE4.0或更高版本的特定功能。
#define_WIN32_IE0x0400//为IE5.0及更新版本改变为适当的值。
#endif

#define_ATL_CSTRING_EXPLICIT_CONSTRUCTORS//某些CString构造函数将是显式的

//关闭MFC对某些常见但经常被安全忽略的警告消息的隐藏
#define_AFX_ALL_WARNINGS

#include<afxwin.h>//MFC核心和标准组件
#include<afxext.h>//MFC扩展
#include<afxdisp.h>//MFC自动化类

#include<afxdtctl.h>//InternetExplorer4公共控件的MFC支持
#ifndef_AFX_NO_AFXCMN_SUPPORT
#include<afxcmn.h>//Windows公共控件的MFC支持
#endif//_AFX_NO_AFXCMN_SUPPORT



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-219284-1-1.html 上篇帖子: Windows服务初探 下篇帖子: Windows mobile Install the Tools
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表