Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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     //-----------------------------------------------------------------------
    00050     void CDynLib::Load()
    00051     {
    00052         // Log library load
    00053         ILogger::Log("Loading library %s", m_strName.c_str());
    00054
    00055                 String strName = m_strName;
    00056 #if KEOS_PLATFORM == KEOS_PLATFORM_LINUX
    00057         // dlopen() does not add .so to the filename, like windows does for .dll
    00058         if (strName.substr(strName.length() - 3, 3) != ".so")
    00059            strName += ".so";
    00060 #endif
    00061         m_hInst = (DYNLIB_HANDLE)DYNLIB_LOAD( strName.c_str() );
    00062
    00063         if( !m_hInst )
    00064         {
    00065             DYNLIB_EXCEPT(m_strName,
    00066             "Could not load dynamic library " + m_strName +  "\nSystem Error: " + DynlibError(),
    00067             "CDynLib::Load");
    00068         }
    00069     }
    00070
    00071     //-----------------------------------------------------------------------
    00072     void CDynLib::Unload()
    00073     {
    00074         // Log library load
    00075         ILogger::Log("Unloading library %s", m_strName.c_str());
    00076
    00077         if( DYNLIB_UNLOAD( m_hInst ) )
    00078                 {
    00079             DYNLIB_EXCEPT(m_strName,
    00080             "Could not unload dynamic library " + m_strName +  "\nSystem Error: " + DynlibError(),
    00081             "CDynLib::Unload");
    00082                 }
    00083     }
    00084
    00085     //-----------------------------------------------------------------------
    00086     void* CDynLib::GetSymbol( const String& strName ) const throw()
    00087     {
    00088         return (void*)DYNLIB_GETSYM( m_hInst, strName.c_str() );
    00089     }
    00090
    00091     //-----------------------------------------------------------------------
    00092     String CDynLib::DynlibError( void )
    00093     {
    00094 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
    00095         LPVOID lpMsgBuf;
    00096         FormatMessage(
    00097             FORMAT_MESSAGE_ALLOCATE_BUFFER |
    00098             FORMAT_MESSAGE_FROM_SYSTEM |
    00099             FORMAT_MESSAGE_IGNORE_INSERTS,
    00100             NULL,
    00101             GetLastError(),
    00102             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    00103             (LPTSTR) &lpMsgBuf,
    00104             0,
    00105             NULL
    00106             );
    00107         String ret = (char*)lpMsgBuf;
    00108         // Free the buffer.
    00109         LocalFree( lpMsgBuf );
    00110         return ret;
    00111 #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
    00112         return String(dlerror());
    00113 #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
    00114         return String(mac_errorBundle());
    00115 #else
    00116         return String("");
    00117 #endif
    00118     }
    00119 } // namespace Keos