BsDynLib.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #if BS_PLATFORM == BS_PLATFORM_WIN32
  4. # define DYNLIB_HANDLE hInstance
  5. # define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
  6. # define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
  7. # define DYNLIB_UNLOAD( a ) !FreeLibrary( a )
  8. struct HINSTANCE__;
  9. typedef struct HINSTANCE__* hInstance;
  10. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  11. # define DYNLIB_HANDLE void*
  12. # define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
  13. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  14. # define DYNLIB_UNLOAD( a ) dlclose( a )
  15. #elif BS_PLATFORM == BS_PLATFORM_APPLE
  16. # define DYNLIB_HANDLE void*
  17. # define DYNLIB_LOAD( a ) mac_loadDylib( a )
  18. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  19. # define DYNLIB_UNLOAD( a ) dlclose( a )
  20. #endif
  21. namespace BansheeEngine
  22. {
  23. /**
  24. * @brief Class that holds data about a dynamic library.
  25. */
  26. class BS_UTILITY_EXPORT DynLib
  27. {
  28. public:
  29. /**
  30. * @brief Constructs the dynamic library object and loads the library with the specified name.
  31. */
  32. DynLib(const String& name);
  33. ~DynLib();
  34. /**
  35. * @brief Loads the library. Does nothing if library is already loaded.
  36. */
  37. void load();
  38. /**
  39. * @brief Unloads the library. Does nothing if library is not loaded.
  40. */
  41. void unload();
  42. /**
  43. * @brief Get the name of the library.
  44. */
  45. const String& getName() const { return mName; }
  46. /**
  47. * @brief Returns the address of the given symbol from the loaded library.
  48. *
  49. * @param strName The name of the symbol to search for.
  50. *
  51. * @returns If the function succeeds, the returned value is a handle to
  52. * the symbol. Otherwise null.
  53. */
  54. void* getSymbol(const String& strName) const;
  55. protected:
  56. friend class DynLibManager;
  57. /**
  58. * @brief Gets the last loading error.
  59. */
  60. String dynlibError();
  61. protected:
  62. String mName;
  63. DYNLIB_HANDLE m_hInst; // Handle to the loaded library.
  64. };
  65. }