Structure Packing

The Windows NT DLLs are compiled with 4-byte default packing. However, the Windows NT headers do not force 4-byte default packing. Instead, Windows NT applications are required be compiled with either /Zp4 or no /Zp specification, which defaults to 4-byte packing. This means that if a Win32 application compiles with /Zp1, most of its Win32 structures will be misaligned with respect to the Windows NT DLLs, and the results will be unpredictable.

MSVC has the unique feature of supporting nested packing modes. This means a Win32 application can compile using MSVC with /Zp1 or /Zp2 and still have the Win32 structures properly packed. They simply need to do the following:

#pragma pack(4) #include <windows.h> #pragma pack()

This will work only for MSVC because only MSVC supports pushing and popping of packing modes. The Win32 headers then, of course, always push or pop the packing mode when they explicitly want to set packing for a particular structure or set of structures. This will not work for any other compiler, including C/Set.

Unlike the Win32 headers, Open32 forces the right packing (4) to occur throughout the Open32 headers, regardless of which compiler options are specified. OS/2 does this to accommodate code bases that are normally compiled using MSVC and to make use of the MSVC packing feature to specify either /Zp1 or /Zp2.

To summarize:


[Back: SMART Conversion Error]
[Next: System Handles]