Click here to Skip to main content
6,595,854 members and growing! (17,241 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » Dialogs     Beginner License: The Code Project Open License (CPOL)

Load a child dialog from a dynamic DLL

By GUNMAN

How to load a child dialog from a dynamic DLL and display it in an EXE project as a child.
C++ (VC6, VC7, VC7.1, VC8.0), Windows, MFC, Dev
Posted:14 Mar 2008
Views:10,182
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.22 Rating: 2.45 out of 5
2 votes, 25.0%
1
2 votes, 25.0%
2
2 votes, 25.0%
3
1 vote, 12.5%
4
1 vote, 12.5%
5

LoadChild

Introduction

This article explains how to use a child window (dialog) exported from a dynamically loaded DLL.

Background

Using the Visual Studio 6.0 environment, I created an MFC (SDI) application and selected the DLL and exported the symbols options.

Using the code

The DLL implementation

Here's the declaration:

/*
*
*       Lee Gun-woo,
*       http://AiRPAGE.ORG
*       Load a child dialog from DLL !!
*
*/
// TestDlgDll.h
//
#ifndef __MYDLG_H__
#define __MYDLG_H__


#ifdef MYDLG_API_EXPORTS
#define MYDLG_API __declspec(dllexport)
#else
#define MYDLG_API __declspec(dllimport)
#endif

class CTestDlgDll
{
public:
 void CreateMyDialog(HWND hWnd);
 void CloseMyDialog();
protected:
 /** @brief constructor */
 CTestDlgDll();
 /** @brief destructor */
 virtual ~CTestDlgDll();
};


//////////////////////////////////////////////////////////////////////////
extern "C" MYDLG_API void CreateMyDlg(HWND hWnd);
extern "C" MYDLG_API void CloseMyDlg(void);

#endif //__MYDLG_H__

and the implementation:

// TestDlgDll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "TestDlgDll.h"
#include "TestDlg.h"
CTestDlg *m_pMyDlg;
//////////////////////////////////////////////////////////////////////////
// you should put the following code into you new dll.
//////////////////////////////////////////////////////////////////////////
extern "C" MYDLG_API void CreateMyDlg(HWND hWnd);
extern "C" MYDLG_API void CloseMyDlg(void);
CTestDlgDll* gpThisDLL;
MYDLG_API void CreateMyDlg(HWND hWnd)
{ 
    gpThisDLL->CreateMyDialog(hWnd);
}
MYDLG_API void CloseMyDlg()
{ 
    gpThisDLL->CloseMyDialog();
}
CTestDlgDll::CTestDlgDll()
{ 
    gpThisDLL = this;
    m_pMyDlg = NULL;
}
CTestDlgDll::~CTestDlgDll()
{
    CloseMyDialog(); 
}
void CTestDlgDll::CreateMyDialog(HWND hWnd)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
    if(m_pMyDlg == NULL)
    { 
        CWnd *pParent = CWnd::FromHandle(hWnd);
        m_pMyDlg = new CTestDlg(pParent);
        m_pMyDlg->Create(IDD_TESTDLG, pParent); 
        m_pMyDlg->ShowWindow(SW_SHOW); 
    } 
}
void CTestDlgDll::CloseMyDialog()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    
    if(m_pMyDlg != NULL)
    { 
        m_pMyDlg->DestroyWindow();
        delete m_pMyDlg; 
        m_pMyDlg = NULL;
    } 
}

The EXE(?) Implementation

//API type list (from dll)
typedef void (*PFnCreateMyDialog)(HWND hWnd);
typedef void (*PFnCloseMyDialog)(void);
HMODULE hDLL = NULL;
void CMainFrame::OnMydialogOn() 
{
    // TODO: Add your command handler code here
    if(hDLL == NULL)
    {
        CString dllPath = "";
        CFileDialog dlg(
        TRUE, 
        NULL, 
        NULL, 
        OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, 
        _T("dll files|*.dll|"), 
        AfxGetMainWnd());

    if( dlg.DoModal() == IDOK)
    {
        //get the dll path
        dllPath = dlg.GetPathName();
        //////////////////////////////////////////////////////
        // load library
        hDLL = LoadLibrary(dllPath);
        if( hDLL == NULL)
        {
            AfxMessageBox("LoadLibrary Error !!");
            return;
        }

        PFnCreateMyDialog pFnCreate = 
          (PFnCreateMyDialog)GetProcAddress(hDLL, "CreateMyDlg");

        if( pFnCreate == NULL)
        {
            FreeLibrary(hDLL);

            AfxMessageBox("Get API - Error !!");
            return;
        }

        //Pass the window handle to dll
        (pFnCreate)(this->GetSafeHwnd());
    } 
   }
   else
   {
    if(hDLL)
    {
        PFnCloseMyDialog pFnClose = 
          (PFnCloseMyDialog)GetProcAddress(hDLL, "CloseMyDlg"); 
        (pFnClose)();
        FreeLibrary(hDLL); 
        hDLL = NULL;
    } 
   }

}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

GUNMAN


Member
Windows/Linux Device driver,
Embedded Systems,
RTOS ...
Occupation: Systems Engineer
Company: http://airpage.org/
Location: Korea, Republic Of Korea, Republic Of

Other popular Dialogs and Windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
QuestionThe application crashes when I open the dll... PinmemberIvan Wilson at Embed Ltd4:19 8 Apr '09  
AnswerRe: The application crashes when I open the dll... Pinmemberbinyo666:26 21 May '09  
AnswerRe: The application crashes when I open the dll... PinmemberMakarandBRaut19:55 14 Jun '09  
GeneralMFC DLL Pinmemberrasmirv21:15 1 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Mar 2008
Editor: Smitha Vijayan
Copyright 2008 by GUNMAN
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project