Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in the name of allah

hi everyone

i work with visual C++ 2008 and i'm a project that contains the c and c++ code together.

the project worked correctly but when i add a .cpp and a .h file to the project, the project

C++
errors the c1189 : MFC requires to use winsock2.h


.

look the codes in the .cpp and .h files are correctly because when i add these files to another mfc project will work,

but when add to my project this error will happen.

can you tell me what the problem is?

first part of the my .h file is like below :

___

C++
#pragma once

#ifdef _WINDOWS_
#undef _WINDOWS_
#endif

#include <Afx.h>
#include <afxwin.h>
#include <atlimage.h>
#include <math.h>


___

in addition i need to use the CWnd and CString and CImage ,... Classes in my project, because of this include the afxwin.h(for CWnd) and atlimage.h(for CImage) and ....

so i have to do these, don't i?

remaining part of .h file is :

___

C++
class Object{
public:
    CImage Image;
    short int x,y;
};

class Theme : public CWnd{
private:
    
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

public:
    CWnd  *Parent;
    CPoint cursor;
    bool drag;
    CString path,imagepath;
    char DialogMove;//0==top to center - 1==center to top - 2==center to down - 3==down to center
    char code,sb;//code = 0=main page - sb=show button=1=ascedung,-1=desceding;
    short int iimage,bstart,bend,//i = index of images-b = index of buttons
    ib;//ib=index of button for clicking and do code
    short int w, h,D;
    float ani_p, ani_max;
    bool ani,mousec;
    CRect WRect,rdesktop;
    CDC *cdc;
    short int lani;
    HWND hwndani;
    Object *Image;
    CRect *Rect;
    bool *BEnable;//enable of buttons-1 = enable
    unsigned char ButtonCount,ImageCount,SysButtonCount;
    bool Transparent;
    int EndMsg;//0=WM_CLOSE        1=PostMessage(IDOK)

    void GotoPage(unsigned char PageNo,unsigned char BStart,unsigned char BEnd);
    void ClearRect(short i,short j,COLORREF color);
    bool IsIn(CPoint point,CRect rect);
    bool IsInRect(CPoint point,CRect rect,CRect Rectangle);
    void Center2Top(int);
    void Top2Center();
    void Down2Center();
    void Center2Down();
    bool AddImage(unsigned char Index,CString ImagePath,unsigned short int X,short int Y);
    void AddButton(unsigned char Index,CRect rectangle,bool enable);
    void DrawImages(unsigned char alpha,int IImage);
    //Theme(unsigned char imagecount,unsigned char buttoncount,unsigned char sysbuttoncount);
    void Init(unsigned char imagecount,unsigned char buttoncount,unsigned char sysbuttoncount);
    ~Theme(void);
    bool CreateTheme(CRect rect,CWnd *parent,unsigned short int W,unsigned short int H,bool transparent);
    afx_msg void OnPaint();
    afx_msg void OnTimer(UINT_PTR nIDEvent);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    
    DECLARE_MESSAGE_MAP()
};


___

after that

in my .cpp file i wrote the body of functions.

so where the problem is?

thanks alot


thanks
Posted

1 solution

This error occurs when including afxsock.h and winsock.h has been included before.

With MFC apps you should not include windows.h or winsock.h. Just include the necessary MFC (afx*) header files. They will include windows.h and winsock2.h is included by afxsock.h.

For non MFC apps, you must include winsock2.h before windows.h because that includes winsock.h which prevents winsock2.h from being loaded.

Because you use MFC, check all your source and header files and remove all inclusions of windows.h and winsock.h. This might be tricky if you use external libraries (not from Microsoft) or source modules with header files that have these files included. Than you must comment out the inclusion in the header files.

In the case of source modules it may be than necessary to add the includes there.

[UPDATE]
Remove these lines from your header file:
#ifdef _WINDOWS_
#undef _WINDOWS_
#endif

They break the error checking of the Windows include files. Especially the error that windows.h must not be included when using MFC.
 
Share this answer
 
v3
Comments
Mahdi Nejadsahebi 9-Feb-15 7:14am    
dear friend
thanks for your ans
but you saw my .h code
i've never included the windows.h and winsock.h

in fact my project isn't MFC project.
i'm using the CWnd and... in console and work with them.

can you help me more?
Jochen Arndt 9-Feb-15 7:21am    
Your project is MFC because you are using the C* types and are including the necessary afx* files.

Regarding your header file I forgot one thing:
Why are you undefining _WINDOWS_?
You should not do so. I will update my answer accordingly.

If you remove that part you may get other errors that will lead you to the real problem. As I already noted, windows.h may be included anywhere.
Mahdi Nejadsahebi 9-Feb-15 8:02am    
i undefining _WINDOWS_ because pre error was the windows.h is already included.
i searched about it and i found this code to prevent definition again.
Jochen Arndt 9-Feb-15 8:12am    
Your problem is that windows.h is included before.
Undefining _WINDOWS_ avoids the error message but does not solve the problem(s).

The only solution:
Don't include windows.h. Then the error message regarding winsock2.h will also vanish.

Did you found the "solution" here: http://www.codeproject.com/Questions/562146/fatalpluserrorplusC-aplus-errorplus-aplusp?
That is definitely the wrong way. I will add a comment there.
Mahdi Nejadsahebi 9-Feb-15 11:47am    
thanks dear jochen

i removed the part of #ifdef _WINDOWS_ ....

but the error is still there.
all right
i'll talk to you there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900