In Dll,I create new class CODMMultiDoctemplate inhert CMultiDoctemplate,and CChildFrame,COnDemandCMDoc,COnDemandCMView .
First,create new Template
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.
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
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
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?