CmDynLib.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmString.h"
  27. #if CM_PLATFORM == CM_PLATFORM_WIN32
  28. # define DYNLIB_HANDLE hInstance
  29. # define DYNLIB_LOAD( a ) LoadLibraryEx( a, NULL, LOAD_WITH_ALTERED_SEARCH_PATH )
  30. # define DYNLIB_GETSYM( a, b ) GetProcAddress( a, b )
  31. # define DYNLIB_UNLOAD( a ) !FreeLibrary( a )
  32. struct HINSTANCE__;
  33. typedef struct HINSTANCE__* hInstance;
  34. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  35. # define DYNLIB_HANDLE void*
  36. # define DYNLIB_LOAD( a ) dlopen( a, RTLD_LAZY | RTLD_GLOBAL)
  37. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  38. # define DYNLIB_UNLOAD( a ) dlclose( a )
  39. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  40. # define DYNLIB_HANDLE void*
  41. # define DYNLIB_LOAD( a ) mac_loadDylib( a )
  42. # define DYNLIB_GETSYM( a, b ) dlsym( a, b )
  43. # define DYNLIB_UNLOAD( a ) dlclose( a )
  44. #endif
  45. namespace CamelotFramework {
  46. /** \addtogroup Core
  47. * @{
  48. */
  49. /** \addtogroup General
  50. * @{
  51. */
  52. /** Resource holding data about a dynamic library.
  53. @remarks
  54. This class holds the data required to get symbols from
  55. libraries loaded at run-time (i.e. from DLL's for so's)
  56. @author
  57. Adrian Cearn„u ([email protected])
  58. @since
  59. 27 January 2002
  60. @see
  61. Resource
  62. */
  63. class CM_UTILITY_EXPORT DynLib
  64. {
  65. protected:
  66. String mName;
  67. /// Gets the last loading error
  68. String dynlibError(void);
  69. public:
  70. /** Default constructor - used by DynLibManager.
  71. @warning
  72. Do not call directly
  73. */
  74. DynLib( const String& name );
  75. /** Default destructor.
  76. */
  77. ~DynLib();
  78. /** Load the library
  79. */
  80. void load();
  81. /** Unload the library
  82. */
  83. void unload();
  84. /// Get the name of the library
  85. const String& getName(void) const { return mName; }
  86. /**
  87. Returns the address of the given symbol from the loaded library.
  88. @param
  89. strName The name of the symbol to search for
  90. @returns
  91. If the function succeeds, the returned value is a handle to
  92. the symbol.
  93. @par
  94. If the function fails, the returned value is <b>NULL</b>.
  95. */
  96. void* getSymbol( const String& strName ) const throw();
  97. protected:
  98. /// Handle to the loaded library.
  99. DYNLIB_HANDLE m_hInst;
  100. };
  101. /** @} */
  102. /** @} */
  103. }