Click here to Skip to main content
15,902,198 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: label background color Pin
xai24-Sep-02 14:06
xai24-Sep-02 14:06 
QuestionWho called InitiateSystemShutdown? Pin
Todd Smith24-Sep-02 13:54
Todd Smith24-Sep-02 13:54 
QuestionHow to rename a project in VS.Net??? Pin
LukeV24-Sep-02 13:06
LukeV24-Sep-02 13:06 
AnswerRe: How to rename a project in VS.Net??? Pin
Nick Hodapp24-Sep-02 13:23
sitebuilderNick Hodapp24-Sep-02 13:23 
GeneralDLL plugin help Pin
DarrollWalsh24-Sep-02 11:52
DarrollWalsh24-Sep-02 11:52 
GeneralRe: DLL plugin help Pin
Tomasz Sowinski24-Sep-02 12:26
Tomasz Sowinski24-Sep-02 12:26 
GeneralRe: DLL plugin help Pin
Pavel Klocek24-Sep-02 12:36
Pavel Klocek24-Sep-02 12:36 
GeneralRe: DLL plugin help Pin
Roger Allen25-Sep-02 3:05
Roger Allen25-Sep-02 3:05 
I am currently writing an MFC extension DLL to handle plug-ins. It should hopefully be finished some time in the next few weeks. It will have all the bells n whistles, but it sounds like you need a very small sub-set of options. Th best bet would be to write a DLL wrapper class which can be passed a filename of a DLL, loads it, gets all the function pointers and such and allows you to call from it. As an example, here is some of the code from my library that does this. It may be of help.


// (c) R.I.Allen 2002
// You may use this code in anyway that you feel, no guarantees or waranties are implied
// please keep all headers with any source used.
//
// CDLLWrapper class
// This class handles dynamically loaded DLL's that are plugged into the main application
// When an object is constructed and passed a filename of a DLL to wrap, it will gain 
// the required function pointers for the plug-in architecture through the use of GetProcAddress()
// These are the standard functions supplied by all plug-in DLL's to suppport the plug-in
// architecture. Only one of these object can wrap a DLL at any time, so if an operator=
// is used, the object being assigned from loses its copy of the DLL information to avoid
// DLL's being released by local object going out of scope etc.
//
// Changes made:
// Version 1.000 3rd September 2002
// Initial development

#ifndef CDLLwrapper_included
#define CDLLwrapper_included 1

class CMyMultiDocTemplate ;

// these are the prototypes of all the wrapped DLL plug-in functions
// document template support for plug-in document types
typedef int (* GETDOCUMENTTEMPLATECOUNT)() ;
typedef CMyMultiDocTemplate* (* GETDOCTEMPLATE)(int index) ;
// intialisation/deintialisation procedures
typedef void (* INITIALISEDLL)(CWinApp *pApp) ;
typedef void (* RELEASEDLL)() ;
// plug-in message map support
typedef AFX_MSGMAP* (* GETPREMESSAGEMAP)(LPCTSTR pClassname) ;
typedef AFX_MSGMAP* (* GETPOSTMESSAGEMAP)(LPCTSTR pClassname) ;

class _PLUGIN_API CDLLWrapper
{
public:
							CDLLWrapper() ;								// default constructor
							CDLLWrapper(CString& filename) ;			// filename constructor
							CDLLWrapper(CDLLWrapper& other) ;			// copy constructor
	virtual					~CDLLWrapper() ;							// destructor

	bool					LoadDLL(CString filename) ;					// true is load successful
	bool					FreeDLL() ;
	CDLLWrapper&			operator=(CDLLWrapper& other) ;				// does not work as regular operator=

	// Plug-in DLL wrapper functions
	void					InitialiseDLL(CWinApp *pApp) ;
	void					ReleaseDLL() ;
	int						GetDocTemplateCount() ;
	CMyMultiDocTemplate*	GetDocTemplate(int index) ;
	// message map DLL plug ins
	AFX_MSGMAP*				GetPreMessageMap(LPCTSTR pClassName) ;
	AFX_MSGMAP*				GetPostMessageMap(LPCTSTR pClassName) ;
private:
	// member variables
	CString					m_filename;		// filename of DLL
	HINSTANCE				m_DLLhInstance ;// Instance of DLL

	// function pointers. Each of these function pointers has a "wrapper" function in the class
	// which allows the rest of he code not to worry about whether the pointer is NULL or not
	// DLL initialisation / de-initialisation
	INITIALISEDLL				m_pFNInitialiseDLL ;
	RELEASEDLL					m_pFNReleaseDLL ;
	// document template support
	GETDOCUMENTTEMPLATECOUNT	m_pFNGetDocTemplateCount ;
	GETDOCTEMPLATE				m_pFNGetDocTemplate ;
	// message map support
	GETPREMESSAGEMAP			m_pFNGetPreMessageMap ;
	GETPOSTMESSAGEMAP			m_pFNGetPostMessageMap ;
} ;

