KeosDynLib.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #include "KeosDynLib.h"
00023 #include "KeosException.h"
00024 #include "KeosLogger.h"
00025 
00026 #if KEOS_PLATFORM == KEOS_PLATFORM_WIN32
00027 #   define WIN32_LEAN_AND_MEAN
00028 #   include <windows.h>
00029 #endif
00030 
00031 namespace Keos
00032 {
00033   //=======================================================================
00034   // CDynLib implementation
00035   //=======================================================================
00036 
00037   //-----------------------------------------------------------------------
00038   CDynLib::CDynLib( const String& strName )
00039   {
00040     m_strName = strName;
00041     m_hInst = NULL;
00042   }
00043 
00044   //-----------------------------------------------------------------------
00045   CDynLib::~CDynLib()
00046   {}
00047 
00048   //-----------------------------------------------------------------------
00049   void CDynLib::Load()
00050   {
00051     // Log library load
00052     ILogger::Log("Loading library %s", m_strName.c_str());
00053 
00054     String strName = m_strName;
00055 #if KEOS_PLATFORM == KEOS_PLATFORM_LINUX
00056     // dlopen() does not add .so to the filename, like windows does for .dll
00057     if (strName.substr(strName.length() - 3, 3) != ".so")
00058       strName += ".so";
00059 #endif
00060     m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( strName.c_str() );
00061 
00062     if ( !m_hInst )
00063     {
00064       DYNLIB_EXCEPT(m_strName,
00065                     "Could not load dynamic library " + m_strName +  "\nSystem Error: " + DynlibError(),
00066                     "CDynLib::Load");
00067     }
00068   }
00069 
00070   //-----------------------------------------------------------------------
00071   void CDynLib::Unload()
00072   {
00073     // Log library load
00074     ILogger::Log("Unloading library %s", m_strName.c_str());
00075 
00076     if ( DYNLIB_UNLOAD( m_hInst ) )
00077     {
00078       DYNLIB_EXCEPT(m_strName,
00079                     "Could not unload dynamic library " + m_strName +  "\nSystem Error: " + DynlibError(),
00080                     "CDynLib::Unload");
00081     }
00082   }
00083 
00084   //-----------------------------------------------------------------------
00085   void* CDynLib::GetSymbol( const String& strName ) const throw()
00086   {
00087     return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
00088   }
00089 
00090   //-----------------------------------------------------------------------
00091   String CDynLib::DynlibError( void )
00092   {
00093 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
00094     LPVOID lpMsgBuf;
00095     FormatMessage(
00096       FORMAT_MESSAGE_ALLOCATE_BUFFER |
00097       FORMAT_MESSAGE_FROM_SYSTEM |
00098       FORMAT_MESSAGE_IGNORE_INSERTS,
00099       NULL,
00100       GetLastError(),
00101       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00102       (LPTSTR) &lpMsgBuf,
00103       0,
00104       NULL
00105     );
00106     String ret = (char*)lpMsgBuf;
00107     // Free the buffer.
00108     LocalFree( lpMsgBuf );
00109     return ret;
00110 #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
00111     return String(dlerror());
00112 #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00113     return String(mac_errorBundle());
00114 #else
00115     return String("");
00116 #endif
00117   }
00118 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1