|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-18 14:02:50
|
Module: performous
Branch: master
Commit: 358bf49e58d6fafdd41db067663c9668535d3fb4
Author: Lasse Karkkainen <tro...@tr...>
Date: Wed Nov 18 16:02:06 2009 +0200
Add dll symbol loading to libplugin++
---
libs/plugin++/include/plugin++/dll.hpp | 25 +++++++++++++++++++++++--
libs/plugin++/src/dll.cc | 12 ++++++++++--
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/libs/plugin++/include/plugin++/dll.hpp b/libs/plugin++/include/plugin++/dll.hpp
index ed4a7b6..39aa091 100644
--- a/libs/plugin++/include/plugin++/dll.hpp
+++ b/libs/plugin++/include/plugin++/dll.hpp
@@ -1,16 +1,37 @@
+#pragma once
#ifndef DLL_HPP_INCLUDED
#define DLL_HPP_INCLUDED
#include "dllhelper.hpp"
+#include <stdexcept>
#include <string>
namespace plugin {
- /** @short RAII wrapper to load and unload dynamic library **/
+
+ /// \brief Exception class for signaling runtime errors from class dll
+ class dll_error: public std::runtime_error {
+ public:
+ dll_error(std::string const& msg): runtime_error(msg) {}
+ };
+
+ /// \brief Dynamic library loader
class DLL_PUBLIC dll {
void* lib;
- public:
+ public:
+ /// Open a dynamic library
+ /// \throws dll_error if the library cannot be loaded
dll(std::string const& filename);
~dll();
+ /// Get a symbol from DLL (symptr can be a data or a function pointer)
+ /// \throws dll_error if no symbol is found
+ template <typename SymPtr> void sym(std::string const& name, SymPtr& symptr) {
+ union { void* ptr; SymPtr symptr; } conv; // Data/function conversion without warnings
+ conv.ptr = sym(name.c_str());
+ if (!conv.ptr) throw dll_error("Symbol " + name + " not found");
+ symptr = conv.symptr;
+ }
+ /// Get a symbol from DLL (low level C style version)
+ void* sym(char const* name);
};
}
diff --git a/libs/plugin++/src/dll.cc b/libs/plugin++/src/dll.cc
index f684654..73662e6 100644
--- a/libs/plugin++/src/dll.cc
+++ b/libs/plugin++/src/dll.cc
@@ -11,19 +11,27 @@ using namespace plugin;
#include <windows.h>
dll::dll(std::string const& filename): lib(LoadLibrary(filename.c_str())) {
- if (!lib) throw std::runtime_error("Unable to open " + filename);
+ if (!lib) throw dll_error("Unable to open " + filename);
}
dll::~dll() { FreeLibrary(static_cast<HINSTANCE>(lib)); }
+void* dll::sym(char const* sym) {
+ return GetProcAddress(static_cast<HINSTANCE>(lib), sym);
+}
+
#else
#include <dlfcn.h>
dll::dll(std::string const& filename): lib(dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL)) {
- if (!lib) throw std::runtime_error(dlerror());
+ if (!lib) throw dll_error(dlerror());
}
dll::~dll() { dlclose(lib); }
+void* dll::sym(char const* sym) {
+ return dlsym(lib, sym);
+}
+
#endif
|