BsDynLib.h 2.0 KB

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