![]() |
Desktop Development »
Dialogs and Windows »
Dialogs
Beginner
License: The Code Project Open License (CPOL)
Load a child dialog from a dynamic DLLBy GUNMANHow 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This article explains how to use a child window (dialog) exported from a dynamically loaded DLL.
Using the Visual Studio 6.0 environment, I created an MFC (SDI) application and selected the DLL and exported the symbols options.
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; } }
//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; } } }
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |