MDI动态加载文档/视图问题 100分
在.dll中:
CMultiDocTemplate* Cdll009::myinit()
{
BOOL bSuccess = FALSE;
CMultiDocTemplate* pDocTemplate;
CMultiDocTemplate* pDocTemplate2;
CWinApp* pApp = AfxGetApp();
ASSERT(pApp);
if (pApp)
{
pDocTemplate = new CMultiDocTemplate(
IDR_MENU1,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CMDIChildWnd),
RUNTIME_CLASS(CMyView));
pDocTemplate2 = new CMultiDocTemplate(
IDR_MENU1,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CMDIChildWnd),
RUNTIME_CLASS(CMyView2));
ASSERT(pDocTemplate);
if (pDocTemplate)
{
pApp->AddDocTemplate(pDocTemplate);
bSuccess = TRUE;
}
}
return pDocTemplate2;
}
在CTest009App类中:
void CTest009App::OnNewt()
{
typedef CMultiDocTemplate* (*MYINIT)();
MYINIT fnMyinit;
FARPROC lpfn=NULL;
HINSTANCE hinst = NULL;
hinst = LoadLibrary("../dll009/Debug/dll009.dll");
if (hinst == NULL)
{
AfxMessageBox("can not load dll");
return;
}
lpfn = GetProcAddress(hinst,"myinit");
fnMyinit = (MYINIT)lpfn;
if(lpfn==NULL)
{
AfxMessageBox("can not get template");
return;
}
pDocTemplate2 = fnMyinit();
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
cmdInfo.m_nShellCommand = ::CCommandLineInfo.FileNew;
if (!ProcessShellCommand(cmdInfo))
return;
//取得主框架窗口的指针
CWnd* pMainWnd = AfxGetApp()->m_pMainWnd;
CMainFrame* pMainFrame = (CMainFrame*)pMainWnd;
pMainFrame->OnTestNew();//重载的OnWindowNew()
pMainFrame->MDITile();
}
mainfrm中重载的OnWindowNew()
void CMainFrame::OnTestNew()
{
CMDIChildWnd* pActiveChild = MDIGetActive();
CDocument* pDocument;
if (pActiveChild == NULL ||
(pDocument = pActiveChild->GetActiveDocument()) == NULL)
{
TRACE0("Warning: No active document for WindowNew command.\n");
AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
return; // command failed
}
// otherwise we have a new frame !
CDocTemplate* pTemplate = ((CTest009App*) AfxGetApp())->pDocTemplate2;//pDocument->GetDocTemplate();
ASSERT_VALID(pTemplate);
CFrameWnd* pFrame = pTemplate->CreateNewFrame(pDocument, pActiveChild);
if (pFrame == NULL)
{
TRACE0("Warning: failed to create new frame.\n");
return; // command failed
}
pTemplate->InitialUpdateFrame(pFrame, pDocument);
}
=======================
问题:
在程序第一次加载dll并调用CTest009App::OnNewt()时,一切正常:先构建mainframe,然后加载dll
创建基于一个文档的2个视图,但当关闭2个视图后,在次调用CTest009App::OnNewt()出错
请高手们帮着解决一下,如何能在关闭子窗口时把程序还原成没调用dll和Cdll009::myinit()时的状态(关闭自窗口后再FreeLibrary出错)
分不够再加~