diff --git a/include/dynlib.h b/include/dynlib.h new file mode 100644 index 0000000..34b4a5a --- /dev/null +++ b/include/dynlib.h @@ -0,0 +1,21 @@ +#pragma once + +#ifdef _WIN32 + #include + typedef HMODULE dynlib_handle; + inline dynlib_handle dynlib_open(const char* path) { return LoadLibraryA(path); } + inline void* dynlib_sym(dynlib_handle h, const char* name) { return (void*)GetProcAddress(h, name); } + inline void dynlib_close(dynlib_handle h) { FreeLibrary(h); } + inline const char* dynlib_error() { + static char buf[256]; + FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, buf, sizeof(buf), NULL); + return buf; + } +#else + #include + typedef void* dynlib_handle; + inline dynlib_handle dynlib_open(const char* path) { return dlopen(path, RTLD_NOW); } + inline void* dynlib_sym(dynlib_handle h, const char* name) { return dlsym(h, name); } + inline void dynlib_close(dynlib_handle h) { dlclose(h); } + inline const char* dynlib_error() { return dlerror(); } +#endif