|
#include "StdAfx.h"
#include "dllmanager.h"
#include "MainFrm.h"
typedef UINT (*PINITFUNC)(CToolBar *m_wndToolbar);
typedef void (*PDESTROYFUNC)();
typedef UINT (*PPLUGINNAMEFUNC)(CString *szPlugInName);
DLLManager::DLLManager(void)
: m_szDLLFileName(_T(""))
, m_unToolBarID(0)
, m_szPlugInName(_T(""))
{
#ifdef DEBUG
TRACE("Called DLLManager::DLLManager() method\n");
#endif
m_pDLLhInstance = NULL;
m_pwndToolBar = NULL;
}
DLLManager::~DLLManager(void)
{
#ifdef DEBUG
TRACE("Called DLLManager::~DLLManager() method\n");
#endif
if (FreeDLL() == false)
{
#ifdef DEBUG
TRACE("ERROR : " + m_szDLLFileName + "\n\t" + "DLL could not be un-loaded");
#endif
}
m_pDLLhInstance = NULL;
m_pwndToolBar = NULL;
}
void DLLManager::SetToolBarPointer(CToolBar *pwndToolBar)
{
#ifdef DEBUG
TRACE("Called DLLManager::SetToolBarPointer() method\n");
#endif
m_pwndToolBar = pwndToolBar;
}
UINT DLLManager::GetToolBarButtonID()
{
#ifdef DEBUG
TRACE("Called DLLManager::GetToolBarPointer() method\n");
#endif
return m_unToolBarID;
}
// Used to load DLL given the filename
bool DLLManager::LoadDLL(CString szFileName)
{
#ifdef DEBUG
TRACE("Called DLLManager::LoadDLL() method\n");
#endif
// TODO: Add your command handler code here
m_szDLLFileName = szFileName;
HMODULE hModule = LoadLibrary(szFileName);
if (!hModule)
{
#ifdef DEBUG
TRACE("ERROR : " + m_szDLLFileName + "\n\t" + " DLL not found\n");
#endif
return false;
}
else
{
m_pDLLhInstance = hModule;
#ifdef DEBUG
TRACE(m_szDLLFileName + "\n\t" + " DLL loaded\n");
#endif
}
return true;
}
// Used to free previously loaded DLL
bool DLLManager::FreeDLL()
{
#ifdef DEBUG
TRACE("Called DLLManager::FreeDLL() method\n");
#endif
bool bFuncCalled = false;
// Release any loaded DLL
if (m_pDLLhInstance != NULL)
{
PDESTROYFUNC pDestroyFunc = (PDESTROYFUNC)GetProcAddress(m_pDLLhInstance, _T("DestroyPlugIn"));
if (pDestroyFunc != NULL)
{
pDestroyFunc();
#ifdef DEBUG
TRACE("Called DestroyPlugIn() method of DLL\n");
#endif
bFuncCalled = true;
}
else
{
#ifdef DEBUG
TRACE("Could not call DestroyPlugIn() method of DLL\n");
#endif
bFuncCalled = false;
}
// Release library
FreeLibrary(m_pDLLhInstance);
// Kill any function pointers
m_pDLLhInstance = NULL;
m_szDLLFileName = "" ;
#ifdef DEBUG
TRACE("Un-Loaded DLL\n");
#endif
}
return bFuncCalled;
}
// Used to initialize the DLL
bool DLLManager::InitDLL()
{
#ifdef DEBUG
TRACE("Called DLLManager::InitDLL() method\n");
#endif
bool bFuncCalled = false;
PINITFUNC pInitFunc = (PINITFUNC)GetProcAddress(m_pDLLhInstance, _T("InitPlugIn"));
if (pInitFunc != NULL)
{
m_unToolBarID = pInitFunc(m_pwndToolBar);
#ifdef DEBUG
TRACE("Called InitPlugIn() method of DLL\n");
#endif
bFuncCalled = true;
}
else
{
#ifdef DEBUG
TRACE("Could not call InitPlugIn() method of DLL\n");
#endif
// Remove Invalid/Corrupt DLL Instance from memory
FreeDLL();
bFuncCalled = false;
}
// Return Success or failure
return bFuncCalled;
}
// Used to set the filename for the DLL
void DLLManager::SetDLLFileName(CString szFileName)
{
#ifdef DEBUG
TRACE("Called DLLManager::SetDLLFileName method\n");
#endif
m_szDLLFileName = szFileName;
}
// Used to extract the Plug-In's name
bool DLLManager::GetDLLName(CString *szName)
{
if (LoadDLL(m_szDLLFileName) == true)
{
PPLUGINNAMEFUNC pFunc = (PPLUGINNAMEFUNC)GetProcAddress(m_pDLLhInstance, _T("GetPlugInName"));
if (pFunc == NULL)
{
#ifdef DEBUG
TRACE("ERROR : " + m_szDLLFileName + "\n\t" + " GetPlugInName() function not found\n");
#endif
// Release library
FreeLibrary(m_pDLLhInstance);
// Kill any pointers
m_pDLLhInstance = NULL;
m_szDLLFileName = "" ;
m_szPlugInName = _T("");
return false;
}
pFunc(szName);
}
else
{
m_pDLLhInstance = NULL;
m_szDLLFileName = "" ;
m_szPlugInName = _T("");
return false;
}
FreeLibrary(m_pDLLhInstance);
m_pDLLhInstance = NULL;
m_szPlugInName = _T("");
return true;
}
|
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.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Living in New Delhi (India). Did my Engg (Comp. Science) in 2000, and since then working on and off with VC++. It's my passion, and would really love to learn it to the very detail. Presently working as a software engg in a Telecom based firm. I've got around 3yrs experience in NMS (network management systems). Well thats all about me for now. Feel free to contact me...