MyGUI  3.0.1
MyGUI_DynLib.cpp
Go to the documentation of this file.
00001 
00008 /*
00009     This file is part of MyGUI.
00010 
00011     MyGUI is free software: you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     MyGUI is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 #include "MyGUI_Precompiled.h"
00025 #include "MyGUI_DynLib.h"
00026 
00027 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00028 #   include <Windows.h>
00029 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
00030 #       include <dlfcn.h>
00031 #endif
00032 
00033 namespace MyGUI
00034 {
00035     DynLib::DynLib( const std::string& name )
00036     {
00037         mName = name;
00038         mInstance = nullptr;
00039     }
00040 
00041 
00042     DynLib::~DynLib()
00043     {
00044     }
00045 
00046 
00047     bool DynLib::load()
00048     {
00049         // Log library load
00050         MYGUI_LOG(Info, "Loading library " << mName);
00051 
00052         #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00053             //APPLE SPECIFIC CODE HERE
00054         #else
00055             mInstance = (MYGUI_DYNLIB_HANDLE)MYGUI_DYNLIB_LOAD( mName.c_str() );
00056         #endif
00057 
00058         return mInstance != 0;
00059     }
00060 
00061 
00062     void DynLib::unload()
00063     {
00064         // Log library unload
00065         MYGUI_LOG(Info, "Unloading library " << mName);
00066         #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00067             //APPLE SPECIFIC CODE HERE
00068         #else
00069             if (MYGUI_DYNLIB_UNLOAD(mInstance))
00070             {
00071                 MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
00072             }
00073         #endif
00074     }
00075 
00076     void* DynLib::getSymbol( const std::string& strName ) const throw()
00077     {
00078         #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00079             //APPLE SPECIFIC CODE HERE
00080             return nullptr;
00081         #else
00082             return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName.c_str());
00083         #endif
00084     }
00085 
00086     std::string DynLib::dynlibError( void )
00087     {
00088 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00089         LPVOID lpMsgBuf;
00090         FormatMessage(
00091             FORMAT_MESSAGE_ALLOCATE_BUFFER |
00092             FORMAT_MESSAGE_FROM_SYSTEM |
00093             FORMAT_MESSAGE_IGNORE_INSERTS,
00094             NULL,
00095             GetLastError(),
00096             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00097             (LPTSTR) &lpMsgBuf,
00098             0,
00099             NULL
00100             );
00101         std::string ret = (char*)lpMsgBuf;
00102         // Free the buffer.
00103         LocalFree( lpMsgBuf );
00104         return ret;
00105 #else
00106         return "no unix error function defined yet";
00107 #endif
00108     }
00109 
00110 } // namespace MyGUI