2010-08-09 13:28:56 +00:00
|
|
|
#ifndef NALL_DL_HPP
|
|
|
|
#define NALL_DL_HPP
|
|
|
|
|
|
|
|
//dynamic linking support
|
|
|
|
|
2011-09-29 12:08:22 +00:00
|
|
|
#include <nall/intrinsics.hpp>
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <nall/stdint.hpp>
|
|
|
|
#include <nall/string.hpp>
|
|
|
|
#include <nall/utility.hpp>
|
|
|
|
|
2013-11-28 10:29:01 +00:00
|
|
|
#if defined(PLATFORM_X) || defined(PLATFORM_MACOSX)
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <dlfcn.h>
|
2011-09-29 12:08:22 +00:00
|
|
|
#elif defined(PLATFORM_WINDOWS)
|
2010-08-09 13:28:56 +00:00
|
|
|
#include <windows.h>
|
2011-08-08 12:01:09 +00:00
|
|
|
#include <nall/windows/utf8.hpp>
|
2010-08-09 13:28:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace nall {
|
2013-05-02 11:25:45 +00:00
|
|
|
|
|
|
|
struct library {
|
|
|
|
explicit operator bool() const { return open(); }
|
|
|
|
bool open() const { return handle; }
|
|
|
|
bool open(const string&, const string& = "");
|
|
|
|
bool open_absolute(const string&);
|
|
|
|
void* sym(const string&);
|
|
|
|
void close();
|
|
|
|
|
|
|
|
library() = default;
|
|
|
|
~library() { close(); }
|
|
|
|
|
|
|
|
library& operator=(const library&) = delete;
|
|
|
|
library(const library&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uintptr_t handle = 0;
|
2010-08-09 13:28:56 +00:00
|
|
|
};
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
#if defined(PLATFORM_X)
|
|
|
|
inline bool library::open(const string& name, const string& path) {
|
|
|
|
if(handle) close();
|
2013-12-03 10:01:59 +00:00
|
|
|
handle = (uintptr_t)dlopen(string(path, !path.empty() && !path.endsWith("/") ? "/" : "", "lib", name, ".so"), RTLD_LAZY);
|
2013-05-02 11:25:45 +00:00
|
|
|
if(!handle) handle = (uintptr_t)dlopen(string("/usr/local/lib/lib", name, ".so"), RTLD_LAZY);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool library::open_absolute(const string& name) {
|
|
|
|
if(handle) close();
|
|
|
|
handle = (uintptr_t)dlopen(name, RTLD_LAZY);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* library::sym(const string& name) {
|
|
|
|
if(!handle) return nullptr;
|
|
|
|
return dlsym((void*)handle, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void library::close() {
|
|
|
|
if(!handle) return;
|
|
|
|
dlclose((void*)handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
2013-11-28 10:29:01 +00:00
|
|
|
#elif defined(PLATFORM_MACOSX)
|
2013-05-02 11:25:45 +00:00
|
|
|
inline bool library::open(const string& name, const string& path) {
|
|
|
|
if(handle) close();
|
2013-12-03 10:01:59 +00:00
|
|
|
handle = (uintptr_t)dlopen(string(path, !path.empty() && !path.endsWith("/") ? "/" : "", "lib", name, ".dylib"), RTLD_LAZY);
|
2013-05-02 11:25:45 +00:00
|
|
|
if(!handle) handle = (uintptr_t)dlopen(string("/usr/local/lib/lib", name, ".dylib"), RTLD_LAZY);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool library::open_absolute(const string& name) {
|
|
|
|
if(handle) close();
|
|
|
|
handle = (uintptr_t)dlopen(name, RTLD_LAZY);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* library::sym(const string& name) {
|
|
|
|
if(!handle) return nullptr;
|
|
|
|
return dlsym((void*)handle, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void library::close() {
|
|
|
|
if(!handle) return;
|
|
|
|
dlclose((void*)handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
#elif defined(PLATFORM_WINDOWS)
|
|
|
|
inline bool library::open(const string& name, const string& path) {
|
|
|
|
if(handle) close();
|
2013-12-07 09:12:37 +00:00
|
|
|
string filepath(path, !path.empty() && !path.endsWith("/") && !path.endsWith("\\") ? "/" : "", name, ".dll");
|
2013-05-02 11:25:45 +00:00
|
|
|
handle = (uintptr_t)LoadLibraryW(utf16_t(filepath));
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool library::open_absolute(const string& name) {
|
|
|
|
if(handle) close();
|
|
|
|
handle = (uintptr_t)LoadLibraryW(utf16_t(name));
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void* library::sym(const string& name) {
|
|
|
|
if(!handle) return nullptr;
|
|
|
|
return (void*)GetProcAddress((HMODULE)handle, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void library::close() {
|
|
|
|
if(!handle) return;
|
|
|
|
FreeLibrary((HMODULE)handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
inline bool library::open(const string&, const string&) { return false; }
|
|
|
|
inline bool library::open_absolute(const string&) { return false; }
|
|
|
|
inline void* library::sym(const string&) { return nullptr; }
|
|
|
|
inline void library::close() {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
#endif
|