Back to Amir Israeli Homepage

[CLoadLibrary : Loads Libraries (DLL's) ]

This article was contributed by Amir Israeli Amir Israeli.
Amir Israeli Web Site

Environment: Win 95/98 VC6 SP4

Loading libraries in windows is very simple : all you need is call ::LoadLibrary(path to library) and you get the handle to the library . But what to do with it ?
LoadLibrary returns HINSTANCE : a handle to the library. Internally : windows maintains a reference count to the loaded libraries (as if the were COM objects) loads them only if not alredy loaded and "releases" (unloads) only if reference count drops to 0.

The primary use to this class if for extracting some functions , Meanwhile I added functionality for extracting by Name (which is relatively inefficient method to use when extarcting functions from DLLs) . (In the future I'll add functions by their index ,(order number))

This class is supposed to be put on stack. it includes 3 types of constructors : you may use the default constructor or else supply a dll file name (+the extension) you dont need to supply the full path if this DLL is under the Windows directory or System directory . The function will find internally the full path.

	CLoadLibrary(LPCTSTR lpszFileName);
	CLoadLibrary(LPCTSTR lpszFileName,BOOL bSystemDirectory,BOOL bWindowsDirectory);
Loading of library is done in the function : Load(...) as you see it gets the relative/full path and 2 booleans (to indicate system/windows directory) . It return TRUE for success , FALSE for failure. Currently the function (class allows loading only DLLs : it does an extra check (checks for .dll extension in file path). The check is done within the Load function.
	BOOL Load(LPCTSTR lpszFileName,BOOL bSystemDirectory=FALSE,BOOL bWindowsDirectory=FALSE);
There are 2 information functions : you may get the instance handle (be warned dont mess with it) or the full path name of library.
	HINSTANCE GetInstance() { return hInstance; }
	CString GetLibrary() { return m_strLibrary; }
Currently this class serves as a base class to other classes I wrote : already available CLibraryFuncExtractor classes, or gives services to other classes : A very common use for such class is determining library version (using DLLVERSIONINFO). I didn't add such functionality : this is rather simple (yet tricky) and mainly because there are free implementations of this feature in the internet.
Protected functions : Free() : frees the library : and done automaticly on destruction and FindProc(...) : to extract a function from this DLL is used internaly by this class or its' derived classes.


Download source - 2 Kb


Date Posted: December 20, 2000

Home  |  Links  |  About

Make your own free website on