in the MFC Dll : one Dialog class :
(the dialog is not destroyed when it receives OnCancel or OnOK, it is oly hidden, the object itself is destroyed in OnPostNCDestroy, the last message received).
"Dlg.h"
#ifndef AFX_DLG_H_INCLUDED_
#define AFX_DLG_H_INCLUDED_
class CDlg : public CDialog
{
public:
CDlg();
BOOL Create(CWnd* pParent = NULL);
// Dialog Data
//{{AFX_DATA(CDlg)
enum { IDD = IDD_DIALOG };
//}}AFX_DATA
// Overrides
//{{AFX_VIRTUAL(CDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);
virtual void PostNcDestroy();
//}}AFX_VIRTUAL
// Generated message map functions
protected:
//{{AFX_MSG(CDlg)
virtual void OnCancel();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif // AFX_DLG_H_INCLUDED_
"Dlg.cpp"
// Dlg.cpp : implementation file
//
#include "StdAfx.h"
#include "TestDll.h"
#include "Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
BEGIN_MESSAGE_MAP(CDlg, CDialog)
//{{AFX_MSG_MAP(CDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CDlg::CDlg()
: CDialog()
{
//{{AFX_DATA_INIT(CDlg)
//}}AFX_DATA_INIT
}
void CDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg)
//}}AFX_DATA_MAP
}
BOOL CDlg::Create(CWnd* pParent /*= NULL*/)
{
if(!CDialog::Create(CDlg::IDD, pParent))return FALSE;
return TRUE;
}
void CDlg::OnCancel()
{
ShowWindow(SW_HIDE);
}
void CDlg::OnOK()
{
ShowWindow(SW_HIDE);
}
void CDlg::PostNcDestroy()
{
delete this;
}
in the MFC Dll, on exported function (createDlg)
"ImpExp.h"
#ifndef IMPEXP_H
#define IMPEXP_H
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API extern "C" __declspec(dllexport)
#else
#define TESTDLL_API extern "C" __declspec(dllimport)
#endif
TESTDLL_API HWND __stdcall CreateDlg(HWND hWndParent);
#endif // IMPEXP_H
"TestDll.cpp"
#define TESTDLL_EXPORTS
#include "ImpExp.h"
#include "Dlg.h"
TESTDLL_API HWND __stdcall CreateDlg(HWND hWndParent)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDlg* pDlg = new CDlg();
if(!pDlg->Create(CWnd::FromHandle(hWndParent))) return NULL;
return pDlg->m_hWnd;
}
"Main.cpp" (link with the MFC dll .lib file)
// Main.cpp : Defines the entry point for the application.
//
#include "StdAfx.h"
#include "resource.h"
#include "..\ImpExp.h"
HWND hModelessDlg;
int CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
hModelessDlg = CreateDlg(hDlg);
break;
}
case WM_DESTROY :
{
DestroyWindow(hModelessDlg);
break;
}
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDCANCEL:EndDialog(hDlg, IDCANCEL); break;
case IDOK:EndDialog(hDlg, IDOK); break;
case IDC_SHOW:
{
ShowWindow(hModelessDlg, SW_SHOW);
break;
}
case IDC_HIDE:
{
ShowWindow(hModelessDlg, SW_HIDE);
break;
}
}
}
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, DlgProc);
}
the IDD_MAINDIALOG Dialog contains 2 buttons IDC_SHOW and IDC_HIDE.