Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Win32/Linux Dynamic Library Loader Class

0.00/5 (No votes)
12 Oct 2006 1  
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...

#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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here