Click here to Skip to main content
13,290,735 members (73,402 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 6 Oct 2003

Using a View (dialog) exported from a dynamically loaded DLL (SDI) in WTL

, 6 Oct 2003
Rate this:
Please Sign up or sign in to vote.
An article on on how to load DLLs that export views (dialogs) into a SDI Application under WTL

Sample Image - SDIViewWTLDLL.jpg


In my effort to use WTL in almost all of my new personal projects I decided that I wanted to be able to load a dialog from a DLL, just like I did with MFC (see[^]).

The DLL Implementation

Using Visual Studio (7.1) project wizard, I created a Win32 Application and selected the DLL and Export Symbols options. I next decided that I needed an abstract interface for my FormView classes to derive from. The abstract interface creates a generic framework that allows classes to be derived from at will and define the methods exposed in the abstract interface. If you are not familiar with abstract interfaces see the references section.

Here’s what I came up with for my abstract interface:

struct  IDLLFormView
    virtual ~IDLLFormView() { }
    virtual HWND     Initialize(HWND hParentWnd) = 0;
    virtual void     Destroy() = 0;
    vitrual TCHAR*     Description() = 0;

With our abstract interface created we are a step closer to being able to load up dialogs into our WTL applications. The next thing that we need to do is to turn our attention to the GetPlugin() function, GetPlugin() will be the only exposed function in the DLL, here’s the declaration:

TESTWTLDLL_API void __stdcall GetPlugin( IDLLFormView** ppOutput );

and the implementation:

TESTWTLDLL_API __stdcall void GetPlugin( IDLLFormView** ppOutput )
//Note that error checking has been removed for clarity
    *ppOutput = new CReplaceFormView;

Here we are simply creating an instance of our CReplaceFormView and setting our pointer pointer appropriately. If you are familiar with MFC this is exactly the same as we would have done using the CRuntimeClass.

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.

Create a ALT/WTL application using the Visual Studio wizard. Under that Application Type tab select SDI and any other options that you may desire, some people like to code WTL in the .h file and others prefer the separation of interface from implementation - it’s really a mater of taste.

Under the User Interface Features in the View Type combo select Form (Dialog Base) and then click Finish. The next part is tricky and if anyone has a better way to do this let me know. What you have to do is unwire the FormView that is created with the application; of course you may want this dialog to be visible; if so leave it wired up.

The next thing that we need to do is create our dialog resource and then create a class that uses the dialog resource as it's template. Create a new ATL/WTL application using the WTL Wizard (you can find a version for VS7.1 here[^]). For the Application Type choose the SDI Application radio and under the User Interface Features choose Form(Dialog based) for the View type.

You can use a Generic window for the View type as there is just a tad bit more difficulty in doing so. But for now just choose the Form view type.

Here’s my class:
class CReplaceFormView : public IDLLFormView, public CDialogImpl

    HWND Initialize(HWND hParentWnd) 
        return this->Create(hParentWnd);
    void Destroy() { }      // nothing to destroy
    TCHAR* Description()
        return("Test loading Dialog from DLL");
    BOOL PreTranslateMessage(MSG* pMsg)
        return IsDialogMessage(pMsg);


LRESULT OnBnClickedButton1(WORD /*wNotifyCode*/, 
  WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);

The next step is to create a way to load up the dialog from the DLL. I chose to place a button on the applications toolbar and when clicked load up the dialog. Here’s the code to do just that:

void CMainFrame::LoadDLLView()
   HINSTANCE hLibrary = (HINSTANCE)::LoadLibrary( "testWTLDLL.dll" );
   typedef void (*GETPLUGIN)( IDLLFormView** );
   GETPLUGIN pfnLoad = (GETPLUGIN)::GetProcAddress( hLibrary, "GetPlugin" );
   if(pfnLoad == NULL)        
      DWORD dwLastError = ::GetLastError();
      LPVOID lpMsgBuf;
         (LPTSTR)&lpMsgBuf, 0, NULL );
      WTL::CString msg;
      msg.Format("GetProcAddress(\"GetPlugin\") Failed\r\nError "
         "Code: %ld\r\nReason:%s",
         dwLastError, lpMsgBuf); 
      ::MessageBox(NULL, msg, "Error", MB_OK );
      return 0;
   IDLLFormView* pOutput = 0;
   pfnLoad( &pOutput );
   m_hWndClient = pOutput->Initialize(m_hWnd);

This is all fairly common code to anyone that has used DLLs dynamically. If you are unfamiliar with the concepts you can find articles that explain these concepts on this site. Essentially, after we have loaded the DLL we GetProcAddress() for the only exported function in our DLL and then call that function passing in the address of our IDLLFormView*. It's probably wise to check the contents of this pointer before using the methods but in my example I have removed this error check.

Once we have called the Initialize() method of our DLL based class we simply call CMainFrame::UpdateLayout() to refresh the windows display.


This work is based on the work that myself and others have done under MFC. Once I got hooked on WTL I decided that I wanted to implement the same functionality in my new projects. Although I have used this method in production code with much success, it will probably find its way onto an archived CD of dead code as I (as most of you) make my way to .NET.


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

GeneralI Met Compile Error too ,Here is Fixed Source Code. Pin
kr.kim10-Oct-09 3:56
memberkr.kim10-Oct-09 3:56 
GeneralSource code doens't work Pin
Vincent_RICHOMME5-Sep-07 23:53
memberVincent_RICHOMME5-Sep-07 23:53 
GeneralRe: Source code doens't work Pin
lroels1-Oct-07 5:04
memberlroels1-Oct-07 5:04 
GeneralRe: Source code doens't work Pin
Vincent_RICHOMME1-Oct-07 6:06
memberVincent_RICHOMME1-Oct-07 6:06 
GeneralRe: Source code doens't work Pin
Mattamoo19-Jan-08 7:39
memberMattamoo19-Jan-08 7:39 
GeneralRe: Source code doens't work Pin
Vincent_RICHOMME20-Jan-08 2:26
memberVincent_RICHOMME20-Jan-08 2:26
GeneralWe can't load a CSplitterWindow view from a DLL!! Pin
Yu_Matrix2-Apr-07 8:57
memberYu_Matrix2-Apr-07 8:57 
GeneralRe: We can't load a CSplitterWindow view from a DLL!! Pin
lsmart15-Apr-11 9:10
memberlsmart15-Apr-11 9:10 
QuestionHow to load a MFC dll to a WTL project? Pin
freehawk18-Jan-04 16:29
memberfreehawk18-Jan-04 16:29 
GeneralActiveX Pin
TW9-Oct-03 2:59
memberTW9-Oct-03 2:59 
GeneralRe: ActiveX Pin
Dave Loeser1-Apr-04 19:10
memberDave Loeser1-Apr-04 19:10 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 7 Oct 2003
Article Copyright 2003 by Dave Loeser
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid