YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

 


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


AfxGetStaticModuleState and LoadLibrary of GetProcAddress and FreeLibrary MFC ARTICLE with LOAD DLL

 
 

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:

Collapse Copy Code
/*
*
*       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:

Collapse Copy Code
// 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

Collapse Copy Code
//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;
    } 
   }

}

 

 

 

Copyright ?1998-2024 UCanCode.Net Software , all rights reserved.
Other product and company names herein may be the trademarks of their respective owners.

Please direct your questions or comments to webmaster@ucancode.net