Sign in to follow this  
cgrenderman

win32 api conflict with mfc..........

Recommended Posts

I use Quest3D's API(base on win32 api) for my application.I want to add the axis's api(base on mfc)to my project.the complier hint:#error WINDOWS.H already included. MFC apps must not #include <windows.h> how can i solve with the problem? thanks!

Share this post


Link to post
Share on other sites
that's odd, what compiler are you using? VS6? my last project that used MFC with a Win32 DLL that included windows.h compiled with no problems... as long as of course you keep the #include<windows.h> statement out of your precompiled header file. say for example:

stdafx.h
--------
#include<windows.h> // NO!!!
#include<afxwin.h>
#include<afxext.h>
#include<afxcmn.h>

is bad but

stdafx.h
--------
#include<afxwin.h>
#include<afxext.h>
#include<afxcmn.h>

someotherfile.cpp
-----------------
#include "stdafx.h"
#include<windows.h> // should be OK

is perfectly fine.

Share this post


Link to post
Share on other sites
My compiler is vs2003,my project include:channel.cpp :needs header file:#include "A3d_List.h"
#include "A3d_Channels.h"(the quest3d sdk's )
capturevideo.h: class CCamimage : public CWnd{......};needs header files:#include <afxwin.h> etc..
during channel.cpp,I need use class CCamimage;
how can i manage the header files?
if i put <afxwin.h> in stadfx.h,put <windows.h>, "A3d_Channels.h" to channel.cpp
.it also occurs to the same problem.

Share this post


Link to post
Share on other sites
Rule #1: Always use precompiled headers for MFC.
Rule #2: Never #include<afxwin.h> in anything other than your precompiled header (you break this rule in capturevideo.h).

Example: Run the following sample MFC app. I include <windows.h>
all over the place, and it compiles fine for me because I adhere
to the above rules. To this code, add your Quest3D stuff anywhere you want,
except in stdafx.h before the MFC includes, and you should be OK.

stdafx.h (Precompiled Header)

#ifndef __STDAFX_H
#define __STDAFX_H

#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif

#ifndef WINVER
#define WINVER 0x0501
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0501
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS
#define _AFX_ALL_WARNINGS

// #include <windows.h> // ERROR!
#include <afxwin.h>
#include <afxext.h>
#include <afxcmn.h>

#endif













stdafx.cpp (This is compiled first to build your PCH)

#include "stdafx.h"
#include<windows.h> // perfectly fine













app.h

#ifndef __MFCAPP_H
#define __MFCAPP_H

#include<windows.h> // perfectly fine

class CMFCAppApp : public CWinApp {
public :
CMFCAppApp();
protected :
virtual BOOL InitInstance();
};

#endif













app.cpp

#include "stdafx.h"
#include "stdres.h"
#include<windows.h> // perfectly fine
#include "app.h"
#include "frame.h"

CMFCAppApp::CMFCAppApp()
{
}

CMFCAppApp theApp;

BOOL CMFCAppApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();
CMainFrame* pFrame = new CMainFrame;
if(!pFrame) return FALSE;
m_pMainWnd = pFrame;
pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}













frame.h

#ifndef __MAINFRM_H
#define __MAINFRM_H

#include<windows.h> // perfectly fine
#include "client.h"

class CMainFrame : public CFrameWnd {
public :
CMainFrame();
virtual ~CMainFrame();
protected :
DECLARE_DYNAMIC(CMainFrame)
public :
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
protected :
CStatusBar m_wndStatusBar;
CChildView m_wndView;
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSetFocus(CWnd *pOldWnd);
afx_msg void OnExit();
DECLARE_MESSAGE_MAP()
};

#endif













frame.cpp

#include "stdafx.h"
#include "stdres.h"
#include<windows.h> // perfectly fine
#include "app.h"
#include "frame.h"

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_SETFOCUS()
ON_COMMAND(CM_EXIT, OnExit)
END_MESSAGE_MAP()

static UINT indicators[] =
{
ID_SEPARATOR,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};

CMainFrame::CMainFrame()
{
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if(CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;

if(!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
return -1;

if(!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)))
return -1;

return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(!CFrameWnd::PreCreateWindow(cs))
return FALSE;
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}

