Click here to Skip to main content
11,923,046 members (66,444 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked

Using a Doc/View exported from a dynamically loaded DLL (SDI)

, 6 May 2002
Rate this:
Please Sign up or sign in to vote.
An article on on how to load DLLs which export views into a SDI Application
<!-- Download Links --> <!-- Article image -->

Sample Image - SdiCViewDll.gif

<!-- Add the rest of your HTML here -->


In a recent project I needed to be able to dynamically load CFormViews into the MainFrame of my application. I searched the developer sites and found a couple articles that explained how to load document/views into MDI applications but unfortunately, I needed to use the SDI approach. I searched MSDN and found even more articles discussing the technique under MDI's. I eventually decided that it shouldn't be too difficult and here is the result.

I started out by referencing an article on entitled "Replacing a view in a doc-view application" by Jorge Lodos Vigil. That article gave me the basics for switching view in an SDI application. The next problem to solve was how to export the CFormView (or CView derived class) from a DLL and what kind of DLL? In the MFC AppWizard(dll) you have three types of DLLs that you can create, they are:

  • Regular DLL with MFC statically linked
  • Regular DLL using shared MFC DLL
  • MFC Extension DLL (using shared MFC DLL)

After some research and the usual trial and error, I discovered that the third choice MFC Extension DLL was the proper DLL type for my requirements. An MFC Extension DLL will only work from an MFC application and in this case that's exactly what was needed. There are other distinctions between the types of DLLs and I urge you to discover them on your own.

The DLL implementation

After creating an MFC Extension DLL project go to the ResourceView in the Workspace and create a new Dialog Resource of type IDD_FORMVIEW. Next select 'Insert|New Class... ' from the main menu and create a new CFormView derived class using the new dialog as the Dialog ID. Add the controls and functionality as you normaly would and then add this code under the DllMain(), ensuring that you replace the variables as explained in the code.

extern "C" AFX_EXT_API UINT Init(CRuntimeClass** view)
    // Replace YourDLLName with the AFX_EXTENSION_MODULE variable above 
    // your DllMain.
    new CDynLinkLibrary(YourDLLName);	
    // Replace CYourClass with the name of CView derived class you are exposing.
    *view = RUNTIME_CLASS(CYourClass);

That's pretty much all you need to do for the DLL side of things. Looks pretty simple, right? Let's move onto the main application or the 'host'.

The 'Host' application

There are endless ways that we can set up the ‘host’ application to load up the DLLs and import the views. In this example I’m not going to do anything fancy, as I want you to understand how to accomplish the task. I will explain the method I have chosen, based on my requirements, at the end of the article so stay tuned.

Create an MFC executable project using the Single Document (SDI) option, you can modify all the options in the Appwizard but on the last page select CView or a CView derived class as the Base class.

Select the ResourceView tab in the Workspace, open the Menu treeitem then double-click on the IDR_MAINFRAME and add a menuitem under the View menu or create a new menu - it makes no difference. Then select your new menuitem and bring up the Class Wizard and add command routing for it, here is the code that will load up the DLL, grab the exported function and retrieve the CRuntimeClass.

void CMainFrame::OnViewLoadviewfromdll() 
    typedef UINT ( * LPDLLFUNC)(CRuntimeClass**);
    LPDLLFUNC lpfnDllFunc = NULL;
    HINSTANCE hDLL = NULL;     	  
    hDLL = LoadLibrary("InitialContact.dll");
        lpfnDllFunc = (LPDLLFUNC)::GetProcAddress(hDLL,"Init_");
        if (!lpfnDllFunc)
	    AfxMessageBox("Function not found in DLL");
	CRuntimeClass* pNewViewClass;
	CSdiDllFramesDoc* pDoc = (CSdiDllFramesDoc*) GetActiveDocument();
	ASSERT(pDoc); // Hmm, do we need this?
        AfxMessageBox("Dll not found!");

For clarity, I present the code from Jorge Lodos Vigil's article referenced above (comments removed):

BOOL CYourDoc::SwitchToView(CRuntimeClass* pNewViewClass)
   CFrameWnd* pMainWnd = (CFrameWnd*)AfxGetMainWnd();
   CView* pOldActiveView = pMainWnd->GetActiveView();

   if (pOldActiveView->IsKindOf(pNewViewClass))
      return TRUE;
   ::SetWindowLong(pOldActiveView->m_hWnd, GWL_ID, 0);

   CCreateContext context;
   context.m_pNewViewClass = pNewViewClass;
   context.m_pCurrentDoc = this;
   CView* pNewView = STATIC_DOWNCAST(CView, pMainWnd->CreateView(&context));
   if (pNewView != NULL)

      return TRUE;

   return FALSE;

There really isn't anything new here. We have typedef'ed a function pointer and created a variable of that type. We then loaded the library, retrieved the address of our exported function and loaded that into our function pointer. Then, using a CRuntimeClass we passed that into our exported function via the function pointer. We then grab a pointer to our CDocument class and call the member SwitchToView() passing in the CRuntimeClass that we retrieved from the DLL.

All in all it was much easier than I thought and I say that with about three failed attempts, having started with the first DLL type and working my way down the list.

An Advanced Example

The requirements in my implementation were that the menuitems were to be read from a database. The table in the database held the following information:

  • Menu Caption
  • Menu ID
  • DLL Name
  • Function Name

With that I decided upon this class framework to hold the information read in from the database. I also provided the class the ability to LoadPlugin() and UnLoadPlugin() the DLLs. Next, I created a CMap to hold each instance of the HSMenuItem class and set the menu id up to be the key. This seemed to be a logical choice and after you see how I handle the menu selections you'll see the ease of use and extensibility that the 'host' application provides. Here's the HSMenuItem class:

class HSMenuItem
    HSMenuItem(CString _MenuCaption, DWORD _MenuID, 
               CString _LibraryName, CString _FuncName) :
               m_MenuCaption(_MenuCaption), m_ButtonID(_ButtonID), 
               m_LibraryName(_LibraryName), m_FuncName(_FuncName), 
        m_lpfnDllFunc = NULL;
        m_hDLL = NULL;
    BOOL LoadPlugin()
        m_hDLL = LoadLibrary(m_LibraryName);
	m_lpfnDllFunc = (LPDLLFUNC)::GetProcAddress(m_hDLL,m_FuncName);
	if (!m_lpfnDllFunc)
        m_bLoaded = TRUE;
    BOOL UnLoadPlugin()
	    m_lpfnDllFunc = NULL;
    typedef UINT ( * LPDLLFUNC)(CRuntimeClass**);
    LPDLLFUNC m_lpfnDllFunc;

    CString m_MenuCaption;   // The caption on the menu
    DWORD   m_MenuID;    // The menu ID - used in OnCommand()
    CString m_LibraryName;    // The DLLs name
    CString m_FuncName;       // The initilizer function

    BOOL m_bLoaded;

typedef CMap <DWORD,DWORD,HSMenuItem*,HSMenuItem*> mapMenuItems;

Loading the Menus up from the database and dynamically creating them is an exercise left to the reader. I will show you how easy it was to handle the menu options. First I hooked up my menus to go through the message map using ON_COMMAND_RANGE routing that to my HandleMenu(UINT menuid) function that looks like this:

BOOL CMainFrame::HandleMenu(UINT menuid) 
    if (menuid != 0) 
        HSMenuItem* mi = NULL;
        if(mi != NULL)
	    CRuntimeClass* runtimeclass;
	    CAlarmAssistantDoc* pDoc = (CAlarmAssistantDoc*) GetActiveDocument();

As you can see this is very flexible and will load up any CView derived class that you have exported from a DLL. I've found this approach very useful in projects where you can deliver optional functionality in stages. Even better, bug fixes do not require a full recompile of the application - just the DLL.

My thanks go out to Mr. Vigil for his SwitchToView() as well as all the other contributors to


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dave Loeser
Web Developer
United States United States
Dave has been programming for the past 20+ years first on a variety of platforms and operating systems using various languages. As a hobbyist Dave cut his teeth on the Commodore Pet and the 64 coding in basic and then moving to 6502 ASM. Dave moved to the Amiga using 68000 ASM and then C. His knowledge of the C language offered the stepping stone for him to make his hobby his profession taking a position coding C on an AIX Unix platform. Since then he has worked on many flavors of Unix, QNX, Windows (3.11 – present), and has been coding games for his Pocket PC in his spare time.

Dave lives in Indiana with his two teenage daughters and two cats.

You may also be interested in...

Comments and Discussions

Question"Using a Doc/View exported from a dynamically loaded DLL (SDI) " do not be downloaded Pin
Member 1041359320-Nov-13 14:53
memberMember 1041359320-Nov-13 14:53 
GeneralMy vote of 5 Pin
JasMineLeaf14-Oct-13 1:19
memberJasMineLeaf14-Oct-13 1:19 
GeneralAdd _AFXDLL into the EXE project to solve the RUNTIME_ERROR of this demo Pin
Jaken Tong7-Nov-09 7:25
memberJaken Tong7-Nov-09 7:25 
QuestionRuntime Error using VC++ 8 Pin
batang_igat9-May-07 22:50
memberbatang_igat9-May-07 22:50 
QuestionCan't make it work with CHTMLView Pin
lviolette14-Jan-07 15:31
memberlviolette14-Jan-07 15:31 
GeneralUse Custom Control Pin
Member #48493911-Jan-07 19:27
memberMember #48493911-Jan-07 19:27 
Generaldll 32 and mfc Pin
azucop24-Jan-06 22:50
memberazucop24-Jan-06 22:50 
Questionat Win32 API ? Pin
desccode6-Mar-05 21:48
memberdesccode6-Mar-05 21:48 
GeneralXP-Style and password edit control Pin
pinyi9-Sep-04 22:30
memberpinyi9-Sep-04 22:30 
Generalmdi Pin
tuxyboy15-Aug-04 20:14
membertuxyboy15-Aug-04 20:14 
GeneralRe: mdi Pin
Dave Loeser16-Aug-04 5:15
memberDave Loeser16-Aug-04 5:15 
GeneralCDocument access by the DLL Pin
MartinWN15-Jul-03 9:11
memberMartinWN15-Jul-03 9:11 
GeneralRe: CDocument access by the DLL Pin
Dave Loeser18-Jul-03 15:13
memberDave Loeser18-Jul-03 15:13 
GeneralRe: CDocument access by the DLL Pin
iProgram (WangYue)22-Aug-03 0:51
memberiProgram (WangYue)22-Aug-03 0:51 
GeneralRegular DLL Displaying CFrameWnd Pin
Alain H.28-Jun-03 9:14
memberAlain H.28-Jun-03 9:14 
GeneralRe: Regular DLL Displaying CFrameWnd Pin
Dave Loeser18-Jul-03 15:28
memberDave Loeser18-Jul-03 15:28 
GeneralRe: Regular DLL Displaying CFrameWnd Pin
Alain HJ.29-Jul-03 5:25
memberAlain HJ.29-Jul-03 5:25 
QuestionWhy not opening a database in the exported view? Pin
zuoch7-Jun-03 4:14
memberzuoch7-Jun-03 4:14 
AnswerRe: Why not opening a database in the exported view? Pin
Dave Loeser18-Jul-03 15:21
memberDave Loeser18-Jul-03 15:21 
GeneralCan't use Custom controls in exported views Pin
pimOOsse6-Mar-03 1:49
memberpimOOsse6-Mar-03 1:49 
GeneralRe: Can't use Custom controls in exported views Pin
Dave Loeser7-Jun-03 9:57
memberDave Loeser7-Jun-03 9:57 
GeneralRe: Can't use Custom controls in exported views Pin
Florian DREVET7-Jun-03 10:01
sussFlorian DREVET7-Jun-03 10:01 
GeneralRe: Can't use Custom controls in exported views Pin
Dave Loeser9-Jun-03 3:15
memberDave Loeser9-Jun-03 3:15 
GeneralRe: Can't use Custom controls in exported views Pin
galroy30-Dec-04 11:01
membergalroy30-Dec-04 11:01 
AnswerRe: Can't use Custom controls in exported views Pin
redjesred11-Jan-07 21:53
memberredjesred11-Jan-07 21:53 
GeneralJust for general information Pin
Mazerim11-Feb-03 15:40
memberMazerim11-Feb-03 15:40 
GeneralRe: Just for general information Pin
Dave Loeser18-Jul-03 15:29
memberDave Loeser18-Jul-03 15:29 
GeneralGood Stuff Dave Pin
James Spibey5-Oct-02 12:47
editorJames Spibey5-Oct-02 12:47 
QuestionResource Leak? Pin
TomPeakz15-Aug-02 17:27
memberTomPeakz15-Aug-02 17:27 
AnswerRe: Resource Leak? Pin
Dave Loeser7-Oct-02 4:51
memberDave Loeser7-Oct-02 4:51 
GeneralRe: Resource Leak? Pin
Dave Loeser10-Oct-02 5:30
memberDave Loeser10-Oct-02 5:30 
GeneralUsing this approach in MDI applications Pin
Jens Scheidtmann25-Jul-02 3:24
memberJens Scheidtmann25-Jul-02 3:24 
GeneralRe: Using this approach in MDI applications Pin
Dave Loeser11-Feb-03 8:22
memberDave Loeser11-Feb-03 8:22 
GeneralFollow Up Pin
Dave Loeser2-Jun-02 16:52
memberDave Loeser2-Jun-02 16:52 
GeneralRe: Follow Up Pin
Aqiruse26-May-03 18:41
memberAqiruse26-May-03 18:41 
GeneralSDI in regular Dll Pin
surbinsho31-May-02 21:02
membersurbinsho31-May-02 21:02 
GeneralRe: SDI in regular Dll Pin
Dave Loeser2-Jun-02 16:48
memberDave Loeser2-Jun-02 16:48 
GeneralRe: SDI in regular Dll Pin
surbinsho3-Jun-02 0:12
membersurbinsho3-Jun-02 0:12 
GeneralRe: SDI in regular Dll Pin
Sadhana Raj27-Dec-02 19:00
memberSadhana Raj27-Dec-02 19:00 
GeneralRe: SDI in regular Dll Pin
Dave Loeser31-Dec-02 6:13
memberDave Loeser31-Dec-02 6:13 
GeneralRe: SDI in regular Dll Pin
Sadhana Raj1-Jan-03 19:10
memberSadhana Raj1-Jan-03 19:10 
GeneralRe: SDI in regular Dll Pin
zhlqcn9-Apr-03 22:33
memberzhlqcn9-Apr-03 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 7 May 2002
Article Copyright 2002 by Dave Loeser
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid