DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
  17. #define DISTRHO_LIBRARY_UTILS_HPP_INCLUDED
  18. #include "../DistrhoUtils.hpp"
  19. #ifdef DISTRHO_OS_WINDOWS
  20. # include <windows.h>
  21. typedef HMODULE lib_t;
  22. #else
  23. # include <dlfcn.h>
  24. typedef void* lib_t;
  25. #endif
  26. // -----------------------------------------------------------------------
  27. // library related calls
  28. /*
  29. * Open 'filename' library (must not be null).
  30. * May return null, in which case "lib_error" has the error.
  31. */
  32. static inline
  33. lib_t lib_open(const char* const filename) noexcept
  34. {
  35. DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  36. try {
  37. #ifdef DISTRHO_OS_WINDOWS
  38. return ::LoadLibraryA(filename);
  39. #else
  40. return ::dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  41. #endif
  42. } DISTRHO_SAFE_EXCEPTION_RETURN("lib_open", nullptr);
  43. }
  44. /*
  45. * Close a previously opened library (must not be null).
  46. * If false is returned, "lib_error" has the error.
  47. */
  48. static inline
  49. bool lib_close(const lib_t lib) noexcept
  50. {
  51. DISTRHO_SAFE_ASSERT_RETURN(lib != nullptr, false);
  52. try {
  53. #ifdef DISTRHO_OS_WINDOWS
  54. return ::FreeLibrary(lib);
  55. #else
  56. return (::dlclose(lib) == 0);
  57. #endif
  58. } DISTRHO_SAFE_EXCEPTION_RETURN("lib_close", false);
  59. }
  60. /*
  61. * Get a library symbol (must not be null).
  62. * Returns null if the symbol is not found.
  63. */
  64. template<typename Func>
  65. static inline
  66. Func lib_symbol(const lib_t lib, const char* const symbol) noexcept
  67. {
  68. DISTRHO_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  69. DISTRHO_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);
  70. try {
  71. #ifdef DISTRHO_OS_WINDOWS
  72. return (Func)::GetProcAddress(lib, symbol);
  73. #else
  74. return (Func)(uintptr_t)::dlsym(lib, symbol);
  75. #endif
  76. } DISTRHO_SAFE_EXCEPTION_RETURN("lib_symbol", nullptr);
  77. }
  78. /*
  79. * Return the last operation error ('filename' must not be null).
  80. * May return null.
  81. */
  82. static inline
  83. const char* lib_error(const char* const filename) noexcept
  84. {
  85. DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  86. #ifdef DISTRHO_OS_WINDOWS
  87. static char libError[2048+1];
  88. std::memset(libError, 0, sizeof(libError));
  89. try {
  90. const DWORD winErrorCode = ::GetLastError();
  91. const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
  92. LPVOID winErrorString;
  93. ::FormatMessage(winErrorFlags, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  94. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  95. ::LocalFree(winErrorString);
  96. } DISTRHO_SAFE_EXCEPTION("lib_error");
  97. return (libError[0] != '\0') ? libError : nullptr;
  98. #else
  99. return ::dlerror();
  100. #endif
  101. }
  102. // -----------------------------------------------------------------------
  103. #endif // DISTRHO_LIBRARY_UTILS_HPP_INCLUDED