Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C++

Win32/Linux Dynamic Library Loader Class

Rate me:
Please Sign up or sign in to vote.
3.94/5 (15 votes)
12 Oct 2006CPOL2 min read 80.3K   795   42   14
A class to dynamically load DLLs and SO files and call functions from them
Sample Image - BCLoadLib.jpg

Introduction

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

Background

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...

C++
#include <iostream>
using namespace std;

#include "BCLoadLib.h"

typedef int (*func)(int);

int main()
{
    try{
        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:

C++
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.

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

Your functions must either be...

C++
extern "C" __declspec(dllexport)

or:

C++
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.

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

This actually loads the lib, windows supports just sending the name, but on Linux, we have to add ./name.so, 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:

C++
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.

Conclusion

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).

History

  • 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.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionif library does mysteriously fails on loading in linux Pin
coder_of_salvation30-Aug-11 11:20
coder_of_salvation30-Aug-11 11:20 
Questionbugfix for mingw Pin
coder_of_salvation19-Aug-11 4:14
coder_of_salvation19-Aug-11 4:14 
GeneralFailure to Load dll Pin
stephen.cossgrove17-Oct-06 16:40
stephen.cossgrove17-Oct-06 16:40 
AnswerRe: Failure to Load dll Pin
Bigcheesegs18-Oct-06 7:27
Bigcheesegs18-Oct-06 7:27 
GeneralBCLoadLib* dll = new BCLoadLib("lib"); Pin
Sceptic Mole12-Oct-06 7:51
Sceptic Mole12-Oct-06 7:51 
GeneralFailure to Load Function Pin
cossy7412-Oct-06 4:11
cossy7412-Oct-06 4:11 
AnswerRe: Failure to Load Function Pin
Bigcheesegs12-Oct-06 4:36
Bigcheesegs12-Oct-06 4:36 
QuestionHow can I hide the export information of my DLL Pin
Simon.W18-Apr-06 5:19
Simon.W18-Apr-06 5:19 
AnswerRe: How can I hide the export information of my DLL Pin
Bigcheesegs18-Apr-06 8:43
Bigcheesegs18-Apr-06 8:43 
AnswerRe: How can I hide the export information of my DLL Pin
Hokei1-May-06 9:02
Hokei1-May-06 9:02 
Generalmemory leak Pin
Zalosny17-Apr-06 12:19
Zalosny17-Apr-06 12:19 
GeneralRe: memory leak Pin
Bigcheesegs17-Apr-06 17:11
Bigcheesegs17-Apr-06 17:11 
GeneralRe: memory leak Pin
Zalosny18-Apr-06 1:40
Zalosny18-Apr-06 1:40 
GeneralRe: memory leak Pin
Bigcheesegs18-Apr-06 8:51
Bigcheesegs18-Apr-06 8: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.