00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00052 ILogger::Log("Loading library %s", m_strName.c_str());
00053
00054 String strName = m_strName;
00055 #if KEOS_PLATFORM == KEOS_PLATFORM_LINUX
00056
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
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
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 }