Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

An MFC extension library to enable DLL plug-in technology for your application using MESSAGE_MAPs

Rate me:
Please Sign up or sign in to vote.
4.85/5 (47 votes)
8 Jun 2004CPOL17 min read 577.1K   8.6K   238  
A plug-in architecture which allows you to write plug-in DLLs for your application and extend/modify its functionality.
// AppPlugInCore.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include <afxdllx.h>

#include "AppPlugInCore.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "AppPlugInCoreDoc.h"
#include "AppPlugInCoreView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAppPlugInCoreApp

BEGIN_MESSAGE_MAP(CAppPlugInCoreApp, CPlugInApp)
	//{{AFX_MSG_MAP(CAppPlugInCoreApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CPlugInApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CPlugInApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CPlugInApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAppPlugInCoreApp construction

CAppPlugInCoreApp::CAppPlugInCoreApp()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CAppPlugInCoreApp object

CAppPlugInCoreApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CAppPlugInCoreApp initialization

BOOL CAppPlugInCoreApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Plug-In Example application"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Load the plug-in DLL's.
	// This needs to be done before any document templates are registered and before the mainframe is created
	// as the plug-in DLL's can hook the mainframe message. Creation is a required message to hook
	// to allow additional toolbars and floating windows etc to be added.
	LoadPlugInDLLs() ;
	ReplaceDocManager() ;

	// Register the application's document templates.  Document templates
	// serve as the connection between documents, frame windows and views.

	CMyMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMyMultiDocTemplate(
		IDR_APPPLUTYPE,
		RUNTIME_CLASS(CAppPlugInCoreDoc),
		RUNTIME_CLASS(CChildFrame),
		RUNTIME_CLASS(CAppPlugInCoreView));
	AddDocTemplate(pDocTemplate);

	// now register any plug-in DLL document templates
	RegisterDLLDocumentTemplates() ;

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;
	UpdateMenus() ;
	UpdateAccelerators() ;
	InitialisePlugIns() ;

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CAppPlugInCoreApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CAppPlugInCoreApp message handlers


int CAppPlugInCoreApp::ExitInstance() 
{
	return CPlugInApp::ExitInstance();
}

static AFX_EXTENSION_MODULE PlugInLibraryDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	// Remove this if you use lpReserved
	UNREFERENCED_PARAMETER(lpReserved);

	if (dwReason == DLL_PROCESS_ATTACH)
	{
		TRACE0("PLUGINLIBRARY.DLL Initializing!\n");
		
		// Extension DLL one-time initialization
		if (!AfxInitExtensionModule(PlugInLibraryDLL, hInstance))
			return 0;

		// Insert this DLL into the resource chain
		// NOTE: If this Extension DLL is being implicitly linked to by
		//  an MFC Regular DLL (such as an ActiveX Control)
		//  instead of an MFC application, then you will want to
		//  remove this line from DllMain and put it in a separate
		//  function exported from this Extension DLL.  The Regular DLL
		//  that uses this Extension DLL should then explicitly call that
		//  function to initialize this Extension DLL.  Otherwise,
		//  the CDynLinkLibrary object will not be attached to the
		//  Regular DLL's resource chain, and serious problems will
		//  result.

		new CDynLinkLibrary(PlugInLibraryDLL);
	}
	else if (dwReason == DLL_PROCESS_DETACH)
	{
		TRACE0("PLUGINLIBRARY.DLL Terminating!\n");
		// Terminate the library before destructors are called
		AfxTermExtensionModule(PlugInLibraryDLL);
	}
	return 1;   // ok
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

Comments and Discussions