Click here to Skip to main content
Licence LGPL3
First Posted 2 Feb 2007
Views 19,355
Downloads 266
Bookmarked 27 times

Yet another DLL manager class

By | 2 Feb 2007 | Article
An article on C++ class useful during utilization of an external DLL

Introduction

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.cpp and LibraryMgr.h files. Then you need to include LibraryMgr.h file before using.

Assuming that MyLib.dll exports int Test(void) function, the next source code presents common usage of the provided LibraryMgr_t class.

#include "LibraryMgr.h"

int _tmain(int argc, _TCHAR* argv[])
{
    LibraryMgr_t lm( _T("C:\\MyProg\\MyLib.dll") );

    typedef int (*Test_t)(void);
    Test_t TestFn;

    TestFn = (Test_t) lm.GetProcAddress( _T("Test") );

    if(TestFn)
    {
        TestFn();
    }

    return 0;
}

Note that the .dll library at the example above will be released automatically when lm variable leaves it lifetime scope. It will be done by LibraryMgr_t class's destructor.

Additional Possibilities

The provided 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 - bool FreeLibrary()
  • retrieving DLL's module handle - HMODULE GetModuleHandle()
  • 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 task programming.

License

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

About the Author

boris_oleinic



Ukraine Ukraine

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Feb 2007
Article Copyright 2007 by boris_oleinic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid