lital maatuk
lital maatuk

Reputation: 6249

windows.h and MFC

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

Upvotes: 7

Views: 9522

Answers (3)

A.J.
A.J.

Reputation: 365

You can include windows.h; but you need to first include afx.h (or similar). If you got the error: "MFC apps must not #include <Windows.h>"; it is from including something like afx.h after including windows.h.

You might need to turn on 'show includes' if not sure how it got included.

Upvotes: 0

Bojan Komazec
Bojan Komazec

Reputation: 9536

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

#ifndef _WINDOWS_
#define _WINDOWS_

which means that _WINDOWS_ becomes defined if this header is included. Afx.h includes afxver_.h and this header includes afxv_w32.h which contains following code:

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

So, if you include windows.h before MFC headers, you'll get this error generated in compile time and, as you can see, if you include afxwin.h you don't need to include windows.h yourself - it will already be included by afxv_w32.h.

Upvotes: 12

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116296

Because in MFC you are not supposed to use it directly. AFAIR you should include afx.h instead, which in turn indirectly includes windows.h the proper way.

Upvotes: 2

Related Questions