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 2.4KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Carla library utils
  3. * Copyright (C) 2011-2013 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. static inline
  26. void* lib_open(const char* const filename)
  27. {
  28. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  29. #ifdef CARLA_OS_WIN
  30. return (void*)LoadLibraryA(filename);
  31. #else
  32. return dlopen(filename, RTLD_NOW|RTLD_LOCAL);
  33. #endif
  34. }
  35. static inline
  36. bool lib_close(void* const lib)
  37. {
  38. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, false);
  39. #ifdef CARLA_OS_WIN
  40. return FreeLibrary((HMODULE)lib);
  41. #else
  42. return (dlclose(lib) == 0);
  43. #endif
  44. }
  45. static inline
  46. void* lib_symbol(void* const lib, const char* const symbol)
  47. {
  48. CARLA_SAFE_ASSERT_RETURN(lib != nullptr, nullptr);
  49. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr, nullptr);
  50. #ifdef CARLA_OS_WIN
  51. return (void*)GetProcAddress((HMODULE)lib, symbol);
  52. #else
  53. return dlsym(lib, symbol);
  54. #endif
  55. }
  56. static inline
  57. const char* lib_error(const char* const filename)
  58. {
  59. CARLA_SAFE_ASSERT_RETURN(filename != nullptr, nullptr);
  60. #ifdef CARLA_OS_WIN
  61. static char libError[2048+1];
  62. carla_zeroChar(libError, 2048+1);
  63. LPVOID winErrorString;
  64. DWORD winErrorCode = GetLastError();
  65. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, winErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&winErrorString, 0, nullptr);
  66. std::snprintf(libError, 2048, "%s: error code %li: %s", filename, winErrorCode, (const char*)winErrorString);
  67. LocalFree(winErrorString);
  68. return libError;
  69. #else
  70. return dlerror();
  71. #endif
  72. }
  73. // -----------------------------------------------------------------------
  74. #endif // CARLA_LIB_UTILS_HPP_INCLUDED