Audio plugin host https://kx.studio/carla
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.

CarlaLibUtils.hpp 3.3KB

10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_LIB_UTILS_HPP_INCLUDED
  18. #define CARLA_LIB_UTILS_HPP_INCLUDED
  19. #include "CarlaUtils.hpp"
  20. #ifndef CARLA_OS_WIN
  21. # include <dlfcn.h>
  22. #endif
  23. // -----------------------------------------------------------------------
  24. // library related calls
  25. /*
  26. * Open 'filename' library (must not be null).
  27. * May return null, in which case "lib_error" has the error.
  28. */
  29. static inline
  30. void* lib_open(const char* const filename) noexcept
  31. {
  32. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  33. try {
  34. #ifdef CARLA_OS_WIN
  35. return (void*)::LoadLibraryA(filename);
  36. #else
  37. return ::dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  38. #endif
  39. } CARLA_SAFE_EXCEPTION_RETURN("lib_open", nullptr);
  40. }
  41. /*
  42. * Close a previously opened library (must not be null).
  43. * If false is returned, "lib_error" has the error.
  44. */
  45. static inline
  46. bool lib_close(void* const lib) noexcept
  47. {
  48. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  49. try {
  50. #ifdef CARLA_OS_WIN
  51. return ::FreeLibrary((HMODULE)lib);
  52. #else
  53. return (::dlclose(lib) == 0);
  54. #endif
  55. } CARLA_SAFE_EXCEPTION_RETURN("lib_close", false);
  56. }
  57. /*
  58. * Get a library symbol (must not be null).
  59. * May return null if the symbol is not found.
  60. */
  61. static inline
  62. void* lib_symbol(void* const lib, const char* const symbol) noexcept
  63. {
  64. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  65. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', nullptr);
  66. #ifdef CARLA_OS_WIN
  67. return (void*)::GetProcAddress((HMODULE)lib, symbol);
  68. #else
  69. return ::dlsym(lib, symbol);
  70. #endif
  71. }
  72. /*
  73. * Return the last operation error ('filename' must not be null).
  74. * May return null.
  75. */
  76. static inline
  77. const char* lib_error(const char* const filename) noexcept
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);
  80. #ifdef CARLA_OS_WIN
  81. static char libError[2048+1];
  82. carla_zeroChar(libError, 2048+1);
  83. try {
  84. const DWORD winErrorCode = ::GetLastError();
  85. const int winErrorFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
  86. LPVOID winErrorString;
  87. ::FormatMessage(winErrorFlags, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  88. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  89. ::LocalFree(winErrorString);
  90. } CARLA_SAFE_EXCEPTION("lib_error");
  91. return (libError[0] != '\0') ? libError : nullptr;
  92. #else
  93. return ::dlerror();
  94. #endif
  95. }
  96. // -----------------------------------------------------------------------
  97. #endif // CARLA_LIB_UTILS_HPP_INCLUDED