moduleLoaderLDL.cpp
Go to the documentation of this file.
00001 #include "moduleLoaderLDL.h"
00002 #include <iostream>
00003 #include <dlfcn.h>
00004 #include <stdio.h>
00005 
00006 
00007 ModuleLoaderLDL::ModuleLoaderLDL()
00008 {
00009 
00010 }
00011 
00012 ModuleLoaderLDL::~ModuleLoaderLDL()
00013 {
00014    for(map<string, void*>::iterator it = _openLibs.begin(); it != _openLibs.end(); it++) {
00015       dlclose(it->second);
00016    }
00017 }
00018 
00019 void* ModuleLoaderLDL::getFunction(string fnString)
00020 {
00021    string libName = extractLibName(fnString);
00022    if(libName.empty())
00023       return NULL;
00024    void* libHandle = NULL;
00025    if(_openLibs.find(libName) != _openLibs.end()) {
00026       libHandle = _openLibs[libName];
00027    } else { // need to open the lib
00028       libHandle = dlopen(libName.c_str(), RTLD_NOW);
00029       if(libHandle == NULL) {
00030          fprintf(stderr, "Opening library %s failed.\n", libName.c_str());
00031          fputs(dlerror(), stderr);
00032          fprintf(stderr, "\n");
00033          return NULL;
00034       }
00035       _openLibs[libName] = libHandle;
00036    }
00037  
00038    string fnName = extractFunctionName(fnString);
00039    if(fnName.empty())
00040       return NULL;
00041 
00042 
00043    void* fn = dlsym(libHandle, fnName.c_str());
00044 
00045    char *error;
00046    if((error = dlerror()) != NULL)  {
00047       fputs(error, stderr);
00048       return NULL;
00049    }
00050 
00051    return fn;
00052 }
00053 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Tue Jan 22 2013 12:25:03