Click here to Skip to main content
Click here to Skip to main content

Win32/Linux Dynamic Library Loader Class

By , 12 Oct 2006
Rate this:
Please Sign up or sign in to vote.
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...

#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:

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)

or:

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

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)

About the Author

Bigcheesegs
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questionif library does mysteriously fails on loading in linux Pinmembercoder_of_salvation30-Aug-11 11:20 
Questionbugfix for mingw Pinmember_sqz_19-Aug-11 4:14 
GeneralFailure to Load dll Pinmemberstephen.cossgrove17-Oct-06 16:40 
AnswerRe: Failure to Load dll PinmemberBigcheesegs18-Oct-06 7:27 
GeneralBCLoadLib* dll = new BCLoadLib("lib"); PinmemberSceptic Mole12-Oct-06 7:51 
The 'new' is not necesary, just do:
 
BCLoadLib dll("lib");
func f = (func)dll.getFunc("bob");
...

GeneralFailure to Load Function Pinmembercossy7412-Oct-06 4:11 
AnswerRe: Failure to Load Function PinmemberBigcheesegs12-Oct-06 4:36 
QuestionHow can I hide the export information of my DLL PinmemberSimon.W18-Apr-06 5:19 
AnswerRe: How can I hide the export information of my DLL PinmemberBigcheesegs18-Apr-06 8:43 
AnswerRe: How can I hide the export information of my DLL PinmemberHokei1-May-06 9:02 
Generalmemory leak PinmemberZalosny17-Apr-06 12:19 
GeneralRe: memory leak PinmemberBigcheesegs17-Apr-06 17:11 
GeneralRe: memory leak PinmemberZalosny18-Apr-06 1:40 
GeneralRe: memory leak PinmemberBigcheesegs18-Apr-06 8:51 

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

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

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