#endif // CDLLwrapper_included


#include "stdafx.h"
#include "DLLwrapper.h"
#include "MyMultiDocTemplate.h"


CDLLWrapper::CDLLWrapper()
{
	m_DLLhInstance = NULL ;				// no DLL loaded
	m_filename = "" ;					// no filename

	// intialise function pointers
	m_pFNGetDocTemplateCount = NULL ;
	m_pFNGetDocTemplate = NULL ;
	m_pFNInitialiseDLL = NULL ;
	m_pFNReleaseDLL = NULL ;
	m_pFNGetPreMessageMap = NULL ;
	m_pFNGetPostMessageMap = NULL ;
}

CDLLWrapper::CDLLWrapper(CDLLWrapper& other)
{
	// use the operator= functionality so its all done in one place
	*this = other ;
}

CDLLWrapper::~CDLLWrapper()
{
	// release any loaded DLL
	FreeDLL() ;
}


bool CDLLWrapper::LoadDLL(CString filename)
{
	m_filename = filename ;
	m_DLLhInstance = LoadLibrary(filename) ;

	if (m_DLLhInstance != NULL)
		{
		// get pointers to functions
		// add others here, if you add your own wrapper functions. Note also you
		// must modify the copy constructor and the operator= as well!
		m_pFNGetDocTemplateCount = (GETDOCUMENTTEMPLATECOUNT)GetProcAddress(m_DLLhInstance, "GetDLLDocTemplateCount") ;
		m_pFNGetDocTemplate = (GETDOCTEMPLATE)GetProcAddress(m_DLLhInstance, "GetDLLDocTemplate") ;
		m_pFNInitialiseDLL = (INITIALISEDLL)GetProcAddress(m_DLLhInstance, "InitialiseDLL") ;
		m_pFNReleaseDLL = (RELEASEDLL)GetProcAddress(m_DLLhInstance, "ReleaseDLL") ;
		m_pFNGetPreMessageMap = (GETPREMESSAGEMAP)GetProcAddress(m_DLLhInstance, "GetPreMessageMap") ;
		m_pFNGetPostMessageMap = (GETPOSTMESSAGEMAP)GetProcAddress(m_DLLhInstance, "GetPostMessageMap") ;
		}
	return (m_DLLhInstance != NULL) ;
}

bool CDLLWrapper::FreeDLL()
{
	// release any loaded DLL
	if (m_DLLhInstance != NULL)
		{
		FreeLibrary(m_DLLhInstance) ;		// release library
		m_DLLhInstance = NULL ;
		m_filename = "" ;
		// kill any function pointers
		m_pFNGetDocTemplateCount = NULL ;
		m_pFNGetDocTemplate = NULL ;
		m_pFNInitialiseDLL = NULL ;
		m_pFNReleaseDLL = NULL ;
		m_pFNGetPreMessageMap = NULL ;
		m_pFNGetPostMessageMap = NULL ;
		}
	return true ;
}

CDLLWrapper& CDLLWrapper::operator=(CDLLWrapper& other)
{
	// NOTE: This operator= does not work as a regular operator= in that both object will not be the same
	// after assignment! If the local object wraps a DLL already, then it is freed. The other object gets
	// copied across and removed from the other object, so that 2 CDLLWrapper object cannot wrap the same
	// object. This makes them unique
	FreeDLL() ;						// free our DLL if we have one
	m_filename = other.m_filename ;	other.m_filename = "" ;
	m_DLLhInstance = other.m_DLLhInstance ; other.m_DLLhInstance = NULL ;
	m_pFNGetDocTemplateCount = other.m_pFNGetDocTemplateCount ; other.m_pFNGetDocTemplateCount = NULL ;
	m_pFNGetDocTemplate = other.m_pFNGetDocTemplate ; other.m_pFNGetDocTemplate = NULL ;
	m_pFNInitialiseDLL = other.m_pFNInitialiseDLL ; other.m_pFNInitialiseDLL = NULL ;
	m_pFNReleaseDLL = other.m_pFNReleaseDLL ; other.m_pFNReleaseDLL  = NULL ;
	m_pFNGetPreMessageMap = other.m_pFNGetPreMessageMap  ; other.m_pFNGetPreMessageMap = NULL ;
	m_pFNGetPostMessageMap = other.m_pFNGetPostMessageMap ; other.m_pFNGetPostMessageMap = NULL ;
	
	// allow cascading =
	// a = b = c ; etc
	return *this ;
}

