Windows.h and MFC

Why can't I include windows.h in afx (MFC) projects?

+7
source share
2 answers

Typically, the MFC application code includes afx.h or afxwin.h (the latter includes the former). The first two lines of windows.h are equal

 #ifndef _WINDOWS_ #define _WINDOWS_ 

which means that _WINDOWS_ becomes definable if this header is included. afx.h includes afxver_.h , and this header includes afxv_w32.h , which contains the following code:

 #ifdef _WINDOWS_ #error WINDOWS.H already included. MFC apps must not #include <windows.h> #endif ... #include <windows.h> 

So, if you included windows.h in front of the MFC headers, you will get this error generated at compile time, and, as you can see, if you enable afxwin.h , you do not need to enable windows.h yourself - it will already be included afxv_w32.h .

+11
source

Because in MFC you should not use it directly. AFAIR you should include afx.h instead, which in turn indirectly includes windows.h appropriately.

+2
source