diff options
Diffstat (limited to 'zen/dll.h')
-rw-r--r-- | zen/dll.h | 28 |
1 files changed, 16 insertions, 12 deletions
@@ -8,10 +8,15 @@ #define DLLLOADER_H_INCLUDED #include <memory> +#ifdef FFS_WIN #include <string> #include "scope_guard.h" #include "win.h" //includes "windows.h" +#elif defined FFS_LINUX || defined FFS_MAC +#include <dlfcn.h> +#endif + namespace zen { /* @@ -35,10 +40,15 @@ class DllFun public: DllFun() : fun(nullptr) {} +#ifdef FFS_WIN DllFun(const wchar_t* libraryName, const char* functionName) : hLibRef(::LoadLibrary(libraryName), ::FreeLibrary), fun(hLibRef ? reinterpret_cast<Func>(::GetProcAddress(static_cast<HMODULE>(hLibRef.get()), functionName)) : nullptr) {} - +#elif defined FFS_LINUX || defined FFS_MAC + DllFun(const char* libraryName, const char* functionName) : + hLibRef(::dlopen(libraryName, RTLD_LAZY), ::dlclose), + fun(hLibRef ? reinterpret_cast<Func>(::dlsym(hLibRef.get(), functionName)) : nullptr) {} +#endif operator Func() const { return fun; } private: @@ -46,6 +56,8 @@ private: Func fun; }; + +#ifdef FFS_WIN //if the dll is already part of the process space, e.g. "kernel32.dll" or "shell32.dll", we can use a faster variant: //NOTE: since the lifetime of the referenced library is *not* controlled, this is safe to use only for permanently loaded libraries like these! template <class Func> @@ -66,7 +78,6 @@ private: Func fun; }; - /* extract binary resources from .exe/.dll: @@ -77,12 +88,7 @@ extract binary resources from .exe/.dll: MY_BINARY_RESOURCE RCDATA "filename.dat" */ std::string getResourceStream(const std::wstring& libraryName, size_t resourceId); - - - - - - +#endif @@ -94,6 +100,7 @@ std::string getResourceStream(const std::wstring& libraryName, size_t resourceId //--------------- implementation--------------------------------------------------- +#ifdef FFS_WIN inline std::string getResourceStream(const wchar_t* libraryName, size_t resourceId) { @@ -102,16 +109,13 @@ std::string getResourceStream(const wchar_t* libraryName, size_t resourceId) ZEN_ON_SCOPE_EXIT(::FreeLibrary(module)); if (HRSRC res = ::FindResource(module, MAKEINTRESOURCE(resourceId), RT_RCDATA)) - { if (HGLOBAL resHandle = ::LoadResource(module, res)) - { if (const char* stream = static_cast<const char*>(::LockResource(resHandle))) return std::string(stream, static_cast<size_t>(::SizeofResource(module, res))); //size is 0 on error - } - } } return std::string(); } +#endif } #endif // DLLLOADER_H_INCLUDED |