Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Dll,I create new class CODMMultiDoctemplate inhert CMultiDoctemplate,and CChildFrame,COnDemandCMDoc,COnDemandCMView .
First,create new Template
VB
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_pOnDemandCMTemplate = new CODMMultiDocODMTemplate(hinst,
    203,
    RUNTIME_CLASS(COnDemandCMDoc),
    RUNTIME_CLASS(CChildFrame),  // custom MDI child frame
    RUNTIME_CLASS(COndemandCmView)
    );
ASSERT_KINDOF(CODMMultiDocODMTemplate,m_pOnDemandCMTemplate);


then,call openDocument function but failed.
VB
CDocument* CReaderODMApp::opendocument()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
	ASSERT(!lpTitle.IsEmpty());		
	AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
	
	COnDemandCMDoc* pDocument = new COnDemandCMDoc;
	if (pDocument == NULL)
	{
		TRACE0("CDocTemplate::CreateNewDocument returned NULL.\n");
		AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
		return NULL;
	}
	ASSERT_VALID(pDocument);
	
	BOOL bAutoDelete = pDocument->m_bAutoDelete;
	pDocument->m_bAutoDelete = FALSE;   // don't destroy if something goes wrong
	CFrameWnd* pFrame =m_pOnDemandCMTemplate->CreateNewFrame(pDocument, NULL);
	pDocument->m_bAutoDelete = bAutoDelete;
	if (pFrame == NULL)
	{
		AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
		delete pDocument;       // explicit delete on error
		return NULL;
	}
	ASSERT_VALID(pFrame);
	
	// open an existing document
	CWaitCursor wait;
	if (!pDocument->OnOpenDocument(lpTitle))
	{
		// user has be alerted to what failed in OnOpenDocument
		TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
		pFrame->DestroyWindow();
		return NULL;
	}
	pDocument->SetPathName(lpTitle,FALSE);
	m_pOnDemandCMTemplate->InitialUpdateFrame(pFrame, pDocument, bMakeVisible);
	return pDocument;
}


I debug it ,and i found a
CFrameWnd* pFrame =m_pOnDemandCMTemplate->CreateNewFrame(pDocument, NULL);
exception here.
then i found
VB
BOOL CMDIChildWnd::Create(LPCTSTR lpszClassName,
	LPCTSTR lpszWindowName, DWORD dwStyle,
	const RECT& rect, CMDIFrameWnd* pParentWnd,
	CCreateContext* pContext)
{
	if (pParentWnd == NULL)
	{
		CWnd* pMainWnd = AfxGetThread()->m_pMainWnd;
		ASSERT(pMainWnd != NULL);
		ASSERT_KINDOF(CMDIFrameWnd, pMainWnd);
		pParentWnd = (CMDIFrameWnd*)pMainWnd;
	}
....
}

ASSERT(pMainWnd != NULL); exception here ,pMainWnd is NULL
why?
Posted

1 solution

Probably, you have to set the CReaderODMApp::m_pMainWnd
to the application's main frame window in the function:
/*virtual*/ BOOL CReaderODMApp::InitInstance() :)

Part Two:
// exe-app.h
class CYourExeApp : public CWinApp
{
  // class CODMMultiDocODMTemplate is exported by your Dll:
  CODMMultiDocODMTemplate* m_pcDllTemplate;
...
public:
...
  // to be visible from the Dll-side:
  CODMMultiDocODMTemplate* GetDllTemplate() { return pcDllTemplate; };
};

// exe-app.cpp
CYourExeApp::CYourExeApp()
{
...
  m_pcDllTemplate = NULL;
...
}

BOOL CYourExeApp::InitInstance()
{
...
  InitYourDllTemplate();
...
}

void CYourExeApp::InitYourDllTemplate()
{
  /*CODMMultiDocODMTemplate* member, by Dll exported class*/
  m_pcDllTemplate = new CODMMultiDocODMTemplate(IDR_YOUR_ID,
                                                RUNTIME_CLASS(COnDemandCMDoc),
                                                RUNTIME_CLASS(CChildFrame),
                                                RUNTIME_CLASS(COndemandCmView));
  m_pcDllTemplate->SetContainerInfo(IDR_YOUR_ID);
  AddDocTemplate(m_pcDllTemplate); // good luck ! :)
}


Please do not use (remove) any "managers" in your DLL, but:
#include "..\YourApp\YourApp.h"
void CSomeDllClass::SomeDllFunction()
{
  ...
  // we are _inside_ , and this is _our_ app-object:
  CYourExeApp* pcApp = (CYourExeApp*) AfxGetApp();
  ASSERT(pcApp);

  COnDemandCMDoc* pcDoc = CDSTATIC_DOWNCAST(COnDemandCMDoc,
    pcApp->GetDllTemplate()->OpenDocumentFile((LPCTSTR) NULL));
  ...
}


The classes:
- CODMMultiDocODMTemplate
- COnDemandCMDoc
- COndemandCmView
...should be defined, implemented and exported by your Dll :)
 
Share this answer
 
v2
Comments
hsx_solon 28-Sep-10 3:55am    
but CreaderODMApp is not inherit CWinApp, it just a manager class, so...
Eugen Podsypalnikov 28-Sep-10 3:56am    
But your combination EXE&DLL will need a main frame,
is there any frame at the exe-app ?.. :)
hsx_solon 28-Sep-10 3:58am    
At the exe-app has main frame,and in dll just CChildFrame class.
hsx_solon 28-Sep-10 4:01am    
Shall i instance a CChildFrame in dll?
Eugen Podsypalnikov 28-Sep-10 4:02am    
Very good :)
Why do you want to use any managers (and AFX_MANAGE_STATE too)
and not just export/import your classes by DLL/EXE ? :)

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