Skip to content

Using the Windows Headers

From: MSDN

Windows API 头文件允许您创建32- 和64-位应用程序。它们包含了Unicode 和 ANSI 版本的 API声明。更多的信息可参见 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++ 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.

条件申明

某些函数在部分Windows版本才具备,使用条件代码来声明它们。您就可以使用编译器来检测这些函数是否被目标Windows版本支持。必须定义适当的宏来编译使用到这些函数的应用程序。否则,你会收到C2065的出错信息。

Windows头文件使用宏来决定

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.

下表描述了Windows 头文件中的首选宏。

最小系统需求 NTDDI_VERSION
Windows Server 2008 NTDDI_WS08
Windows Vista SP1 NTDDI_VISTASP1
Windows Vista NTDDI_VISTA
Windows Server 2003 SP1 NTDDI_WS03SP1
Windows Server 2003 NTDDI_WS03
Windows XP SP2 NTDDI_WINXPSP2
Windows XP SP1 NTDDI_WINXPSP1
Windows XP NTDDI_WINXP
Windows 2000 SP4 NTDDI_WIN2KSP4
Windows 2000 SP3 NTDDI_WIN2KSP3
Windows 2000 SP2 NTDDI_WIN2KSP2
Windows 2000 SP1 NTDDI_WIN2KSP1
Windows 2000 NTDDI_WIN2K

下表描述了在Windows头文件中使用的宏。

最小系统需求 _WIN32_WINNT 和 WINVER的最小值
Windows Server 2008 0x0600
Windows Vista 0x0600
Windows Server 2003 SP1, Windows XP SP2 0x0502
Windows Server 2003, Windows XP 0x0501
Windows 2000 0x0500
最小版本需求 _WIN32_IE的最小值
Internet Explorer 7.0 0x0700
Internet Explorer 6.0 SP2 0x0603
Internet Explorer 6.0 SP1 0x0601
Internet Explorer 6.0 0x0600
Internet Explorer 5.5 0x0550
Internet Explorer 5.01 0x0501
Internet Explorer 5.0, 5.0a, 5.0b 0x0500

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, the GetDllDirectory function 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 the Projects menu and click Properties. Go to Configuration Properties, then C++, then Command Line. Enter the option under Additional Options.

控制结构包

Projects should be compiled to use the default structure packing, which is currently 8 bytes because the largest integral type is 8 bytes. Doing so ensures that all structure types within the header files are compiled into the application with the same alignment the Windows API expects. It also ensures that structures with 8-byte values are properly aligned and will not cause alignment faults on processors that enforce data alignment.

With Microsoft Visual C++ 2005, structure packing is controlled by the /Zp switch. When this switch is omitted from a project build, the compiler automatically uses the default structure packing size.

If you are using a packing setting other than the default, be sure to use a pragma pack statement as shown in the following example to ensure that the Windows header files are packed correctly.

 

用最少的头文件来快速编译

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 NOapi symbols to exclude the API. For example, NOCOMM excludes the serial communication API. For a list of support NOapi symbols, see Windows.h.

Post a Comment

Your email is never published nor shared. Required fields are marked *