BsDynLib.h 2.2 KB

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