aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/bfc/loadlib.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Src/Wasabi/bfc/loadlib.cpp')
-rw-r--r--Src/Wasabi/bfc/loadlib.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/Src/Wasabi/bfc/loadlib.cpp b/Src/Wasabi/bfc/loadlib.cpp
new file mode 100644
index 00000000..8682cd29
--- /dev/null
+++ b/Src/Wasabi/bfc/loadlib.cpp
@@ -0,0 +1,85 @@
+#include "precomp_wasabi_bfc.h"
+
+#include "loadlib.h"
+
+#if !defined(WIN32) && !defined(LINUX)
+#error port me
+#endif
+
+Library::Library(const wchar_t *filename) : NamedW(filename)
+{
+ lib = NULL;
+}
+
+Library::~Library()
+{
+ unload();
+}
+
+int Library::load(const wchar_t *newname)
+{
+ if (lib != NULL && newname == NULL)
+ return 1;
+ unload();
+ if (newname != NULL)
+ setName(newname);
+
+ const wchar_t *n = getName();
+ ASSERT(n != NULL);
+#ifdef WIN32
+ __try {
+ lib = LoadLibraryW(n);
+ } __except(EXCEPTION_EXECUTE_HANDLER)
+ {
+ // stupid DLL
+ lib = NULL;
+ OutputDebugString(L"exception while loading dll");
+ OutputDebugStringW(newname);
+ OutputDebugString(L"\n");
+ }
+#elif defined(LINUX)
+ // Not using string to try to not use common/wasabi in Studio.exe
+ char *conv = _strdup( getName() );
+ int len = strlen( conv );
+ if ( ! strcasecmp( conv + len - 4, ".dll" ) )
+ {
+ strcpy( conv + len - 4, ".so" );
+ }
+
+ lib = dlopen(conv, RTLD_LAZY);
+
+ free( conv );
+#else
+#error port me!
+#endif
+ if (lib == NULL) return 0;
+ return 1;
+}
+
+void Library::unload()
+{
+ if (lib != NULL)
+ {
+#ifdef WIN32
+ FreeLibrary(lib);
+#elif defined(LINUX)
+ dlclose(lib);
+#else
+#error port me!
+#endif
+
+ }
+ lib = NULL;
+}
+
+void *Library::getProcAddress(const char *procname)
+{
+ ASSERT(procname != NULL);
+#if defined(WIN32)
+ return GetProcAddress(lib, procname);
+#elif defined(LINUX)
+ return dlsym(lib, procname);
+#else
+#error port me!
+#endif
+}