From cee868801b55aa4302c91a46df8daf38388163e7 Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 4 Jul 2008 12:01:52 +0000 Subject: Adding project files. --- src/lua-plugin.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/lua-plugin.cc (limited to 'src/lua-plugin.cc') diff --git a/src/lua-plugin.cc b/src/lua-plugin.cc new file mode 100644 index 0000000..79968a6 --- /dev/null +++ b/src/lua-plugin.cc @@ -0,0 +1,43 @@ +#include + +typedef void(*init_ptr_t)(Lua *); + +#ifdef _WIN32 +#include + +void LuaLoadPlugin(const String & _fname, Lua * L) throw (GeneralException) { + HMODULE handle; + String fname = _fname + ".dll"; + + if (!(handle = LoadLibraryEx(fname.to_charp(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH)) && + !(handle = LoadLibraryEx(fname.to_charp(), NULL, NULL))) { + throw GeneralException("File not found or error loading shared object file: " + fname + "; Error #" + String((int) GetLastError())); + } + + init_ptr_t init_ptr = (init_ptr_t) GetProcAddress(handle, "init_plugin"); + + if (!init_ptr) { + throw GeneralException("No init pointer on plugin " + fname); + } + + init_ptr(L); +} +#else +#include + +void LuaLoadPlugin(const String & fname, Lua * L) throw (GeneralException) { + void * handle = dlopen(("./" + fname + ".so").to_charp(), RTLD_NOW | RTLD_GLOBAL); + + if (!handle) { + throw GeneralException("File not found or error loading shared object file: " + fname + "; " + dlerror()); + } + + init_ptr_t init_ptr = (init_ptr_t) dlsym(handle, "init_plugin"); + + if (!init_ptr) { + throw GeneralException("No init pointer on plugin " + fname); + } + + init_ptr(L); +} +#endif -- cgit v1.2.3