Patch 2793067: fix trunk with OGRE_THREAD_SUPPORT=1 on non-Windows platforms (don...
[ogre3d.git] / OgreMain / src / OgreDynLib.cpp
blob14abb7158a2974d1eeb90cacb4b5d6a46eed378a
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/
7 Copyright (c) 2000-2006 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
13 version.
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22 http://www.gnu.org/copyleft/lesser.txt.
24 You may alternatively use this source under the terms of a specific version of
25 the OGRE Unrestricted License provided you have obtained such a license from
26 Torus Knot Software Ltd.
27 -----------------------------------------------------------------------------
29 #include "OgreStableHeaders.h"
31 #include "OgreDynLib.h"
33 #include "OgreException.h"
34 #include "OgreLogManager.h"
36 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
37 # define WIN32_LEAN_AND_MEAN
38 # define NOMINMAX // required to stop windows.h messing up std::min
39 # include <windows.h>
40 #endif
42 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
43 # include "macPlugins.h"
44 #endif
47 namespace Ogre {
49 //-----------------------------------------------------------------------
50 DynLib::DynLib( const String& name )
52 mName = name;
53 m_hInst = NULL;
56 //-----------------------------------------------------------------------
57 DynLib::~DynLib()
61 //-----------------------------------------------------------------------
62 void DynLib::load()
64 // Log library load
65 LogManager::getSingleton().logMessage("Loading library " + mName);
67 String name = mName;
68 #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
69 // dlopen() does not add .so to the filename, like windows does for .dll
70 if (name.substr(name.length() - 3, 3) != ".so")
71 name += ".so";
72 #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
73 // dlopen() does not add .dylib to the filename, like windows does for .dll
74 if (name.substr(name.length() - 6, 6) != ".dylib")
75 name += ".dylib";
76 #elif OGRE_PLATFORM == OGRE_PLATFORM_WIN32
77 // Although LoadLibraryEx will add .dll itself when you only specify the library name,
78 // if you include a relative path then it does not. So, add it to be sure.
79 if (name.substr(name.length() - 4, 4) != ".dll")
80 name += ".dll";
81 #endif
82 m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( name.c_str() );
84 if( !m_hInst )
85 OGRE_EXCEPT(
86 Exception::ERR_INTERNAL_ERROR,
87 "Could not load dynamic library " + mName +
88 ". System Error: " + dynlibError(),
89 "DynLib::load" );
92 //-----------------------------------------------------------------------
93 void DynLib::unload()
95 // Log library unload
96 LogManager::getSingleton().logMessage("Unloading library " + mName);
98 if( DYNLIB_UNLOAD( m_hInst ) )
100 OGRE_EXCEPT(
101 Exception::ERR_INTERNAL_ERROR,
102 "Could not unload dynamic library " + mName +
103 ". System Error: " + dynlibError(),
104 "DynLib::unload");
109 //-----------------------------------------------------------------------
110 void* DynLib::getSymbol( const String& strName ) const throw()
112 return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
114 //-----------------------------------------------------------------------
115 String DynLib::dynlibError( void )
117 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
118 LPVOID lpMsgBuf;
119 FormatMessage(
120 FORMAT_MESSAGE_ALLOCATE_BUFFER |
121 FORMAT_MESSAGE_FROM_SYSTEM |
122 FORMAT_MESSAGE_IGNORE_INSERTS,
123 NULL,
124 GetLastError(),
125 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
126 (LPTSTR) &lpMsgBuf,
128 NULL
130 String ret = (char*)lpMsgBuf;
131 // Free the buffer.
132 LocalFree( lpMsgBuf );
133 return ret;
134 #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
135 return String(dlerror());
136 #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
137 return String(mac_errorBundle());
138 #else
139 return String("");
140 #endif