17 #ifndef DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
18 #define DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
20 #include "../DistrhoUtils.hpp"
22 #ifdef DISTRHO_OS_WINDOWS
26 # include <winsock2.h>
28 typedef HMODULE lib_t;
44 lib_t lib_open(
const char*
const filename) noexcept
46 DISTRHO_SAFE_ASSERT_RETURN(filename !=
nullptr && filename[0] !=
'\0',
nullptr);
49 #ifdef DISTRHO_OS_WINDOWS
50 return ::LoadLibraryA(filename);
52 return ::dlopen(filename, RTLD_NOW|RTLD_LOCAL);
54 } DISTRHO_SAFE_EXCEPTION_RETURN(
"lib_open",
nullptr);
62 bool lib_close(
const lib_t lib) noexcept
64 DISTRHO_SAFE_ASSERT_RETURN(lib !=
nullptr,
false);
67 #ifdef DISTRHO_OS_WINDOWS
68 return ::FreeLibrary(lib);
70 return (::dlclose(lib) == 0);
72 } DISTRHO_SAFE_EXCEPTION_RETURN(
"lib_close",
false);
79 template<
typename Func>
81 Func lib_symbol(
const lib_t lib,
const char*
const symbol) noexcept
83 DISTRHO_SAFE_ASSERT_RETURN(lib !=
nullptr,
nullptr);
84 DISTRHO_SAFE_ASSERT_RETURN(symbol !=
nullptr && symbol[0] !=
'\0',
nullptr);
87 #ifdef DISTRHO_OS_WINDOWS
88 # if defined(__GNUC__) && (__GNUC__ >= 9)
89 # pragma GCC diagnostic push
90 # pragma GCC diagnostic ignored "-Wcast-function-type"
92 return (Func)::GetProcAddress(lib, symbol);
93 # if defined(__GNUC__) && (__GNUC__ >= 9)
94 # pragma GCC diagnostic pop
97 return (Func)(uintptr_t)::dlsym(lib, symbol);
99 } DISTRHO_SAFE_EXCEPTION_RETURN(
"lib_symbol",
nullptr);
107 const char* lib_error(
const char*
const filename) noexcept
109 DISTRHO_SAFE_ASSERT_RETURN(filename !=
nullptr && filename[0] !=
'\0',
nullptr);
111 #ifdef DISTRHO_OS_WINDOWS
112 static char libError[2048+1];
113 std::memset(libError, 0,
sizeof(libError));
116 const DWORD winErrorCode = ::GetLastError();
117 const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
118 LPVOID winErrorString;
120 ::FormatMessage(winErrorFlags,
nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0,
nullptr);
122 std::snprintf(libError, 2048,
"%s: error code %li: %s", filename, winErrorCode, (
const char*)winErrorString);
123 ::LocalFree(winErrorString);
124 } DISTRHO_SAFE_EXCEPTION(
"lib_error");
126 return (libError[0] !=
'\0') ? libError :
nullptr;
#define END_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:949
#define START_NAMESPACE_DISTRHO
Definition: DistrhoInfo.hpp:943