void CDLLWrapper::InitialiseDLL(CWinApp *pApp)
{
	if (m_pFNInitialiseDLL != NULL)
		{
		//ASSERT(FALSE == IsBadCodePtr(m_pFNInitialiseDLL)) ;
		m_pFNInitialiseDLL(pApp) ;
		}
}

void CDLLWrapper::ReleaseDLL()
{
	if (m_pFNReleaseDLL)
		{
		//ASSERT(FALSE == IsBadCodePtr(m_pFNReleaseDLL)) ;
		m_pFNReleaseDLL() ;
		}
}

int CDLLWrapper::GetDocTemplateCount()
{
	if (m_pFNGetDocTemplateCount)
		{
		//ASSERT(FALSE == IsBadCodePtr(m_pFNGetDocTemplateCount)) ;
		return m_pFNGetDocTemplateCount() ;
		}
	return 0 ;
}

CMyMultiDocTemplate* CDLLWrapper::GetDocTemplate(int index)
{
	if (m_pFNGetDocTemplate)
		{
		ASSERT(index >= 0) ;
		ASSERT(index < GetDocTemplateCount()) ;
		//ASSERT(FALSE == IsBadCodePtr(m_pFNGetDocTemplate)) ;
		return m_pFNGetDocTemplate(index) ;
		}
	return NULL ;
}

AFX_MSGMAP* CDLLWrapper::GetPreMessageMap(LPCTSTR pClassName)
{
	if (m_pFNGetPreMessageMap)
		{
		return m_pFNGetPreMessageMap(pClassName) ;
		}
	return NULL ;
}

AFX_MSGMAP* CDLLWrapper::GetPostMessageMap(LPCTSTR pClassName)
{
	if (m_pFNGetPostMessageMap)
		{
		return m_pFNGetPostMessageMap(pClassName) ;
		}
	return NULL ;
}



Roger Allen
Sonork 100.10016

I think I need a new quote, I am on the prowl, so look out for a soft cute furry looking animal, which is really a Hippo in disguise. Its probably me.
GeneralRe: DLL plugin help Pin
DarrollWalsh25-Sep-02 17:59
DarrollWalsh25-Sep-02 17:59 
GeneralIShellView, CView and OnSize (display bug) Pin
luc224-Sep-02 11:38
luc224-Sep-02 11:38 
GeneralRe: IShellView, CView and OnSize (display bug) Pin
Jawache24-Sep-02 13:16
Jawache24-Sep-02 13:16 
GeneralRe: IShellView, CView and OnSize (display bug) Pin
luc225-Sep-02 1:24
luc225-Sep-02 1:24 
GeneralInitializing listboxes Pin
RalfPeter24-Sep-02 11:01
RalfPeter24-Sep-02 11:01 
GeneralRe: Initializing listboxes Pin
Tomasz Sowinski24-Sep-02 11:04
Tomasz Sowinski24-Sep-02 11:04 
QuestionIs the parent window handle correct? Pin
ns24-Sep-02 10:21
ns24-Sep-02 10:21 
AnswerRe: Is the parent window handle correct? Pin
Tomasz Sowinski24-Sep-02 10:31
Tomasz Sowinski24-Sep-02 10:31 
GeneralRe: Is the parent window handle correct? Pin
ns24-Sep-02 10:43
ns24-Sep-02 10:43 
GeneralRe: Is the parent window handle correct? Pin
Tomasz Sowinski24-Sep-02 10:49
Tomasz Sowinski24-Sep-02 10:49 
Generalnope... Pin
ns24-Sep-02 11:06
ns24-Sep-02 11:06 
AnswerRe: Is the parent window handle correct? Pin
Chris Richardson24-Sep-02 11:17
Chris Richardson24-Sep-02 11:17 
GeneralAD Schema changes... Pin
Ray Cassick24-Sep-02 10:02
Ray Cassick24-Sep-02 10:02 
GeneralAuto scroll selected text to the top of a view Pin
Wade H.24-Sep-02 9:37
Wade H.24-Sep-02 9:37 
Question??? Number Formating ??? Pin
Nitron24-Sep-02 9:24
Nitron24-Sep-02 9:24 
AnswerRe: ??? Number Formating ??? Pin
Jawache24-Sep-02 9:28
Jawache24-Sep-02 9:28 
AnswerRe: ??? Number Formating ??? Pin
Tomasz Sowinski24-Sep-02 9:32
Tomasz Sowinski24-Sep-02 9:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.