MyGUI  3.2.1
MyGUI_DynLib.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_DynLib.h"
00009 
00010 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00011 #   include <windows.h>
00012 #elif MYGUI_PLATFORM == MYGUI_PLATFORM_LINUX
00013 #   include <dlfcn.h>
00014 #endif
00015 
00016 namespace MyGUI
00017 {
00018     DynLib::DynLib(const std::string& name) :
00019         mName(name),
00020         mInstance(nullptr)
00021     {
00022     }
00023 
00024     DynLib::~DynLib()
00025     {
00026     }
00027 
00028     bool DynLib::load()
00029     {
00030         // Log library load
00031         MYGUI_LOG(Info, "Loading library " << mName);
00032 
00033 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00034         //APPLE SPECIFIC CODE HERE
00035 #else
00036         mInstance = (MYGUI_DYNLIB_HANDLE)MYGUI_DYNLIB_LOAD( mName.c_str() );
00037 #endif
00038 
00039         return mInstance != 0;
00040     }
00041 
00042 
00043     void DynLib::unload()
00044     {
00045         // Log library unload
00046         MYGUI_LOG(Info, "Unloading library " << mName);
00047 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00048         //APPLE SPECIFIC CODE HERE
00049 #else
00050         if (MYGUI_DYNLIB_UNLOAD(mInstance))
00051         {
00052             MYGUI_EXCEPT("Could not unload dynamic library '" << mName << "'. System Error: " << dynlibError());
00053         }
00054 #endif
00055     }
00056 
00057     void* DynLib::getSymbol( const std::string& strName ) const throw()
00058     {
00059 #if MYGUI_PLATFORM == MYGUI_PLATFORM_APPLE
00060         //APPLE SPECIFIC CODE HERE
00061         return nullptr;
00062 #else
00063         return (void*)MYGUI_DYNLIB_GETSYM(mInstance, strName.c_str());
00064 #endif
00065     }
00066 
00067     std::string DynLib::dynlibError() const
00068     {
00069 #if MYGUI_PLATFORM == MYGUI_PLATFORM_WIN32
00070         LPVOID lpMsgBuf;
00071         FormatMessage(
00072             FORMAT_MESSAGE_ALLOCATE_BUFFER |
00073             FORMAT_MESSAGE_FROM_SYSTEM |
00074             FORMAT_MESSAGE_IGNORE_INSERTS,
00075             NULL,
00076             GetLastError(),
00077             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00078             (LPTSTR) &lpMsgBuf,
00079             0,
00080             NULL);
00081         std::string ret = (char*)lpMsgBuf;
00082         // Free the buffer.
00083         LocalFree( lpMsgBuf );
00084         return ret;
00085 #else
00086         return "no unix error function defined yet";
00087 #endif
00088     }
00089 
00090     std::string DynLib::getName(void) const
00091     {
00092         return mName;
00093     }
00094 
00095 } // namespace MyGUI