(root)/DLLoader.cpp @ 2 - Rev 2
Blame |
Last modification |
View Log
| RSS feed
/**
* Example Application for Dynamic Loading of SDL
* Copyright (C) 2010 Matthias -apoc- Hecker <apoc@sixserv.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DLLoader.h"
DLLoader::DLLoader()
{
libhandle = NULL;
load_proc_symbol_error = false;
}
// DLLoader::DLLoader(const DLLoader& orig) {}
DLLoader::~DLLoader() {}
bool DLLoader::Load(std::string library_filename)
{
std::cout << "-------------- Load Library --------------" << std::endl;
if (libhandle) {
std::cout << "library already loaded" << std::endl;
return true;
}
std::cout << "loading shared library: " << library_filename << std::endl;
#ifdef _WIN32
libhandle = LoadLibrary(library_filename.c_str());
if (libhandle == NULL) {
std::cout << "error loading shared library" << std::endl;
return false;
}
#else
libhandle = dlopen(library_filename.c_str(), RTLD_LAZY);
if (libhandle == NULL) {
std::cout << "error loading shared library: " << dlerror() << std::endl;
return false;
}
#endif
std::cout << "loading library function symbols" << std::endl;
LoadProcSymbols();
if (load_proc_symbol_error) {
std::cout << "error loading library function symbols" << std::endl;
return false;
}
return true;
}
void DLLoader::Unload()
{
std::cout << "------------- Unload Library -------------" << std::endl;
UnloadProcSymbols();
if (!libhandle) {
std::cout << "library handle not loaded" << std::endl;
return;
}
#ifdef _WIN32
FreeLibrary(libhandle);
#else
if (dlclose(libhandle) != 0) {
std::cout << "error closing library handle: " << dlerror() << std::endl;
return;
}
#endif
std::cout << "successfully closed library handle" << std::endl;
libhandle = NULL;
}
void DLLoader::LoadProcSymbols() {}
void DLLoader::UnloadProcSymbols() {}
void *DLLoader::LoadSymbol(const char *symbol_name)
{
void *symbol = NULL;
#ifdef _WIN32
symbol = (void *)GetProcAddress(libhandle, symbol_name);
#else
symbol = dlsym(libhandle, symbol_name);
#endif
if (symbol == NULL) {
load_proc_symbol_error = true;
std::cout << "error couldn't load library procedure symbol: " << symbol_name << std::endl;
}
return symbol;
}