CmDynLib.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "CmDynLib.h"
  25. #include "CmException.h"
  26. #if CM_PLATFORM == CM_PLATFORM_WIN32
  27. # define WIN32_LEAN_AND_MEAN
  28. # if !defined(NOMINMAX) && defined(_MSC_VER)
  29. # define NOMINMAX // required to stop windows.h messing up std::min
  30. # endif
  31. # include <windows.h>
  32. #endif
  33. #if CM_PLATFORM == CM_PLATFORM_APPLE
  34. # include "macUtils.h"
  35. # include <dlfcn.h>
  36. #endif
  37. namespace CamelotEngine {
  38. //-----------------------------------------------------------------------
  39. DynLib::DynLib( const String& name )
  40. {
  41. mName = name;
  42. m_hInst = NULL;
  43. }
  44. //-----------------------------------------------------------------------
  45. DynLib::~DynLib()
  46. {
  47. }
  48. //-----------------------------------------------------------------------
  49. void DynLib::load()
  50. {
  51. String name = mName;
  52. #if CM_PLATFORM == CM_PLATFORM_LINUX
  53. // dlopen() does not add .so to the filename, like windows does for .dll
  54. if (name.substr(name.length() - 3, 3) != ".so")
  55. name += ".so";
  56. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  57. // dlopen() does not add .dylib to the filename, like windows does for .dll
  58. if (name.substr(name.length() - 6, 6) != ".dylib")
  59. name += ".dylib";
  60. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  61. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  62. // if you include a relative path then it does not. So, add it to be sure.
  63. if (name.substr(name.length() - 4, 4) != ".dll")
  64. name += ".dll";
  65. #endif
  66. m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() );
  67. if( !m_hInst )
  68. {
  69. CM_EXCEPT(InternalErrorException,
  70. "Could not load dynamic library " + mName +
  71. ". System Error: " + dynlibError());
  72. }
  73. }
  74. //-----------------------------------------------------------------------
  75. void DynLib::unload()
  76. {
  77. if( DYNLIB_UNLOAD( m_hInst ) )
  78. {
  79. CM_EXCEPT(InternalErrorException,
  80. "Could not unload dynamic library " + mName +
  81. ". System Error: " + dynlibError());
  82. }
  83. }
  84. //-----------------------------------------------------------------------
  85. void* DynLib::getSymbol( const String& strName ) const throw()
  86. {
  87. return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
  88. }
  89. //-----------------------------------------------------------------------
  90. String DynLib::dynlibError( void )
  91. {
  92. #if CM_PLATFORM == CM_PLATFORM_WIN32
  93. LPVOID lpMsgBuf;
  94. FormatMessage(
  95. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  96. FORMAT_MESSAGE_FROM_SYSTEM |
  97. FORMAT_MESSAGE_IGNORE_INSERTS,
  98. NULL,
  99. GetLastError(),
  100. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  101. (LPTSTR) &lpMsgBuf,
  102. 0,
  103. NULL
  104. );
  105. String ret = (char*)lpMsgBuf;
  106. // Free the buffer.
  107. LocalFree( lpMsgBuf );
  108. return ret;
  109. #elif CM_PLATFORM == CM_PLATFORM_LINUX || CM_PLATFORM == CM_PLATFORM_APPLE
  110. return String(dlerror());
  111. #else
  112. return String("");
  113. #endif
  114. }
  115. }