Everyone sometimes has to use DLL. There are several common steps like :
- load the DLL
- get DLL exported function's addresses by names
- release the DLL
When I got tired with frequent reimplementation of the such simple steps, I wrote C++ class to reuse the code.
Using the code
To use the code you should add to your project
LibraryMgr.h files. Then you need to include
LibraryMgr.h file before using.
function, the next source code presents common usage of the provided
int _tmain(int argc, _TCHAR* argv)
LibraryMgr_t lm( _T("C:\\MyProg\\MyLib.dll") );
typedef int (*Test_t)(void);
TestFn = (Test_t) lm.GetProcAddress( _T("Test") );
Note that the .dll library at the example above will be released
lm variable leaves it lifetime scope.
It will be done by
LibraryMgr_t class's destructor.
LibraryMgr_t class allows the next
operations in addition to presented by the sample above:
- explicit loading of the DLL library -
bool LoadLibrary( const TCHAR* ptcFileName )
- explicit releasing of the DLL library -
- retrieving DLL's module handle -
- imposing a ban on auto releasing of the DLL by destructor -
void SetAutoFree(bool bFree)
- and even panic style function -
bool FreeLibraryAndExitThread( DWORD dwExitCode )
Points of Interest
This simple class allows us to improve code readability and clearance
then allows us to be fully focused on application level aspect of our