2#include <UnCompute/Base/PlatformInclude.h>
3#include <UnCompute/Memory/Memory.h>
6# define UN_LoadLibrary(name) LoadLibraryA(name)
7# define UN_GetFunctionAddress(handle, name) reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name))
8# define UN_FreeLibrary(handle) FreeLibrary(static_cast<HMODULE>(handle))
10# define UN_LoadLibrary(name) dlopen(name, RTLD_LAZY)
11# define UN_GetFunctionAddress(handle, name) dlsym(handle, name)
12# define UN_FreeLibrary(handle) dlclose(handle)
20 void* m_NativeHandle =
nullptr;
22 void* GetFunctionImpl(
const char* functionName);
38 ResultCode
Init(std::string_view name);
52 inline ResultCode
GetFunction(
const char* functionName, FPtr* pResult)
54 void* proc = GetFunctionImpl(functionName);
58 *pResult =
reinterpret_cast<FPtr
>(proc);
59 return ResultCode::Success;
62 return ResultCode::Fail;
67 *ppLibrary = AllocateObject<DynamicLibrary>();
68 (*ppLibrary)->AddRef();
69 return ResultCode::Success;
75 std::string fullName(name);
76 fullName += UN_DLL_EXTENSION;
77 m_NativeHandle = UN_LoadLibrary(fullName.c_str());
79 UN_Error(m_NativeHandle,
"Library '{}{}' was not loaded due to an error", name, UN_DLL_EXTENSION);
80 return m_NativeHandle ? ResultCode::Success : ResultCode::Fail;
83 inline void* DynamicLibrary::GetFunctionImpl(
const char* functionName)
85 return UN_GetFunctionAddress(m_NativeHandle, functionName);
88 inline DynamicLibrary::~DynamicLibrary()
95 if (m_NativeHandle ==
nullptr)
100 UNLOG_Info(
"Unloading dynamic library at {}...", m_NativeHandle);
101 UN_FreeLibrary(m_NativeHandle);
102 m_NativeHandle =
nullptr;
A class for loading and unloading DLLs.
Definition: DynamicLibrary.h:19
void Unload()
Unload the library.
Definition: DynamicLibrary.h:93
ResultCode GetFunction(const char *functionName, FPtr *pResult)
Load a function entry point from the DLL.
Definition: DynamicLibrary.h:52
ResultCode Init(std::string_view name)
Load a library with specified name.
Definition: DynamicLibrary.h:73
Base class for dynamic reference counted objects.
Definition: Object.h:51