BsDynLib.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 || BS_PLATFORM == BS_PLATFORM_OSX
  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. #endif
  25. /** Class that holds data about a dynamic library. */
  26. class BS_UTILITY_EXPORT DynLib
  27. {
  28. public:
  29. /** System-specific file extension for a dynamic library (e.g. "dll"). */
  30. static const char* EXTENSION;
  31. /** Constructs the dynamic library object and loads the library with the specified name. */
  32. DynLib(const String& name);
  33. ~DynLib();
  34. /** Loads the library. Does nothing if library is already loaded. */
  35. void load();
  36. /** Unloads the library. Does nothing if library is not loaded. */
  37. void unload();
  38. /** Get the name of the library. */
  39. const String& getName() const { return mName; }
  40. /**
  41. * Returns the address of the given symbol from the loaded library.
  42. *
  43. * @param[in] strName The name of the symbol to search for.
  44. * @return If the function succeeds, the returned value is a handle to the symbol. Otherwise null.
  45. */
  46. void* getSymbol(const String& strName) const;
  47. protected:
  48. friend class DynLibManager;
  49. /** Gets the last loading error. */
  50. String dynlibError();
  51. protected:
  52. String mName;
  53. DYNLIB_HANDLE m_hInst; // Handle to the loaded library.
  54. };
  55. /** @} */
  56. }