void CMainFrame::OnSetFocus(CWnd *pOldWnd)
{
m_wndView.SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
if(m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) return TRUE;
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

void CMainFrame::OnExit()
{
SendMessage(WM_CLOSE);
}













client.h

#ifndef __CHILDVIEW_H
#define __CHILDVIEW_H

#include<windows.h> // perfectly fine

class CChildView : public CWnd {
public :
CChildView();
virtual ~CChildView();
protected :
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected :
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};

#endif













client.cpp

#include "stdafx.h"
#include<windows.h> // perfectly fine
#include "app.h"
#include "client.h"

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}

BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if(!CWnd::PreCreateWindow(cs)) return FALSE;
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);
return TRUE;
}

void CChildView::OnPaint()
{
CPaintDC dc(this);
}













stdres.h

#define IDR_MAINFRAME 128
#define CM_EXIT 100

















stdres.rc

#include "stdres.h"

IDR_MAINFRAME MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", CM_EXIT
END
END











Share this post


Link to post
Share on other sites
Firstly,thanks for your help.^_^
when i do as you guide,it also has problems.
This time,i use precomplier heads and put all mfc's header files in it,put <windows.h> in my Quest3D's program;
my project:
channelDemo.cpp:
#include "stdafx.h"
#include "ChannelDemo.h"

ChannelType channelType;

extern "C" __declspec( dllexport ) ChannelType* __cdecl GetType() {
ZeroMemory(channelType.name, 80);
StringCbCopy(channelType.name, 79, CHANNELDEMO_NAME);
channelType.version = CHANNELDEMO_VERSION;
channelType.guid = CHANNELDEMO_GUID;
channelType.baseguid = CHANNELDEMO_GUID;
channelType.minimumEdition = EDITION_LEVEL_ALL; // All Quest3D Editions

return &channelType;
}

CHANNELDEMODLL_EXPORTS

ChannelDemo::ChannelDemo() {
SetChannelName(CHANNELDEMO_NAME);
//TODO: Add construction logic here
}

ChannelDemo::~ChannelDemo()
{
//TODO:: Add destruction logic here
}
......


channelDemo.h:
#define CHANNELDEMO_NAME "ChannelDemo"
#define CHANNELDEMO_VERSION 1

//{F65D46E2-ACC8-407B-8D6B-5725E9BB6FD4}
static const GUID CHANNELDEMO_GUID = { 0xF65D46E2, 0xACC8, 0x407B, { 0x8D, 0x6B, 0x57, 0x25, 0xE9, 0xBB, 0x6F, 0xD4 } };

class CHANNELDEMO_API ChannelDemo: public A3d_Channel
{
public:
ChannelDemo();
virtual ~ChannelDemo();
....
};

stdafx.h:
#include <windows.h>
#include "A3d_List.h"
#include "A3d_Channels.h"
.....

now ,i need use camimage.h in my project
camimage.h:
class CCamimage : public CWnd
{
protected:
DECLARE_DYNCREATE(CCamimage)
public:
CLSID const& GetClsid()
{
static CLSID const clsid

= { 0xC79D3167, 0x6133, 0x4E7C, { 0x82, 0x1C, 0x5C, 0x11, 0x46, 0x11, 0x2, 0x2D } };
return clsid;
}
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwstyle,
const RECT& rect, CWnd* pParentWnd, UINT nID,
CCreateContext* pContext = NULL)
{
return CreateControl(GetClsid(), lpszWindowName, dwstyle, rect, pParentWnd, nID);
}
.....
}

it needs :#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes

how i do the header files?thanks,

Share this post


Link to post
Share on other sites
In your last post, you have:

stdafx.h:
#include <windows.h>
#include "A3d_List.h"
#include "A3d_Channels.h"

it could be instead:

stdafx.h:
#include<afxwin.h> // MFC core and standard components
#include<afxext.h> // MFC extensions
#include<afxdisp.h> // MFC Automation classes

#include "A3d_List.h" // this your quest3d stuff?
#include "A3d_Channels.h" // this your quest3d stuff?

As for camimage.h, don't worry if it needs the MFC headers. Because you're using precompiled headers, you don't need to include the MFC headers in camimage.h.



Share this post


Link to post
Share on other sites
I have solved my problem.As long as i put MFC header files ahead of <windows.h> and use the /MTD to debug,it's ok!^_^,thanks for your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this