BsDynLib.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. ~DynLib();
  30. /**
  31. * @brief Loads the library. Does nothing if library is already loaded.
  32. */
  33. void load();
  34. /**
  35. * @brief Unloads the library. Does nothing if library is not loaded.
  36. */
  37. void unload();
  38. /**
  39. * @brief Get the name of the library.
  40. */
  41. const String& getName() const { return mName; }
  42. /**
  43. * @brief Returns the address of the given symbol from the loaded library.
  44. *
  45. * @param strName The name of the symbol to search for.
  46. *
  47. * @returns If the function succeeds, the returned value is a handle to
  48. * the symbol. Otherwise null.
  49. */
  50. void* getSymbol(const String& strName) const;
  51. protected:
  52. friend class DynLibManager;
  53. DynLib(const String& name);
  54. /**
  55. * @brief Gets the last loading error.
  56. */
  57. String dynlibError();
  58. protected:
  59. String mName;
  60. DYNLIB_HANDLE m_hInst; // Handle to the loaded library.
  61. };
  62. }