Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows.h and MFC

Tags:

c++

mfc

afx

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

like image 420
lital maatuk Avatar asked Feb 17 '11 08:02

lital maatuk


People also ask

What is Windows H used for?

h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems.

Why Windows H is not working?

h" or "cannot open include file 'winres. h'". That problem happens because the file, which is needed to compile programs that make calls to the Windows operating system, is not installed. To fix this, download and install the Microsoft Windows SDK for your system (it is free).

Where is Windows H header file?

Search for and locate the windows. h header file in the C:\Program Files\Microsoft SDKs directory. Open the file C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars32.


2 Answers

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.

like image 68
Bojan Komazec Avatar answered Oct 05 '22 21:10

Bojan Komazec


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.

like image 33
Péter Török Avatar answered Oct 05 '22 21:10

Péter Török