学习资料:
dl中如何装入文档和窗口(其实很简单,看DLL 学习3)
http://www.codeproject.com/KB/docview/sdicviewdll.aspx
http://www.codeproject.com/KB/docview/docviewfromdll.aspx
extern "C" AFX_EXT_API UINT Init(CRuntimeClass** view)
{
// Replace YourDLLName with the AFX_EXTENSION_MODULE variable above
// your DllMain.
new CDynLinkLibrary(Dll4DLL); //Dll4DLL为我当前的DLL名
// Replace CYourClass with the name of CView derived class you are exposing.
*view = RUNTIME_CLASS(CMyFormView); //CMyFormView是我的类
return(0);
}
注释:
1.AFX_EXT_API 是 MFC Extension DLL 可以使用的一个宏,定义成
#ifdef _AFXEXT
#define AFX_EXT_API __declspec( dllexport )
#else
#define AFX_EXT_API __declspec( dllimport )
#endif
DLL调用程序
void CMainFrame::OnViewLoadviewfromdll() { typedef UINT ( * LPDLLFUNC)(CRuntimeClass**); LPDLLFUNC lpfnDllFunc = NULL; HINSTANCE hDLL = NULL; hDLL = LoadLibrary("Dll4.dll"); if(hDLL) { lpfnDllFunc = (LPDLLFUNC)::GetProcAddress(hDLL,"Init"); if (!lpfnDllFunc) { AfxMessageBox("Function not found in DLL"); FreeLibrary(hDLL); return; } CRuntimeClass* pNewViewClass; lpfnDllFunc(&pNewViewClass); ASSERT(pNewViewClass); CSdiDllFramesDoc* pDoc = (CSdiDllFramesDoc*) GetActiveDocument(); ASSERT(pDoc); // Hmm, do we need this? pDoc->SwitchToView(pNewViewClass); } else { AfxMessageBox("Dll not found!");//DLL 装载失败 } }