⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dllmanager.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
字号:
#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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -