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