windows-nt/Source/XPSP1/NT/admin/snapin/framewrk/dynamlnk.cpp

78 lines
1.7 KiB
C++
Raw Normal View History

2020-09-26 03:20:57 -05:00
// DynamLnk.cpp : base class for DLLs which are loaded only when needed
#include "stdafx.h"
#include "DynamLnk.h"
DynamicDLL::DynamicDLL(LPCTSTR ptchLibraryName, LPCSTR* apchFunctionNames)
: m_hLibrary( (HMODULE)-1 ),
m_apfFunctions( NULL ),
m_ptchLibraryName( ptchLibraryName ),
m_apchFunctionNames( apchFunctionNames ),
m_nNumFunctions( 0 )
{
ASSERT( !IsBadStringPtr(m_ptchLibraryName,MAX_PATH) );
ASSERT( NULL != apchFunctionNames );
for (LPCSTR pchFunctionName = *apchFunctionNames;
NULL != pchFunctionName;
pchFunctionName = *(++apchFunctionNames) )
{
m_nNumFunctions++;
ASSERT( !IsBadStringPtrA(pchFunctionName,MAX_PATH) );
}
}
DynamicDLL::~DynamicDLL()
{
if (NULL != m_apfFunctions)
{
delete m_apfFunctions;
m_apfFunctions = NULL;
}
if ((HMODULE)-1 != m_hLibrary && NULL != m_hLibrary)
{
VERIFY( ::FreeLibrary( m_hLibrary ) );
m_hLibrary = NULL;
}
}
BOOL DynamicDLL::LoadFunctionPointers()
{
if ((HMODULE)-1 != m_hLibrary)
return (NULL != m_hLibrary);
m_hLibrary = ::LoadLibrary( m_ptchLibraryName );
if (NULL == m_hLibrary)
{
// The library is not present
return FALSE;
}
// let this throw an exception
m_apfFunctions = new FARPROC[m_nNumFunctions];
for (INT i = 0; i < m_nNumFunctions; i++)
{
m_apfFunctions[i] = ::GetProcAddress( m_hLibrary, m_apchFunctionNames[i] );
if ( NULL == m_apfFunctions[i] )
{
// The library is present but does not have all of the entrypoints
VERIFY( ::FreeLibrary( m_hLibrary ) );
m_hLibrary = NULL;
return FALSE;
}
}
return TRUE;
}
FARPROC DynamicDLL::QueryFunctionPtr(INT i) const
{
if ( 0 > i || m_nNumFunctions <= i || NULL == m_apfFunctions || NULL == m_apfFunctions[i] )
{
ASSERT( FALSE );
return NULL;
}
return m_apfFunctions[i];
}