Click here to Skip to main content
13,356,787 members (64,503 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 17 Apr 2006

Win32/Linux Dynamic Library Loader Class

, 12 Oct 2006
Rate this:
Please Sign up or sign in to vote.
A class to dynamically load DLLs and SO files and call functions from them
Sample Image - BCLoadLib.jpg


This class allows you to load a Library (a .dll or .so) and get function addresses from the lib on Windows or Linux.


I have been looking around for simple class like this for some of my cross platform programs and couldn't find one, so I made one.

Using the Code

The first thing you should know (one thing that held me up for a bit) is that on Linux you must add -ldl to the compile command so that it can link dlopen, dlsym and dlclose.
The best way to explain the code would be an example...

#include <iostream>
using namespace std;

#include "BCLoadLib.h"

typedef int (*func)(int);

int main()
        BCLoadLib* dll = new BCLoadLib("lib");
        cout << "Loaded.\n";
        func f = (func)dll->getFunc("bob");
        cout << f(7) << "\n";
        delete dll;
        cout << "Unloaded.\n";
    }catch(char* e){
        cout << e << "\n";
    return 0;

The first line of interest is:

typedef int (*func)(int);

This is a function pointer, you must define these yourself for every function you want to use from your DLL.
The code for this function in the lib is, if this doesn't make sense, you should look up function pointers.

extern "C" __declspec(dllexport) int bob(int i){
        return i;

Your functions must either be...

extern "C" __declspec(dllexport)


extern "C"

and have a .def file. You cannot load a C++ function with this lib.

This class uses throw/catch for error handling. If it can't open the library (file not there or error in file), or if it can't find the function you requested with getFunc, it will throw an error.

The next important line is where we create the class.

BCLoadLib* dll = new BCLoadLib("lib");

This actually loads the lib, windows supports just sending the name, but on Linux, we have to add ./, this way your program doesn't have to know if we are on Windows or Linux.

The next two important lines get a function address and call that function:

func f = (func)dll->getFunc("bob");
cout << f(7) << "\n";

Remember that func is our typedef for the function, so we create a new func f and set the address to the address from the DLL, typecasting the void* to our function type.


Well, I hope that helps some of you out there. Please tell me anything that might help or make the code better (or any spelling mistakes :P).


  • October 12, 2006: Fixed some spelling mistakes. This code is rather old, I would recommend looking at the class (it's small) and integrating it into your own code.
  • April 17, 2006: Fixed memory leak and possible memory access error in the Linux loading code.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionif library does mysteriously fails on loading in linux Pin
coder_of_salvation30-Aug-11 12:20
membercoder_of_salvation30-Aug-11 12:20 
Questionbugfix for mingw Pin
_sqz_19-Aug-11 5:14
member_sqz_19-Aug-11 5:14 
GeneralFailure to Load dll Pin
stephen.cossgrove17-Oct-06 17:40
memberstephen.cossgrove17-Oct-06 17:40 
AnswerRe: Failure to Load dll Pin
Bigcheesegs18-Oct-06 8:27
memberBigcheesegs18-Oct-06 8:27 
GeneralBCLoadLib* dll = new BCLoadLib("lib"); Pin
Sceptic Mole12-Oct-06 8:51
memberSceptic Mole12-Oct-06 8:51 
GeneralFailure to Load Function Pin
cossy7412-Oct-06 5:11
membercossy7412-Oct-06 5:11 
AnswerRe: Failure to Load Function Pin
Bigcheesegs12-Oct-06 5:36
memberBigcheesegs12-Oct-06 5:36 
QuestionHow can I hide the export information of my DLL Pin
Simon.W18-Apr-06 6:19
memberSimon.W18-Apr-06 6:19 
AnswerRe: How can I hide the export information of my DLL Pin
Bigcheesegs18-Apr-06 9:43
memberBigcheesegs18-Apr-06 9:43 
AnswerRe: How can I hide the export information of my DLL Pin
Hokei1-May-06 10:02
memberHokei1-May-06 10:02 
You can't hide them 100%, but you can export by ordinal rather than name. Then, someone inspecting your DLL only sees "1," "2," "3," "4," etc. as the exports. This method requires using a .DEF file in your DLL project.

Exporting Functions from a DLL by Ordinal Rather Than by Name:
Generalmemory leak Pin
Zalosny17-Apr-06 13:19
memberZalosny17-Apr-06 13:19 
GeneralRe: memory leak Pin
Bigcheesegs17-Apr-06 18:11
memberBigcheesegs17-Apr-06 18:11 
GeneralRe: memory leak Pin
Zalosny18-Apr-06 2:40
memberZalosny18-Apr-06 2:40 
GeneralRe: memory leak Pin
Bigcheesegs18-Apr-06 9:51
memberBigcheesegs18-Apr-06 9:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 12 Oct 2006
Article Copyright 2006 by Bigcheesegs
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid