MFC 8.0 problem: afxcomctl32.h structure alignment is 8 bytes (in MFC80.DLL)

Summary: In the release versions of MFC80.DLL the structure alignment when building afxcomctl32.h is 8 bytes.  This is different from any other structure in MFC 8.0 (things normally use 4 byte alignment). 

I was having a crash with the CComCtlWrapper class when calling some of the CImageList functions (e.g. GetImageCount)  

I finally isolated the problem to alignment issues.  Did you know that certain parts of MFC 8.0 are now alignment sensitive   I was building my MFC app with 2 byte alignment, and the MFC80.DLL is built with 8 byte alignment.

In previous versions of MFC, it was not required to use #pragma pack(push, 8) and
#pragma pack(pop)  around the inclusion of <afxwin.h> etc.  All classes were written in such a way as to work with 2, 4, or 8 byte alignment (i.e. those pragmas were already included in the MFC header files)

Now it appears that certain structures do not align the same way if specifying 2 byte alignment vs 8 byte alignment.   This is the case with CComCtlWrapper.

So in every stdafx.h in my project I have now had to add the pragmas around any MFC inclusions to avoid this problem. 

Here's the reason I think this problem is occurring:

in <afxwin.h> you include a file named <afxcomctl32.h>

You also have a mechanism using _AFX_PACKING where you do this:

#ifdef _AFX_PACKING
#pragma pack(push, _AFX_PACKING)
#endif

and _AFX_PACKING is defined as 4 for Intel x86 builds.  But the problem is, this is done after afxcomctl32.h is included in afxwin.h (it's lower down in the file).  The fact is: in x86 builds you have aligned all MFC structures using 4 bytes alignment, but built the DLL with 8 byte alignment.  Therefore, everything in afxcomctl32.h is aligned using 8 byte alignment.  Therefore you have different alignment for afxcomctl32.h CComCtlWrapper vs. everything else in MFC.  So using 2 or possibly even 4 byte alignment in an MFC project would cause things to fail badly with CComCtlWrapper.  The only workaround is to use the #pragmas as I mentioned above around any inclusion of <afxwin.h> to force 8 byte alignment.

The main problem here is that in previous MFC versions every MFC header file "did the correct thing" and pushed and popped the packing of the structure alignment, so it made no difference what alignment an application used.    Now it does make a difference, negating all the work done to protect this structure alignment, as the user will be forced to wrap any MFC inclusions with push/pop in their own stdafx.h files. 

Ted. 



Answer this question

MFC 8.0 problem: afxcomctl32.h structure alignment is 8 bytes (in MFC80.DLL)