Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C++

Yet another DLL manager class

Rate me:
Please Sign up or sign in to vote.
3.20/5 (7 votes)
2 Feb 2007LGPL31 min read 31.6K   467   29  
An article on C++ class useful during utilization of an external DLL
// Yet another DLL manager class
// Copyright (c) 2007 Boris Oleinic <obn@inbox.ru>
// You may do whatever you like with this file, I just don't care.

#include "LibraryMgr.h"

LibraryMgr_t::LibraryMgr_t(void)
: m_hModule(0), m_bAutoFree(true)
{
}

LibraryMgr_t::LibraryMgr_t(const TCHAR* ptcFileName)
: m_hModule(0), m_bAutoFree(true)
{
	LoadLibrary( ptcFileName );
}

LibraryMgr_t::~LibraryMgr_t(void)
{
	if ( m_hModule && m_bAutoFree )
	{
		FreeLibrary();
	}
}

bool LibraryMgr_t::LoadLibrary(
							 const TCHAR* ptcFileName
							 )
{
	if (! ptcFileName)
		return false;

	if ( m_hModule )
	{
		FreeLibrary();
	}

	m_sFileNAme.assign( ptcFileName );
	m_hModule = ::LoadLibrary( m_sFileNAme.c_str() );

	return m_hModule != NULL;
}

bool LibraryMgr_t::FreeLibrary()
{
	if( ! m_hModule )
		return false;

	::FreeLibrary( m_hModule );
	m_hModule = NULL;
	m_sFileNAme.clear();

	return true;
}

FARPROC LibraryMgr_t::GetProcAddress(
		const TCHAR* ptcProcName
		)
{
	if( ! m_hModule )
		return NULL;

	return ::GetProcAddress( m_hModule, ptcProcName );
}

HMODULE LibraryMgr_t::GetModuleHandle()
{
	if( ! m_hModule )
		return NULL;

	return ::GetModuleHandle( m_sFileNAme.c_str() );
}

bool LibraryMgr_t::GetModuleFileName(std::basic_string<TCHAR>& sName)
{
	if( ! m_hModule )
		return false;

	sName.assign( m_sFileNAme );
	return true;
}

bool LibraryMgr_t::FreeLibraryAndExitThread( DWORD dwExitCode )
{
	if( ! m_hModule )
		return false;

	::FreeLibraryAndExitThread( m_hModule, dwExitCode );

	m_hModule = 0;

	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.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Systems Engineer N/A
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions