BsDynLib.h 2.3 KB

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