Separating the views of an MDI application into different DLLs
Different views can be seperated into different dlls in an MDI application:
Advantages:
I will try to explain this using an example. In my example, I have an EXE which I call Host. It is an MDI application. Then I have 2 views inside 2 DLLs. The name of these dlls are inside an INI file called HOST.INI.
The only catch is all DLLs that the HOST application can load must export a function by called Init(). Some kind of a rudimentary COM, eh? :)
At runtime, the HOST application loads the DLL & collects information from the INI file such as its CMultiDocTemplate pointer, the handle to the image to be displayed on the menu and string to be displayed on the menu to invoke the view.
All of this information is stored in a simple CList of a struct I defined.
struct __VIEWINFO
{
int iImage;
CString strLabel;
CMultiDocTemplate *pDocTemp;
};
In the OnCreate() function of the CMainFrame, the DLL is loaded.
HICON hIcon;
CString strLabel;
CMultiDocTemplate *pDocTemplate;
HINSTANCE hDLL;
LPDLLFUNC lpfnDllFunc;
UINT uReturnVal;
m_List.SubclassDlgItem(IDC_LIST, &m_wndMyDialogBar);
m_pImageList = new CImageList();
m_pImageList ->Create(32, 32, TRUE,1, 4);
int n = GetPrivateProfileInt("dll","Total",0,"host.ini");
for (int i = 0; i < n; i++)
{
CString strDllName;
strDllName.Format("dll%d",i+1);
GetPrivateProfileString("dll",strDllName,0,strDllName.GetBuffer(15),15,"host.ini");
strDllName.TrimRight();
if(strDllName.IsEmpty())
continue;
hDLL = LoadLibrary(strDllName);
lpfnDllFunc = (LPDLLFUNC)::GetProcAddress(hDLL,"Init");
if (!lpfnDllFunc)
FreeLibrary(hDLL);
else
{
uReturnVal = lpfnDllFunc(&hIcon,&strLabel,&pDocTemplate);
m_pImageList ->Add(hIcon);
__VIEWINFO *viewInfo = new __VIEWINFO;
viewInfo->iImage = m_pImageList->GetImageCount()-1;
viewInfo->strLabel = strLabel;
viewInfo->pDocTemp = pDocTemplate;
m_ViewList.AddTail(viewInfo);
}
}
m_List.SetImageList(m_pImageList,LVSIL_NORMAL);
POSITION pos = m_ViewList.GetHeadPosition();
i = 0;
while(pos != NULL)
{
__VIEWINFO *viewInfo = m_ViewList.GetNext(pos);
ASSERT(viewInfo);
int nIndex = m_List.InsertItem(i++,viewInfo->strLabel,viewInfo->iImage);
m_List.SetItemData(nIndex,(ULONG)viewInfo->pDocTemp);
}
When the user selects the menu item to invoke the view the view can be opened using the following code
{
int nSel = m_List.GetNextItem(-1, LVNI_SELECTED);
if(-1 == nSel)
return;
CMultiDocTemplate *pDocTemplate = (CMultiDocTemplate *)m_List.GetItemData(nSel);
pDocTemplate->OpenDocumentFile(NULL);
}
DLLS:
The DLLs are MFC extension DLLs. As I mentioned earlier, all of the DLLs that HOST will use need to
export a function called Init. It is inside this function that the DLL returns it's identity to the HOST application.
extern "C" AFX_EXT_API
UINT Init(HICON *hIcon,CString *strLabel,CMultiDocTemplate **pDocTemplate)
{
new CDynLinkLibrary(View1DLL);
CWinApp* pApp = AfxGetApp();
ASSERT(pApp != NULL);
*pDocTemplate = new CMultiDocTemplate(IDR_VIEW1TYPE,
RUNTIME_CLASS(CView1Doc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CView1View));
pApp->AddDocTemplate(*pDocTemplate);
*hIcon = pApp->LoadIcon(IDI_VIEW1ICON);
*strLabel = "View 1";
return 0;
}
I have put togother a very simple demo, which clearly explains the whole mechanism:
NOTE : Copy the ini file to the System directory
Date Last Updated: April 3, 1999

Comments
Release Version Error
Posted by Legacy on 09/15/2002 12:00amOriginally posted by: sahil
man it doesn't seems to be working with Release verrsion i m having assertion why is it.? it goes fine with debug version
ReplyResource problem can also be solved by...
Posted by Legacy on 04/02/2000 12:00amOriginally posted by: Kabir
I think the resource problem of mixing and duplicating same resorce names among diff. dll's can also be solved by maintaining a single ResourceDll for the project. Any new Dll that is added needs to make modifications to this Dll only. I know this solution is not as flexible as the original idea but since for most purposes i guess the dll'd would be developed by the same organization as the one developing applications , It would be a practical solution in most cases.
Reply...kabir
How to ...
Posted by Legacy on 01/23/2000 12:00amOriginally posted by: Goran
ReplyIt's not so simple..
Posted by Legacy on 12/21/1999 12:00amOriginally posted by: Olivier Raoul
ReplyVery cool, but
Posted by Legacy on 09/23/1999 12:00amOriginally posted by: Joerg Preiss
First tests seem to work, but I wanted to have a complete data editor with a list of items and a dialog box to edit and add items. If I have several dlls, the resources are not loaded correctly, I see in one view the dll resource of another dll. Also, I see a trace line that a dll is relocated because of a collision with another.
ReplyThe second thing: how can I connect to the menu bar inside such an extension dll?
Separating the views of an MDI application into different DLLs
Posted by Legacy on 08/28/1999 12:00amOriginally posted by: Steve Sun
Good idea, I love this and enjoy this code.
ReplyThank you.
How to use SDI (Single Document, Multi View..)
Posted by Legacy on 05/03/1999 12:00amOriginally posted by: Sung Hoon, Cho
How to use this methode for SDI (single document , Multi View..)
View is CFormView....
Let me know !!
ReplyUse regular DLL?
Posted by Legacy on 04/20/1999 12:00amOriginally posted by: Vadim
Any ideas how to do the same in regular DLL, so 'host' can be any MDI app, not only MFC app?
Reply